Codeforces 650B 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.
-------------------------------------------------------
仔细读题啊!
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的更多相关文章
- codeforces 650B . Image Preview 二分
题目链接 B. Image Preview time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces 650B Image Preview(尺取法)
题目大概说手机有n张照片.通过左滑或者右滑循环切换照片,滑动需要花费a时间:看一张照片要1时间,而看过的可以马上跳过不用花时间,没看过的不能跳过:有些照片要横着看,要花b时间旋转方向.那么问T时间下最 ...
- 汕头市队赛 C KMP codeforces B. Image Preview
汕头市队赛题目传送门 codeforces题目传送门 这道题我的做法是 尝试先往左走然后往右走 或者先往右走然后往左走 然后注意一下枚举顺序就okay啦 #include<cstdio> ...
- Codeforces 651D Image Preview【二分+枚举】
题意: 若干张照片,从头开始可以向左右两边读,已经读过的不需要再读,有的照片需要翻转,给定读.滑动和翻转消耗的时间,求在给定时间内最多能读多少页? 分析: 首先明确,只横跨一次,即先一直读一边然后再一 ...
- Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分
D. Image Preview 题目连接: http://www.codeforces.com/contest/651/problem/D Description Vasya's telephone ...
- codeforces 650D D. Image Preview (暴力+二分+dp)
题目链接: http://codeforces.com/contest/651/problem/D D. Image Preview time limit per test 1 second memo ...
- 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 - 651D:Image Preview (双指针&)
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed ...
随机推荐
- javascript替换手机号中间4位
// 匹配手机号首尾,以类似“123****8901”的形式输出 '12345678901'.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'); 此段正则匹配字符串 ...
- [解决方案]在Sql Server 2008/2005 数据库还原出现 3154错误
在Sql Server 2008/2005 数据库还原出现 3154错误 解决方法1:不要在数据库名字上点右键选择还原,而要是在根目录“数据库”三个字上点右键选择还原,然后再选择数据库,问题便可以解决 ...
- 【转】【WPF】WPF 登录窗口关闭时打开主窗口
在WPF中设计登录窗口关闭时打开主窗口,自动生成的App.xaml不能满足要求, 1.把App.xaml的属性窗口中的生成操作设定为 无 2.添加Program类 static class Progr ...
- 入门Linux
45分钟带你入门Linux(附:笔者在工作室开讨论班录制的视频讲解) 第一部分 熟悉Linux基本操作 一.初识Linux 1.Linux特点 ◊ 开放性 ◊ 多用户 ◊ 多任务 ◊ ...
- Viewpager模仿微信主布局的三种方式 ViewPager,Fragment,ViewPager+FragmentPagerAdapter
效果大概就是这样 很简单 : 1 创建 top 和bottom 2主界面布局 添加top 和bottom 中间添加一个ViewPage 3 给ViewPager 和 底部View设置点击事件 源码下载 ...
- IOS开发之—— model最原始的封装,MJExtension加入工程(后续model都继承于它)
DMBasicDataModel.h #import <Foundation/Foundation.h> @interface DMBasicDataModel : NSObject - ...
- 常见面试第二题之什么是Context
今天的面试题,也就是我们常见面试题系列的第二题,我们来讲一讲android中的context.我相信大家android开发者一定对于这个context非常熟悉,肯定都有使用过,肯定没有没使用过的.但是 ...
- [BZOJ 1052][HAOI2007]覆盖问题(二分答案)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1052 分析: 挺有想法的一道题,先二分答案ans,主要是判断的问题. 首先可以弄出把所 ...
- http加速软件使用说明
HTTP加速软件使用说明 http加速软件使用于卫星链路,在卫星链路时延高的情况下提高http的传输速率 1.1 软件包依赖 (1)squid-3.4.5.tar.gz (2)trafficserve ...
- Linux下搭建nginx php环境
下载安装所需包 openssl-1.0.1i.tar.gz zlib-1.2.8.tar.gz pcre-8.35.tar.gz nginx-1.7.4.tar.gz 以上为nginx依赖文件 lib ...