题目链接

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. node.js(五)字符串转换

    1.stringify函数的基本用法 stringify函数的作用就是序列化对象,也就是说将对象类型转换成一个字符串类型(默认的分割符("&")和分配符("=&q ...

  2. bootstrap注意事项(七)图片

    在本章中,我们将学习 Bootstrap 对图片的支持.Bootstrap 提供了三个可对图片应用简单样式的 class: .img-rounded:添加 border-radius:6px 来获得图 ...

  3. asp.net 超链接 下载TEXT文件,而不是直接在IE中打开

    问题描述:后台生成了文本文件,用超链接提供给用户下载.点击超链接,下载Excel文件没问题,但文本文件会直接被打开,而不是弹出下载窗口. 解决方法:把HyperLink改为LinkButton,在Cl ...

  4. java.lang.ArithmeticException: / by zero

  5. 自适应Cell

        // //  ViewController.m //  04-自适应cell // //  Created by 

  6. C#:占位符的例子

    在c#中有两种方式可以输出多个字符. static void Main() { string c=Console.ReadLine(); string d=Console.ReadLine(); Co ...

  7. 实现ModelDriver接口的功能

    ModelDriver接口 来自com.opensymphony.xwork2.ModelDriven.是xwork-2.1.2-750.jar包的东西. 下面是源码: package com.ope ...

  8. PHP获取中文汉字首字母方法

    function getFirstLetter($str){ $fchar = ord($str{0}); if($fchar >= ord("A") and $fchar ...

  9. Java中的String,StringBuffer,StringBuilder详解与区别

    1.String Java中string类是不可变的,其中在声明的源代码中用的final,所以只能声明一次.所以每次在明面上的改变其实是重新生成一个String对象,指针指向新的String对象.同时 ...

  10. caffe 配置 札记

    cudnn的配置 1. 安装前请去先官网下载cuDNN (cudnn-7.0-linux-x64-v3),建议安装v3,v4有些问题. 将cudnn-7.0-linux-x64-v3解压后会有两个文件 ...