Codeforces 650B 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·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.

-------------------------------------------------------

仔细读题啊!

Solution:

看了题解过的,果然萎得不行。

我能想到的:

看的图片必然连续,因为每次只能选择看右边或左边还没看的那张图。

最后看的图的分布:

#(n-l)#(n-l+1)#(n-l+2)#...#(n-1)#(n)#(1)#(2)#(3)...#(r)

|<--------------------------------------------               

|_____________________________------------------->

               --------------------- >|

  <-----------------------------------------————————|

(#表示图片,括号内是其ID,虚线表示看图,实线表示跳过)

但是我TM就是没看出来要达到最有解,翻图的路线只有两种情况:

对于给定的(l, r)看图的时间(包括调整方向与看)是固定,所以总时间取决于翻图的次数。

---------------------------------------------------------------------------------------------------------------------

所以最优解只有四种情况:

1.一直向右翻

2.一直向左翻

3.先向左翻,再向右翻

4.先向右翻再向左翻

后两种情况可以用双指针(two-pointers)做。

------------------------------------------------------------------

Implementation:

two-pointers写得磕磕绊绊,coding弱得不行,sigh.

(Ignore the comment like a compiler :D)

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N(5e5+5);

char o[N];
int n, a, b, T;

int cost(int x){
    return 1+(o[x]=='w')*b+(bool)x*a;
}

int main(){
    for(;cin>>n>>a>>b>>T>>o;){
        int ans=0;
        for(;cost(0)<=T;){
            int t=T-cost(0), i, j;
            //two-pointers
            for(i=1; i<n; i++){
                if(cost(i)>t) break;
                t-=cost(i);
            }
            ans=max(ans, i);
            if(ans==n) break;

            // L.I.:
            // i-1: current position to the righ;
            for(t-=(i-1)*a, j=0; i>=2; t+=cost(i-1)+a, i--){
                // L.I.:
                // j:offset to the left
                // n-j-1:current left position
                for(;j<n-i && cost(n-j-1)<=t; j++){
                    t-=cost(n-j-1);
                }
                ans=max(ans, i+j);
            }

            if(ans==n) break;

            t=T-cost(0);
            for(i=1; i<n; i++){
                if(cost(n-i)>t) break;
                t-=cost(n-i);
            }

            ans=max(ans, i);
            if(ans==n) break;

            // n-i+1: last position
            for(t-=(i-1)*a, j=0; i>=2; t+=cost(n-i+1)+a, i--){//one step back
                for( ; j<n-i && cost(j+1)<=t; j++){
                    t-=cost(j+1);
                }
                ans=max(ans, i+j);  
            }
            break;
        }

        cout<<ans<<'\n';
    }
    return 0;
}

 

 

 

 

 

 

转载于:https://www.cnblogs.com/Patt/p/5266889.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值