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 ;
}

Codeforces 650B Image Preview的更多相关文章

  1. codeforces 650B . Image Preview 二分

    题目链接 B. Image Preview time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. Codeforces 650B Image Preview(尺取法)

    题目大概说手机有n张照片.通过左滑或者右滑循环切换照片,滑动需要花费a时间:看一张照片要1时间,而看过的可以马上跳过不用花时间,没看过的不能跳过:有些照片要横着看,要花b时间旋转方向.那么问T时间下最 ...

  3. 汕头市队赛 C KMP codeforces B. Image Preview

    汕头市队赛题目传送门 codeforces题目传送门 这道题我的做法是 尝试先往左走然后往右走 或者先往右走然后往左走 然后注意一下枚举顺序就okay啦 #include<cstdio> ...

  4. Codeforces 651D Image Preview【二分+枚举】

    题意: 若干张照片,从头开始可以向左右两边读,已经读过的不需要再读,有的照片需要翻转,给定读.滑动和翻转消耗的时间,求在给定时间内最多能读多少页? 分析: 首先明确,只横跨一次,即先一直读一边然后再一 ...

  5. Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分

    D. Image Preview 题目连接: http://www.codeforces.com/contest/651/problem/D Description Vasya's telephone ...

  6. codeforces 650D D. Image Preview (暴力+二分+dp)

    题目链接: http://codeforces.com/contest/651/problem/D D. Image Preview time limit per test 1 second memo ...

  7. Codeforces Round #345 D. Image Preview(二分)

    题目链接 题意:看一个图片需要1单位时间,如果是 w 需要翻转 b 时间,切换到相邻位置(往左或者往右)需要 a 时间,求T时间最多能看几张图片 从第一个开始向右走看若干个图片然后往如果往左走就不会再 ...

  8. Codeforces Round #345 (Div. 1) B. Image Preview

    Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed ...

  9. CodeForces - 651D:Image Preview (双指针&)

    Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed ...

随机推荐

  1. android studio 使用入门 (快捷键等收集)

    1. 解决 android studio cannot resolve symbol 1) file->import proj->create proj from exit proj .. ...

  2. Wordpress 3.5.1的debug流水账

    一个合作网站是基于Wordpress做的, 不能正常使用FeedWordPress这个插件, 对方没有开发者, 只有一个类似于美工和编辑的人, 因为我在本地使用同样的版本(Wordpress 3.5. ...

  3. RDLC系列之七 条码打印

    参考: C# 条码标签打印程序,RDLC报表动态显示多条码标签的方法 http://www.cnblogs.com/vice/p/4105898.html 我做的思路是:不使用数据库存储image的b ...

  4. java读取文件批量插入记录

    只是一个例子,方便以后查阅. import ey.db.oracle.OracleHelper; import ey.db.type.*; import java.io.BufferedReader; ...

  5. cobbler

    原理: http://www.cnblogs.com/mchina/p/centos-pxe-kickstart-auto-install-os.html 一键脚本 http://tshare365. ...

  6. 加密算法使用(一):用CRC32来压缩32uuid字符串

    CRC32相比MD5重复率较高, 不过我们仍然可以使用CRC32然后转长整形的方式将32位的UUID字符串压缩成更短的整形唯一标识. /** * * @Title: getCRC32Value * @ ...

  7. Objective-c基础学习

    核心内容 标识号 OC语言中,对各种变量,方法和类等要素命名时使用的字符序列称为标识符. OC标识符命名规则标识符由字母,下划线“_”,美元符号“$”和数字组成,标识符必须以字母,下划线,美元符号开头 ...

  8. 完美隐藏win7文件和文件夹

    有没有一种方法即使使用隐藏模式也不能查看, 没错可以用上帝模式....... 啥是Win7上帝模式?不知道的看看..... <<<<<<<<<&l ...

  9. POJ2289-Jamie's Contact Groups-二分图多重匹配-ISAP

    注意POJ数组越界可能返回TLE!!! 网络流的maxn大小要注意 其他没什么了 裸二分答案+isap乱搞 不过复杂度没搞懂 V=1e3 E = 1e5 那ISAP的O(V^2E)怎么算都不行啊 /* ...

  10. (旧)子数涵数·Flash——Flash Player的操作命令

    一.什么是Flash Player? Flash Player就是官方指定的一种FLash播发器. 用百度的话来讲,Adobe Flash Player 是一款高级客户端运行时使用的播放器.它短小精悍 ...