Codeforces 650B Image Preview

时间:2021-09-10 08:31:25
  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+); char o[N];
int n, a, b, T; int cost(int x){
return +(o[x]=='w')*b+(bool)x*a;
} int main(){
for(;cin>>n>>a>>b>>T>>o;){
int ans=;
for(;cost()<=T;){
int t=T-cost(), i, j;
//two-pointers
for(i=; 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-)*a, j=; i>=; t+=cost(i-)+a, i--){
// L.I.:
// j:offset to the left
// n-j-1:current left position
for(;j<n-i && cost(n-j-)<=t; j++){
t-=cost(n-j-);
}
ans=max(ans, i+j);
} if(ans==n) break; t=T-cost();
for(i=; 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-)*a, j=; i>=; t+=cost(n-i+)+a, i--){//one step back
for( ; j<n-i && cost(j+)<=t; j++){
t-=cost(j+);
}
ans=max(ans, i+j);
}
break;
} cout<<ans<<'\n';
}
return ;
}