codeforces 650B . Image Preview 二分
1 second
256 megabytes
standard input
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.
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 我是先把第一个字符减去, 因为第一个肯定要读, 然后减完的t值, 如果小于0直接输出0就可以了。
然后把从前往后读的和从后往前读所花费的时间都预处理出来。 最优的情况应该是从前往后读几个, 从后往前也读几个, (几个)可以为0。 然后我们枚举从前往后读的情况, 二分查找在这种情况下, 从后往前读最多可以读多少个。 在枚举从后往前的情况, 二分从前往后的情况。 读过的照片不花费时间, 但是移动还是要花费的, 这点要注意..
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
int pre[], suf[], b, a;
int val(char c) {
if(c == 'w')
return b++a;
return +a;
}
int main()
{
int n, t;
char ch;
string s, str;
cin>>n>>a>>b>>t;
cin>>ch;
if(ch == 'w')
t -= b;
t--;
if(t<) {
puts("");
return ;
}
int maxx = ;
cin>>s;
for(int i = ; i<=s.size(); i++) {
if(i == )
pre[i] = ;
else
pre[i] = pre[i-]+val(s[i-]);
}
reverse(s.begin(), s.end());
for(int i = ; i<=s.size(); i++) {
if(i == )
suf[i] = ;
else
suf[i] = suf[i-]+val(s[i-]);
}
for(int i = ; i<=s.size(); i++) {
int l = , r = s.size()-i, tmp = -;
if(pre[i]>t)
break;
while(l<=r) {
int mid = (l+r)>>;
if(pre[i]+suf[mid]+(i)*a<=t) { //i*a的意思是, 从前往后读了几个以后又移动回去的值
tmp = mid;
l = mid+;
} else {
r = mid-;
}
}
if(tmp == -)
maxx = max(maxx, i);
maxx = max(maxx, i+tmp);
}
for(int i = ; i<=s.size(); i++) {
int l = , r = s.size()-i, tmp = -;
if(suf[i]>t)
break;
while(l<=r) {
int mid = (l+r)>>;
if(pre[mid]+suf[i]+(i)*a<=t) {
tmp = mid;
l = mid+;
} else {
r = mid-;
}
}
if(tmp == -)
maxx = max(maxx, i);
maxx = max(maxx, i+tmp);
}
cout<<maxx+<<endl;
return ;
}
codeforces 650B . Image Preview 二分的更多相关文章
- Codeforces 650B Image Preview(尺取法)
题目大概说手机有n张照片.通过左滑或者右滑循环切换照片,滑动需要花费a时间:看一张照片要1时间,而看过的可以马上跳过不用花时间,没看过的不能跳过:有些照片要横着看,要花b时间旋转方向.那么问T时间下最 ...
- Codeforces 650B Image Preview
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- [Codeforces 1199C]MP3(离散化+二分答案)
[Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...
- Codeforces 651D Image Preview【二分+枚举】
题意: 若干张照片,从头开始可以向左右两边读,已经读过的不需要再读,有的照片需要翻转,给定读.滑动和翻转消耗的时间,求在给定时间内最多能读多少页? 分析: 首先明确,只横跨一次,即先一直读一边然后再一 ...
- Codeforces Round #345 D. Image Preview(二分)
题目链接 题意:看一个图片需要1单位时间,如果是 w 需要翻转 b 时间,切换到相邻位置(往左或者往右)需要 a 时间,求T时间最多能看几张图片 从第一个开始向右走看若干个图片然后往如果往左走就不会再 ...
- CodeForces 670D1 暴力或二分
今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1 This problem is given in two versions that diff ...
- codeforces 895B XK Segments 二分 思维
codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...
- Codeforces 626C Block Towers(二分)
C. Block Towers time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...
- codeforces 803D Magazine Ad(二分+贪心)
Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...
随机推荐
- java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/LoopTag
异常原因:缺少jstl.jar包.一般在新建Web项目是jstl一项不要选none就不会出现这个问题.(还有可能就是缺少servlet-api.jar)
- UTL_FILE 的用法
UTL_FILE 的用法 UTL_FILE 是用来进行文件IO处理的专用包,使用这外包的注意事项如下: 1. 生成的文件好象只能放置在DATABASE所在的服务器路径中. 2. 生成的文件如何DO ...
- js对JSON数据排序
一.适用于数字排序和字幕排序json 的排序方法有很多种,这是其中最简单的一种方法. 代码如下: var sortBy = function (filed, rev, primer) { rev ...
- jq方法
DOM属性-获取和设置页面元素的DOM属性 .addClass()..attr()..prop()..hasClass()..html()..removeAttr()..removeClass().. ...
- 安装solaris_11.2与windows双系统(VM10模拟实现)(一)
感慨:这周刚接触solaris,装solaris很蛋疼,一个字:慢! 在上面安装软件包依然很慢,无线网也很不稳定. 在上面搭建环境更蛋疼,一个字:惨! 什么环境之类的废话就不多说了,一般的电脑都可以. ...
- Junit技巧
测试套件: @RunWith(Suite.class) @Suite.SuiteClasses({TaskTest1.class, TaskTest2.class, TaskTest3.class}) ...
- thanks使用注意事项;
router.get('/api/users/search/:key/:page', function(req, res) { if(_.isEmpty(req.params.key)) { res. ...
- favicon
<link rel="icon" href="Images/wangyi.ico" type="text/plain" />
- leetcode_question_115 Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 简单的Session登录
Login前台页面 <form id="form1" action ="" method="post"> <input t ...