1. 程式人生 > >ACM/ICPC 2018亞洲區預選賽北京賽站網路賽-題目4 : 80 Days (模擬+簡單數學)

ACM/ICPC 2018亞洲區預選賽北京賽站網路賽-題目4 : 80 Days (模擬+簡單數學)

題目4 : 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

題意:

有n個城市,它們按照順序從1到n編號。當你到達i城市時,你將獲得 A[i] 美元(ai甚至可以是負數),如果你想去下一個城市,你應該支付 B[i] 美元。在一開始你有C美元。這個遊戲的目標是選擇一個城市作為起點,然後沿著圓圈去一次訪問所有城市,最後回到起點。 在旅行期間,您所擁有的錢必須不低於零。這裡有一個問題:完成旅行,您將選擇哪個城市作為開始城市?如果有多個答案,請輸出編號最小的城市

思路:

先計算出每一個城市獲得的利潤(可為負)w[i] 

計算整個過程的所需經費sum+初始金額c sum+c>=0 否則 printf(-1);

再通過單純的模擬我們可以發現從 a[i] 城市開始遊歷過程中經費的最小值minn1 與從 a[i+1] 城市開始的minn2 相差一個a[i];

當我們計算a[i+1] 時直接用 minn1-a[i] 判斷 minn2>=0 則   printf(i+1);

AC程式碼:

#include<stdio.h>
using namespace std;
const int maxn = 2e6 + 6;
long long  a[maxn];//經費
long long  b[maxn];//花費
long long  w[maxn];//利潤

int n;
long long c;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        long long sum = 0;
        long long sum2 = 0;
        long long minn = 0x3f3f3f3f3f3f;
        long long minn2 = 0x3f3f3f3f3f3f3f;
        int id = 0;
        int id2 = 0;
        scanf("%d%lld",&n,&c);
        for(int i = 1;i <= n;i++)
            scanf("%lld",&a[i]);

        for(int i = 1;i <= n;i++)
        {
            scanf("%lld",&b[i]);
            w[i] = a[i] - b[i];//利潤
            sum += w[i];//累加
            if(minn > sum){
                minn = sum;//累加過程中的最小值 相當minn1
                id = i;
            }
        }
        if( c + sum < 0){printf("-1\n");continue;}//不可能
        if(minn + c >= 0){printf("1\n");continue;}//從1開始遊歷


        for(int i = 1;i <= n;i++)
        {
            minn -= w[i];//從i+1開始遊歷 相當minn2
            if(minn + c >= 0){//是否可以成功
                    id = i + 1;
            break;//跳出
            }
        }

        printf("%d\n",id);
    }
}