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 ...
随机推荐
- android入门——数据存储
首先是SharedPreferences 用户偏好 package com.example.wkp.aboutdata; import android.content.Intent; import a ...
- /-- Encapsulated exception ------------\ java.lang.NoSuchMethodException: com.sjzdaj.po.Class.setN_id([Ljava.lang.String;)
转自:http://ruinxdgzy.iteye.com/blog/805138
- Constructor JavaScript构造器模式。
构造器模式 : Constructor模式中, 通过在构造器前面加 new 关键字, 告诉JavaScript 像使用构造器一样实例化一个新对象,并且对象成员由该函数定义. 构造器内, 使用this ...
- js设置元素readonly属性注意事项
注意大小写,应该为:obj.readOnly = true;
- windbg命令学习1
一.windbg 常用知识: 1. Windbg中的调试命令,分为三种:基本命令,元命令和扩展命令.基本命令和元命令是调试器自带的,元命令总是以“.”开头,而扩展命令是外部加入的,总是以感叹号“!”开 ...
- 采购件不允许挂BOM
应用 Oracle Bill Of Materiel 层 Level Function 函数名 Funcgtion Name BOM_BOMFDBOM 表单名 Form Name BOMFDBOM ...
- C#版-百度网盘API的实现(一)
在这篇文章中,楼主将会给大家介绍一下,通过C# winform程序在后台模拟用户登陆百度网盘的基本思路 首先了解下模拟登陆的流程,如下: 一,访问http://www.baidu.com网站,获取BA ...
- IZ65534: 'JAVA.LANG.CLASSFORMATERROR' ERROR FOR A VALID IDENTIFIER
PAR status Closed as program error. Error description Error Message: The java class could not be loa ...
- Error copying image in the datastore: Not allowed to copy image file
opennebula error copying image in the datastore not allowed to copy image file Error copying image i ...
- Android UI学习组件概述
Android的UI组件繁多,如果学习的时候不能自己总结和分类而是学一个记一个不去思考和学习他们内在的联系那真的是只有做Farmer的命了.为了向注定成为Farmer的命运抗争,在学习Android的 ...