Codeforces 651D Image Preview【思维+二分+前缀后缀和】

来源:互联网 发布:交换机端口上的ip 编辑:程序博客网 时间:2024/06/04 18:03

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 photo1. It takesa 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 andcan't be rotated. It takesb 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 spends1 second to notice all details in it. If photo is in the wrong orientation, he spendsb 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 photoi should be seen in thehorizontal orientation.

If the i-th position of a string contains 'h', then the photoi should be seen invertical orientation.

Output

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

Examples
Input
4 2 3 10wwhw
Output
2
Input
5 2 4 13hhwhh
Output
4
Input
5 2 4 1000hhwhh
Output
5
Input
3 1 100 10whw
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张照片,h表示竖着的,w表示横着的,横着的图片将其竖过来需要b时间,滑动一下图片需要a时间。

如果一张照片没看过,那么需要1时间去看,如果看过了,滑动到这个位子了,没有任何操作。

必须看完一张照片才能看下一张照片,不能跳过某照片。

照片是一个圈,从第一张照片可以滑动到最后一张。滑动操作可以选择左或者右。


思路:


1、我们首先确定一点,因为不能跳过任意一张照片,所以我们肯定看过了的照片是一个连续的区间。

那么我们可以O(n)去枚举区间的左端点。

又因为枚举到的右端点越远,越有可能做不到(达不到目的),所以我们这里可以二分右端点。


2、注意两个方向去走,所以我们可以维护一个后缀和一个前缀和,然后贪心的去判断往哪边先走更优,然后O(1)查询当前左右端点是否合法,如果不合法,减少右端点,否则加大右端点。

总时间复杂度:O(nlongn);


Ac代码:

#include<stdio.h>#include<iostream>#include<string.h>using namespace std;char s[500060];int sum[500060];int back[500060];int output;int n,a,b,t;int Slove(int mid,int posend){    int have=t-back[posend]-min((n-posend+1)*a,(mid-1)*a);    if(have<=0)return 0;    else    {        int ned=sum[mid]-sum[1];        if(ned<=have)return 1;        else return 0;    }}int main(){    while(~scanf("%d%d%d%d",&n,&a,&b,&t))    {        scanf("%s",s+1);        output=0;        memset(sum,0,sizeof(sum));        for(int i=1;i<=n;i++)        {            if(i==1)            {                int tmp=0;                if(s[i]=='w')tmp=b;                sum[i]=1+tmp;            }            else            {                int tmp=0;                if(s[i]=='w')tmp=b;                sum[i]=1+a+tmp+sum[i-1];            }            if(sum[i]<=t)output=max(i,output);        }        int tmp=0;        if(s[1]=='w')tmp=b;        back[1]=1+tmp;        for(int i=n;i>=2;i--)        {            if(i==n)            {                tmp=0;                if(s[i]=='w')tmp=b;                back[i]=1+tmp+back[1]+a;            }            else            {                tmp=0;                if(s[i]=='w')tmp=b;                back[i]=1+a+tmp+back[i+1];            }        }        for(int i=2;i<=n;i++)        {            if(back[i]>t)continue;            else            {                int tmpans=n-i+2;                int l=1;                int r=i-1;                while(r-l>=0)                {                    int mid=(l+r)/2;                    if(Slove(mid,i)==1)                    {                        tmpans=n-i+2+mid-1;                        l=mid+1;                    }                    else r=mid-1;                }                output=max(tmpans,output);            }        }        printf("%d\n",output);    }}






0 0