ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(尺取)题解

题意:n个城市,初始能量c,进入i城市获得a[i]能量,可能负数,去i+1个城市失去b[i]能量,问你能不能完整走一圈。

思路:也就是走的路上能量不能小于0,尺取维护l,r指针,l代表出发点,r代表当前走到的点,维护一个sum。和工程的题一模一样啊。

代码:

#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = 1e6 + 10;
const int seed = 131;
const ll MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
ll a[maxn << 1], b[maxn << 1];
int main(){
    int T;
    scanf("%d", &T);
    ll n, c;
    while(T--){
        scanf("%lld%lld", &n, &c);
        for(int i = 1; i <= n; i++){
            scanf("%lld", &a[i]);
            a[i + n] = a[i];
        }
        for(int i = 1; i <= n; i++){
            scanf("%lld", &b[i]);
            b[i + n] = b[i];
        }

        int l = 1, r = 1, ans = -1;
        ll sum = c;
        while(l <= n){
            sum += a[r];
            while(sum < 0 && l < r){
                sum = sum - a[l] + b[l];
                l++;
            }
            sum -= b[r];
            r++;
            while(sum < 0 && l < r){
                sum = sum - a[l] + b[l];
                l++;
            }
            if(r == l + n){
                ans = l;
                break;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
/*
2
3 0
3 4 5
5 4 3
3 100
-3 -4 -5
30 40 50
*/

 

posted @ 2018-09-23 22:16  KirinSB  阅读(189)  评论(0编辑  收藏  举报