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, a, b, T (1 ≤ n ≤ 5·105, 1 ≤ a, b ≤ 1000, 1 ≤ T ≤ 109),然后一个字符串序列,
给你n个照片, 从第一个照片开始看, 如果一张照片是'w', 那么要花费b时间去反转他, 否则不用反转, 看一张从来没看过的照片要1时间, 从一张滑动到另一张要a时间。 如果一张照片看过, 则不用再反转, 也不用再话1s 。 求在不超过T时间的前提下最多能看多少张。

思路:很显然的一个二分答案,然后枚举左边看了多少张,右边看了多少张,这里有两种情况,先看左边和先看右边,分别判断一下就可以了。

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=500100;
__int64 a,b,cost[maxn],T;
__int64 pre[maxn],suf[maxn];
char s[maxn];

bool check(int m){
    for(int i=1;i<=m;i++){
        if(pre[i]+suf[m-i]+min(i-1,m-i)*a<=T)
            return true;
    }
    return false;
}

int main(){
    int n;
    while(scanf("%d%I64d%I64d%I64d",&n,&a,&b,&T)!=EOF){
        scanf("%s",s+1);
        pre[0]=0;
        for(int i=1;i<=n;i++){
            pre[i]=pre[i-1];
            if(i==1){
                if(s[i]=='w')
                    pre[i]=1+b;
                else
                    pre[i]=1;
                continue;
            }
            if(s[i]=='w')
                pre[i]=pre[i]+a+b+1;
            else
                pre[i]=pre[i]+a+1;//看右边i个需要花费的时间
        }
        suf[0]=0;
        for(int i=n,j=1;i>=1;i--,j++){
            suf[j]=suf[j-1];
            if(s[i]=='w')
                suf[j]=suf[j]+a+b+1;
            else
                suf[j]=suf[j]+a+1;//看左边i个需要花费的时间
        }
        int low=0,high=n,ans=0;
        while(high-low>=0){
            int mid=(low+high)>>1;
            if(check(mid)){
                ans=mid;
                low=mid+1;
            }
            else
                high=mid-1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值