题目
题目链接:http://codeforces.com/contest/650/problem/B
题目来源:http://codeforces.com/contest/650
简要题意:看图,旋转图和移动有代价,碰到图必须看,求有限时间内最多看多少图。
题解
可能的情况为从一边走,一边走到某点返回走另一边。
如果多次往返,必然能在减少时间的同时看到同样多的图片。
于是可以做前缀和然后进行二分。
需要注意的是如果去二分另一边的话由于起始的位置不用再看,需要返还这部分代价。
然后做的时候要枚举两边,因为两边走的时候要去往返更短的一边。
如果手写二分的话则可以避免考虑上面的部分东西。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
const int N = 5e5+5;
char fw[N];
int sumfw[N], sumbk[N];
int n, a, b, t;
int cal(char ch) {
return ch == 'h' ? 1 : b+1;
}
int solve(int n, int sumfw[N], int sumbk[N]) {
int ans = 0;
for (int i = 0; i < n && sumfw[i] <= t; i++) {
int rem = t - sumfw[i] - a * i + sumfw[0];
int pos = upper_bound(sumbk, sumbk + n, rem) - sumbk;
ans = max(ans, pos + i);
}
return ans;
}
int main() {
scanf("%d%d%d%d", &n, &a, &b, &t);
scanf("%s", fw);
sumfw[0] = sumbk[0] = cal(fw[0]);
for (int i = 1; i < n; i++) {
sumfw[i] = sumfw[i-1] + cal(fw[i]) + a;
sumbk[i] = sumbk[i-1] + cal(fw[n-i]) + a;
}
int ans = max(solve(n, sumfw, sumbk), solve(n, sumbk, sumfw));
printf("%d\n", min(ans, n));
return 0;
}