#345 div2 D. Image Preview

Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent.

For each photo it is known which orientation is intended for it — horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo.

Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos.

Help Vasya find the maximum number of photos he is able to watch during T seconds.

Input

The first line of the input contains 4 integers n, a, b, T (1 ≤ n ≤ 5·105, 1 ≤ a, b ≤ 1000, 1 ≤ T ≤ 109) — the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo.

Second line of the input contains a string of length n containing symbols 'w' and 'h'.

If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation.

If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation.

Output

Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds.

Examples
Input
4 2 3 10
wwhw
Output
2
Input
5 2 4 13
hhwhh
Output
4
Input
5 2 4 1000
hhwhh
Output
5
Input
3 1 100 10
whw
Output
0
Note

In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds.

Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.


思路:

做过最恶心的题之一

首先从想法上来说,只可能有一次折返,所以就分别向右和向左枚举折返的点,然后二分查找反方向能到达的最远点

可以将字符串复制一遍放到n+1->2*n的位置上,但这样做在涉及向左向右移动的时候下标的更改不是一般的麻烦和容易出错

关于二分,left和right的赋值是一个比较新颖的地方,值得注意一下

剩下的全是细节,各种该死的细节

比如while(left<=right)区域内的tmp变量,它的初始值的设置是个非常令人头疼的事情,你要很充分的理解这个变量的实质究竟是什么才能搞对


#include <iostream>
#include <cstring>
#define maxn 500007
using namespace std;

char s[maxn];
int l2r[maxn];
int r2l[maxn];//能用一个数组表示的不要轻易的换成两个连续的数组
              //既浪费了空间,还由于要考虑index的+-size问题而容易引起错误 

int main()
{
    int n,a,b,T;
    while(cin>>n>>a>>b>>T)
    {
        int l_most = 1;
        int r_most = n-1;
        cin>>s;
        l2r[0] = s[0]=='w'?1+b:1;
        for(int i = 1;i < n;i++) 
            l2r[i] = l2r[i-1]+1+a+b*(s[i]=='w');
        r2l[n-1] = s[n-1]=='w'?l2r[0]+a+1+b:l2r[0]+a+1;
        for(int i = n-2;i > 0;i--) 
            r2l[i] = r2l[i+1]+1+a+b*(s[i]=='w');
        
        int ans = 0;
        //left
        for(int i = n-1;i>0 && r2l[i]<=T;i--)
        {
            if(r2l[i]+a*(n-i)<T) {
                int tmp = 0;
                int left = 0;
                int right = i-1;
                int mid;
                while(left <= right) {
                    mid = (left+right)>>1;
                    if(r2l[i]+a*(n-i)+l2r[mid]-l2r[0] <= T){
                        tmp = mid;
                        left = mid+1;
                    }
                    else 
                        right = mid-1;
                }
                ans = max(ans,n-i+tmp+1);
            }
            else 
                ans = max(ans,n-i+1);        
        }
        //right
        for(int i = 0;i<=n-1 && l2r[i]<=T;i++) 
        {
            if(l2r[i]+a*i < T) {
                int tmp = n;
                int left = i+1;
                int right = n-1;
                int mid;
                while(left <= right) {
                    mid = (left+right)>>1;
                    if(l2r[i]+a*i+r2l[mid]-l2r[0] <= T) {
                        tmp = mid;
                        right = mid-1;
                    }
                    else 
                        left = mid+1;
                }
                ans = max(ans,i+1+n-tmp);
            }
            else 
                ans = max(ans,i+1);
        }
        cout<<ans<<endl;
    } 
    return 0;
}
原文地址:https://www.cnblogs.com/immortal-worm/p/5973755.html