#345 div2 D. 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 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.
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 the only integer, the maximum number of photos Vasya is able to watch during those T seconds.
4 2 3 10
wwhw
2
5 2 4 13
hhwhh
4
5 2 4 1000
hhwhh
5
3 1 100 10
whw
0
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.
思路:
做过最恶心的题之一
首先从想法上来说,只可能有一次折返,所以就分别向右和向左枚举折返的点,然后二分查找反方向能到达的最远点
可以将字符串复制一遍放到n+1->2*n的位置上,但这样做在涉及向左向右移动的时候下标的更改不是一般的麻烦和容易出错
关于二分,left和right的赋值是一个比较新颖的地方,值得注意一下
剩下的全是细节,各种该死的细节
比如while(left<=right)区域内的tmp变量,它的初始值的设置是个非常令人头疼的事情,你要很充分的理解这个变量的实质究竟是什么才能搞对
#include <iostream>
#include <cstring>
#define maxn 500007
using namespace std; char s[maxn];
int l2r[maxn];
int r2l[maxn];//能用一个数组表示的不要轻易的换成两个连续的数组
//既浪费了空间,还由于要考虑index的+-size问题而容易引起错误 int main()
{
int n,a,b,T;
while(cin>>n>>a>>b>>T)
{
int l_most = ;
int r_most = n-;
cin>>s;
l2r[] = s[]=='w'?+b:;
for(int i = ;i < n;i++)
l2r[i] = l2r[i-]++a+b*(s[i]=='w');
r2l[n-] = s[n-]=='w'?l2r[]+a++b:l2r[]+a+;
for(int i = n-;i > ;i--)
r2l[i] = r2l[i+]++a+b*(s[i]=='w'); int ans = ;
//left
for(int i = n-;i> && r2l[i]<=T;i--)
{
if(r2l[i]+a*(n-i)<T) {
int tmp = ;
int left = ;
int right = i-;
int mid;
while(left <= right) {
mid = (left+right)>>;
if(r2l[i]+a*(n-i)+l2r[mid]-l2r[] <= T){
tmp = mid;
left = mid+;
}
else
right = mid-;
}
ans = max(ans,n-i+tmp+);
}
else
ans = max(ans,n-i+);
}
//right
for(int i = ;i<=n- && l2r[i]<=T;i++)
{
if(l2r[i]+a*i < T) {
int tmp = n;
int left = i+;
int right = n-;
int mid;
while(left <= right) {
mid = (left+right)>>;
if(l2r[i]+a*i+r2l[mid]-l2r[] <= T) {
tmp = mid;
right = mid-;
}
else
left = mid+;
}
ans = max(ans,i++n-tmp);
}
else
ans = max(ans,i+);
}
cout<<ans<<endl;
}
return ;
}
#345 div2 D. Image Preview的更多相关文章
- CF#345 div2 A\B\C题
A题: 贪心水题,注意1,1这组数据,坑了不少人 #include <iostream> #include <cstring> using namespace std; int ...
- Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分
D. Image Preview 题目连接: http://www.codeforces.com/contest/651/problem/D Description Vasya's telephone ...
- Codeforces Round #345 D. Image Preview(二分)
题目链接 题意:看一个图片需要1单位时间,如果是 w 需要翻转 b 时间,切换到相邻位置(往左或者往右)需要 a 时间,求T时间最多能看几张图片 从第一个开始向右走看若干个图片然后往如果往左走就不会再 ...
- 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 ...
- Codeforces #345 Div.1
Codeforces #345 Div.1 打CF有助于提高做题的正确率. Watchmen 题目描述:求欧拉距离等于曼哈顿距离的点对个数. solution 签到题,其实就是求有多少对点在同一行或同 ...
- 解读发布:.NET Core RC2 and .NET Core SDK Preview 1
先看一下 .NET Core(包含 ASP.NET Core)的路线图: Beta6: 2015年7月27日 Beta7: 2015年9月2日 Beta8: 2015年10月15日 RC1: 2015 ...
- VS15 preview 5打开文件夹自动生成slnx.VC.db SQLite库疑惑?求解答
用VS15 preview 5打开文件夹(详情查看博客http://www.cnblogs.com/zsy/p/5962242.html中配置),文件夹下多一个slnx.VC.db文件,如下图: 本文 ...
- .NET跨平台之旅:将示例站点升级至 .NET Core 1.1 Preview 1
今天微软发布了 .NET Core 1.1 Preview 1(详见 Announcing .NET Core 1.1 Preview 1 ),紧跟 .NET Core 前进的步伐,我们将示例站点 h ...
- Android Starting Window(Preview Window)
当打开一个Activity时,如果这个Activity所属的应用还没有在运行,系统会为这个Activity所属的应用创建一个进程,但进程的创建与初始化都需要时间,在这个动作完成之前系统要做什么呢?如果 ...
随机推荐
- 项目报错,tomcat中引起
1.项目报错,但发现工程并没有错.此刻错误应该定位如下,即工程里面引用的jar可能有错,可能是路劲变了....
- Pomelo实现最简单的通信-egret。
昨天因为需要开始学习Pomelo 做H5游戏的服务端. 因为个人学习习惯,我从来不适合去跟着文档看.一般我直接是看下大概的API,但是Pomelo的API全部都是英文的. 昨天我就告诉自己用一下午时间 ...
- angular template浅析
在我们浏览的页面中有大的网站,也有中小型网站,类型不同其中的页面也就不同,但是纵观大部分的网页是否有什么相同的地方呢?如果浏览的是一般的门户网站或者是什么小型的页面的话这种感觉就不是很明显,但是如果关 ...
- hdoj 1176(可转化为数塔)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissio ...
- MATLAB中mexFunction函数的接口规范
MEX文件的调用极为方便,其调用方式与MATALAB的内建函数完全相同,只需要在命令窗口内输入对应的文件名称即可. C语言MEX程序代码文件有计算子例程(Computational routine)和 ...
- SGU 196.Matrix Multiplication
时间限制:0.25s 空间限制:4M Solution n=10000,m=100000,显然不能用矩阵乘法乘出来. S= ATA 对于矩阵S的一行,所有在A矩阵中1位置的元素都相等,并且都等于这一行 ...
- 织梦dedecms后台发布文章不自动更新首页与栏目列表页
dedecms发文章不自动更新首页也列表页解决办法如下: 登陆dedecms后台,找到“系统”“系统基本参数”“性能选项”,把“arclist标签调用缓存”设置成0,然后把“发布文章后马上更新网站主页 ...
- width:100%缩小窗口时背景图片出现空白bug
页面容器(#wrap)与页面头部(#header )为100%宽度.而内容的容器(#page)为固定宽度960px.浏览窗口缩小而小于内容层宽度时会产生宽度理解上的差异.如下图所示窗口宽度大于内容层宽 ...
- 利用Python读取Matlab的Mat文件内容
手头有别人写的Matlab程序,其中用到了Mat文件.现在不想安装Matlab,却又想读取Mat文件内容,该怎么办呢? 感谢scipy!!! import scipy.io data = scipy. ...
- 常用Firefox扩展
最近思维混乱,无心做事,故整理下东西.(PS:有些是firefox自带的.) 1.标签页管理器 2.1.41 用途:在新标签页打开书签.历史.地址.搜索. 主页:http://www.firefox. ...