博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 650B Image Preview
阅读量:5050 次
发布时间:2019-06-12

本文共 4256 字,大约阅读时间需要 14 分钟。

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

 

 

 

 

 

 

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

你可能感兴趣的文章
在ns2.35中添加myevalvid框架
查看>>
【贪心+DFS】D. Field expansion
查看>>
为什么要使用href=”javascript:void(0);”
查看>>
二进制文件的查看和编辑
查看>>
C# Async与Await的使用
查看>>
Mysql性能调优
查看>>
iOS基础-UIKit框架-多控制器管理-实例:qq界面框架
查看>>
javascript学习---BOM
查看>>
IOS-每个程序员的编程之路上都应该看这11本书
查看>>
自定义tabbar(纯代码)
查看>>
extjs fieldset 和 radio
查看>>
小程序底部导航栏
查看>>
Codeforces Gym101505G:Orchard Division(扫描线+线段树第k大)
查看>>
ibatis学习笔记
查看>>
18-ES6(1)
查看>>
poj1611 简单并查集
查看>>
tensorflow实现迁移学习
查看>>
Ubuntu 14.04下安装CUDA8.0
查看>>
跨平台开发 -- C# 使用 C/C++ 生成的动态链接库
查看>>
关于Redis处理高并发
查看>>