Codeforces #345 div1 B. Image Preview 二分

题目

题目链接: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值