CodeForces - 651D:Image Preview (雙指針&)
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
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
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
Input4 2 3 10Output
wwhw
2Input
5 2 4 13Output
hhwhh
4Input
5 2 4 1000Output
hhwhh
5Input
3 1 100 10Output
whw
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.
題意:最開始手機顯示第一張照片,每次滑動可以到達上一張照片或者下一張,滑動的時間為a;如果第一次看到某照片,會花1時間去觀察。如果照片是w型的,觀察前需要格外花B時間。求T時間裏最多能觀察到多少照片。
思路:環型的,先加倍。然後雙指針即可。
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; const int maxn=1000010; char c[maxn]; int a[maxn],sum[maxn]; int main() { int N,A,B,T,ans=0; scanf("%d%d%d%d%s",&N,&A,&B,&T,c+1); rep(i,1,N) a[i]=a[i+N]=c[i]==‘h‘?1:1+B; rep(i,1,N+N) sum[i]=sum[i-1]+a[i]; int L=2,R=N+1; while(L<=N+1&&R<=N+N){ while(R-L+1>N||sum[R]-sum[L-1]+(R-L+min(R-N-1,N+1-L))*A>T) L++; ans=max(ans,R-L+1); R++; } printf("%d\n",ans); return 0; }
CodeForces - 651D:Image Preview (雙指針&)