1. 程式人生 > >無法拯救我的菜-----北京網路賽 #1831 : 80 Days

無法拯救我的菜-----北京網路賽 #1831 : 80 Days

時間限制:1000ms 單點時限:1000ms 記憶體限制:256MB 描述 80 Days is an interesting game based on Jules Verne’s science fiction “Around the World in Eighty Days”. In this game, you have to manage the limited money and time.

Now we simplified the game as below:

There are n cities on a circle around the world which are numbered from 1 to n by their order on the circle. When you reach the city i at the first time, you will get ai dollars (ai can even be negative), and if you want to go to the next city on the circle, you should pay bi dollars. At the beginning you have c dollars.

The goal of this game is to choose a city as start point, then go along the circle and visit all the city once, and finally return to the start point. During the trip, the money you have must be no less than zero.

Here comes a question: to complete the trip, which city will you choose to be the start city?

If there are multiple answers, please output the one with the smallest number.

輸入 The first line of the input is an integer T (T ≤ 100), the number of test cases.

For each test case, the first line contains two integers n and c (1 ≤ n ≤ 106, 0 ≤ c ≤ 109). The second line contains n integers a1, …, an (-109 ≤ ai ≤ 109), and the third line contains n integers b1, …, bn (0 ≤ bi ≤ 109).

It’s guaranteed that the sum of n of all test cases is less than 106

輸出 For each test case, output the start city you should choose.

提示 For test case 1, both city 2 and 3 could be chosen as start point, 2 has smaller number. But if you start at city 1, you can’t go anywhere.

For test case 2, start from which city seems doesn’t matter, you just don’t have enough money to complete a trip.

樣例輸入 2 3 0 3 4 5 5 4 3 3 100 -3 -4 -5 30 40 50 樣例輸出 2 -1

尺取法,因為是迴圈,所以將a[i] - b[i]陣列複製一份(賽場上想錯了,當遇到小於0的情況,縮尾直接縮到頭上了,沒有一步一步縮,雖然大致來看是因為遇到一個更大的負數才變為負,不知道卡啥資料)

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 2 * 1e6 + 5;

LL a[N];
LL b[N];
LL ans[N];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,c;
        scanf("%d %d",&n,&c);
        for(int i = 1;i <= n;++i)
            scanf("%lld",&a[i]);
        for(int i = 1;i <= n;++i)
            scanf("%lld",&b[i]);
        for(int i = 1;i <= n;++i)
            ans[i] = ans[n + i] = a[i] - b[i];
        int l = 1,r = 1;
        LL res = 0;
        while(l <= n && (r - l + 1) <= n)
        {
            res += ans[r++];
            while(res  + c < 0) res -= ans[l++];
        }
        if(l <= n){
            printf("%d\n",l);
        }else{
            printf("-1\n");
        }
    }
    return 0;
}

沒有複製陣列,直接就 j =(j + 1) % n寫的

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 5;

LL a[N];
LL b[N];
int n,c;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&c);
        for(int i = 0;i < n;++i)
            scanf("%lld",&a[i]);
        for(int i = 0;i < n;++i)
            scanf("%lld",&b[i]);
        if(n == 1){
            if(c + a[0] >= 0){
                printf("1\n");
            }else{
                printf("-1\n");
            }
            continue;
        }
        int i = 0,j = 0;
        LL ans = 0;
        int res = 0;
        int p = 0;
        bool flag = false;
        while(i < n && res < n)
        {
            ans += (a[j] - b[j]);
            j = (j + 1) % n;
            res++;
            while(ans + c < 0){
                ans -= (a[i] - b[i]);
                i++;
                res--;
            }
            //cout << i << " " << j << " " << ans << " " << res << endl;
        }
        //cout << p << endl;
        if(res != n){
            printf("-1\n");
        }else{
            printf("%d\n",i + 1);
        }
    }
    return 0;
}