1. 程式人生 > >80 Days (尺取法)

80 Days (尺取法)

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

題意:t組資料,每組資料中有n個城市組成一個環,剛開始有c元。緊跟的一行代表可以在每個城市掙得錢數a[i](可為負數),之後一行代表在每個城市花費的錢數b[i]。求可以從哪個城市開始旅行,中途自身的錢數不能小於0,且能回到起點。不能的話輸出-1.

思路:先計算出在每個城市的收入c[i](c[i]=a[i]-b[i]).然後用尺取法開始找點: 先找一個左指標k 指向第一個數,並假設它為起點,然後右指標依次讓後走計算當前和sum,當sum<0時,代表此時在中途錢數小於0了,既起點不滿足方案,因此我們把左指標向左依次移動,知道sum>=0時停止,然後繼續走右指標。因為是一個環,那麼當右指標走到終點時,需要重新返回到起點,繼續判斷。那右指標再次和左指標重合的時候,說明以左指標為起點的這條路徑可行,輸出左指標的索引。  當左指標已經到達n,且不滿足條件的時候,說明沒有滿足條件的路徑。

程式碼如下:

#include<cstdio>
#include<cstring>
#define LL long long
#define N 1000010
LL a[N],b[N],c[N];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        for(int i=1; i<=n; i++)
        {
            scanf("%lld",&b[i]);
            c[i]=a[i]-b[i];  //計算此點的收入
        }
        LL sum=m;//初始化金錢數
        int k=0;//k為左指標,並假設k為起點
        //只有n個城市,起點最多隻能從第n個城市開始
        for(int i=1; i<=n; i++)  //i為右指標
        {
            sum+=c[i]; //計算和
            if(sum<0) //當和小於0時,說明此時的起點不能滿足條件,需要往右移
            {
                while(sum<0&&k<=n)  //找到當前滿足條件的起點
                    sum-=c[++k];
            }
        }
        for(int i=1; i<=k; i++)  //因為是個環,終點肯定在左指標k處,k為動態的,最大為n
        {
            sum+=c[i];
            if(sum<0)
            {
                while(sum<0&&k<=n)  //起點右移
                {
                    sum-=c[++k];
                    if(k>n)
                        break;
                }
            }
            if(k>n)
                break;
        }
        if(k<=n)  //找到了一條路
            printf("%d\n",k+1);
        if(k>n)
            printf("-1\n");
    }
}