Codeforces Round #345 (Div. 2)D Image Preview

D. Image Preview
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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·1051 ≤ a, b ≤ 10001 ≤ 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張照片從第一張開始看,每次可以向左翻一張向右翻一張,照片有橫着的有豎着的手機屏是豎着的旋轉一張照片需要的時間是b翻到下一張需時間a給T 的時間問最多能看的照片,必須連續看看中間不允許跳過照片,看一張照片需要1s。

解題思路:因爲只能從第一張開始看所以每次枚舉能看到的最左邊的位置即可。

/* ***********************************************
Author       : ryc
Created Time : 2016-08-10 Wednesday
File Name    : E:\acm\codeforces\345D.cpp
Language     : c++
Copyright 2016 ryc All Rights Reserved
************************************************ */
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
#include<map>
using namespace std;
typedef pair<int,int > pii;
typedef long long LL;
const int maxn=500010;
LL n,a,b,T;
LL num[maxn];
char str[maxn];
bool judge(LL mid){
    for(LL i=1;i<=mid;++i){
        LL t1=num[i]+(num[n]-num[n-(mid-i)]);
        LL t2=min((i-1ll)*a*2ll+(mid-i)*a,(mid-i)*a*2ll+(i-1ll)*a);
        if(t1+t2<=T)
            return true;
    }
    return false;
}
int main()
{
    scanf("%lld%lld%lld%lld%s",&n,&a,&b,&T,str+1);
    LL left=0,right=n,ans=0;
    for(int i=1;i<=n;++i){
        num[i]=num[i-1]+1;
        if(str[i]=='w')num[i]+=b;
    }
    while(left<=right){
        LL mid=(left+right)>>1ll;
        if(judge(mid)){
            ans=mid;left=mid+1ll;
        }
        else {
            right=mid-1;
        }
    }
    printf("%lld\n",ans);
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章