1. 程式人生 > >hihoCoder #1831 : 80 Days【思維 巧用佇列】

hihoCoder #1831 : 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

【題意】

有n個城市圍成一圈 編號為1-n

到達第i個城市需要花費ai金幣 同時獲得bi金幣

起初有x金幣

可以任意選擇一個城市作為起點 

問能否從一個城市出發環遊一圈?

【起點等於經過了兩次】

【小結】

這是一道思維題 因為要求優先輸出編號小的城市 我們可以從1開始依次列舉各個城市

如果可以到達這個城市就入佇列  

如果當列舉到某一城市發現它不符合入佇列的條件就說明從當前佇列頭的城市不能走到這個城市

於是讓隊頭元素出佇列 

如果發現仍不能入佇列 就一直出佇列

如果最終佇列的元素為n

輸出隊頭元素即可

【程式碼】

#include<bits/stdc++.h>
#define MAX 2000010
using namespace std;
typedef long long ll;

ll a[MAX],b[MAX];
queue<int>q;

int main()
{
    int t,n,i,j;
    ll x;
    scanf("%d",&t);
    while(t--){
        scanf("%d%lld",&n,&x);
        for(i=1;i<=n;i++){
            scanf("%lld",&a[i]);
        }
        for(i=n+1;i<=n+n;i++){
            a[i]=a[i-n];
        }
        for(i=1;i<=n;i++){
            scanf("%lld",&b[i]);
        }
        for(i=n+1;i<=n+n;i++){
            b[i]=b[i-n];
        }
        while(q.size()){
            q.pop();
        }
        int f=0;
        for(i=1;i<=n+n;i++){
            if(x+a[i]-b[i]>=0){
                x+=a[i]-b[i];
                q.push(i);
                if(q.size()>=n){
                    printf("%d\n",q.front());
                    f=1;
                    break;
                }
            }
            else{
                while(x+a[i]-b[i]<0&&q.size()){
                    x-=a[q.front()]-b[q.front()];
                    q.pop();
                }
                if(x+a[i]-b[i]>=0){
                    x+=a[i]-b[i];
                    q.push(i);
                    if(q.size()>=n){
                        printf("%d\n",q.front());
                        f=1;
                        break;
                    }
                }
            }
        }
        if(f==0) printf("-1\n");
    }
    return 0;
}