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 ...
随机推荐
- poj1936--暴力解法
求s1是否是s2的子串. 分析: 例如sequence 和 subsequence . 从头开始比较,s1[0]与s2[0]相同,那么它们下标都可+1,接着比较s1[1]和s2[1],不相同:这时应该 ...
- floyd学习中
#include<iostream> #include<math.h> #include<stdio.h> using namespace std; //long ...
- 关于jQuery中toggle()函数的使用
今天遇到一个有趣的例子,将它记录下来. 一个一级菜单,里边有一个二级菜单,二级菜单是通过锚点来链接页面元素的.想要实现的效果是当点击锚点时,页面链接到相应锚点,同时二级菜单隐藏,再点击一级菜单时,继续 ...
- navigator.geolocation例子
navigator.geolocation例子 <html> <head> <script type="text/javascript"> fu ...
- 方形图片转动并转换成圆形CSS特效
<style> img { transition:all 0.8s ease 0s;} img:hover { border-radius:50%; transform:rotate(72 ...
- 服务端NETTY 客户端非NETTY处理粘包和拆包的问题
之前为了调式和方便一直没有处理粘包的问题,今天专门花了时间来搞NETTY的粘包处理,要知道在高并发下,不处理粘包是不可能的,数据流的混乱会造成业务的崩溃什么的我就不说了.所以这个问题 在我心里一直是个 ...
- javascript学习笔记(2)
<html> <head><title>Throwing die</title><script> var canv_width = ...
- 查看oracle数据库的大小和空间使用情况
查看oracle数据库的大小和空间使用情况 (2012-06-19 14:44:30) 转载▼ 标签: 杂谈 分类: oracle 1.查看表空间的使用状况 SELECT upper(f.tables ...
- DEM 数据下载
https://centaurus.caf.dlr.de:8443/short_guide/index.html https://centaurus.caf.dlr.de:8443/eoweb-ng/ ...
- 无线功率 mW 和 dBm 的换算
无线电发射机输出的射频信号,通过馈线(电缆)输送到天线,由天线以电磁波形式辐射出去.电磁波到达接收地点后,由天线接收下来(仅仅接收很小很小一部分功率),并通过馈线送到无线电接收机.因此在无线网络的工程 ...