ACM/ICPC2018北京网络赛-Hihocoder1831 : 80 Days(思维)

ACM/ICPC2018北京网络赛-Hihocoder1831 : 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个城市,给c元。按顺序访问,当你抵达一个城市时,给ai元;离开该城市时,付bi元。问存不存在巡游序列,使得全程钱不小于0。给出字典序最小的解,无解输出-1

思路

维护一个抵达i需要的钱数。然后保存从n-1,到i的最多需要多少钱。保存从1到i的最左需要多少钱。然后遍历1-n,找出第一个满足条件的解,否则输出‘-1’

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
int T;

const int MAXN = 1e6+10;
int a[MAXN],b[MAXN],w[MAXN],mi[MAXN],mii[MAXN];

int main() {
    scanf("%d",&T);
    int n,c; 
    while(T--){
        bool flag = false;
        scanf("%d %d",&n,&c);
        for(int i=1;i<=n;i++)   scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)   scanf("%d",&b[i]);
        for(int i=1;i<=n;i++)   w[i]=w[i-1]+a[i]-b[i];
        mi[n] = w[n];
        mii[1] = w[1];
        for(int i=n-1;i>=1;i--) mi[i] = min(mi[i+1],w[i]);
        for(int i=2;i<=n;i++) mii[i] = min(mii[i-1],w[i]);
        for(int i=1;i<=n;i++){
            int val=mi[i]- w[i-1];
            if (c+val<0) continue;
            val=w[n]-w[i-1]+c;
            if (val+mii[i-1]<0) continue;
            printf("%d\n",i);
            flag=true;
            break;
        }
        if(flag == false)   printf("-1\n");
    }
}

猜你喜欢

转载自www.cnblogs.com/caomingpei/p/9691253.html