题目链接

B. Image Preview
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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

Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds.

Examples
input
4 2 3 10
wwhw
output
2
input
5 2 4 13
hhwhh
output
4
input
5 2 4 1000
hhwhh
output
5
input
3 1 100 10
whw
output
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 二分的更多相关文章

  1. Codeforces 650B Image Preview(尺取法)

    题目大概说手机有n张照片.通过左滑或者右滑循环切换照片,滑动需要花费a时间:看一张照片要1时间,而看过的可以马上跳过不用花时间,没看过的不能跳过:有些照片要横着看,要花b时间旋转方向.那么问T时间下最 ...

  2. Codeforces 650B Image Preview

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  3. [Codeforces 1199C]MP3(离散化+二分答案)

    [Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...

  4. Codeforces 651D Image Preview【二分+枚举】

    题意: 若干张照片,从头开始可以向左右两边读,已经读过的不需要再读,有的照片需要翻转,给定读.滑动和翻转消耗的时间,求在给定时间内最多能读多少页? 分析: 首先明确,只横跨一次,即先一直读一边然后再一 ...

  5. Codeforces Round #345 D. Image Preview(二分)

    题目链接 题意:看一个图片需要1单位时间,如果是 w 需要翻转 b 时间,切换到相邻位置(往左或者往右)需要 a 时间,求T时间最多能看几张图片 从第一个开始向右走看若干个图片然后往如果往左走就不会再 ...

  6. CodeForces 670D1 暴力或二分

    今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1   This problem is given in two versions that diff ...

  7. codeforces 895B XK Segments 二分 思维

    codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...

  8. Codeforces 626C Block Towers(二分)

    C. Block Towers time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...

  9. codeforces 803D Magazine Ad(二分+贪心)

    Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...

随机推荐

  1. 检索算法 -- 数据结构与算法的javascript描述 第13章

    检索算法-如何在列表中查找特定的值. 顺序查找 从列表的第一个元素开始对列表元素逐个进行判断,直到找到了想要的结果,它属于暴力查找技巧的一种,在执行查找时可能会访问到数据结构里的所有元素. 代码: / ...

  2. mvc中HttpPost理解

    public ActionResult Delete(int id) { Book book = db.Book.Find(id); if (book == null) { //重定向到行动(跳转到i ...

  3. ORACLE函数TO_CHAR以及数字转换格式[Z]

    本来这是很简单的函数,但在屡次忘记格式之后,决定还是翻译一遍以铭记在心.      参考<<Oracle Database SQL Reference>>.      关于nl ...

  4. Lua与C++的交互

    这篇文章说的挺详细的:Lua与C++的交互

  5. Could not lock surface java.lang.IllegalArgumentException

    08-07 14:46:33.795: E/Surface(4927): dequeueBuffer failed (Invalid argument) 08-07 14:46:33.800: E/V ...

  6. php传参方式1--ajax

    AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 JavaScript和 ...

  7. sql server 2012 镜像和出现的问题

    镜像安装的环境: 主机:win server 2012 , sql server 2012 ,ip:192.168.1.189  PC账户:administrator 备机:win server 20 ...

  8. 获取EIP(汇编语言直接给Delphi变量赋值)

    var EIP: Cardinal; procedure GetEIP(); stdcall; asm pop eax; mov EIP,eax; push eax; end; procedure T ...

  9. 百度云是用SOUI开发的产品

    http://www.cnblogs.com/setoutsoft/p/4155997.html

  10. (8) Xamarin使用Jar檔

    原文 Xamarin使用Jar檔 这个范例是如何在Xamarin.Android中去使用一个我们自行在开发的JAR档案. 主要会执行的步骤如下 在Xamarin建立一个Android Java Bin ...