ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D.80 Days(水)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yz467796454/article/details/82814719

题目链接:https://hihocoder.com/problemset/problem/1831?sid=1389856

时间限制: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个城市,初始有c的钱,每到i城市,会获得a[i]的金钱,失去b[i]的金钱,问能否走遍n个城市,过程中金钱不为负数,输出起始城市,如果答案有多个,输出最小的数字

思路:l表示起始城市,r表示目前走到的城市,sum记录金钱,当走到某一个城市,金钱为负数,则起始城市要向前,直至n个城市作为起始城市都不符合要求,则跳出循环,或者走遍n个城市,跳出循环。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;

ll a[1000005],b[1000005];

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		ll c;
		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]);
			a[i]-=b[i];
		}
		ll sum=c;
		int num=0;
		int l=1,r=1;
		while(1){
			if(r>n)sum+=a[r-n];
			else sum+=a[r];
			num++;
			while(sum<0){
				if(l>n)sum-=a[l-n];
				else sum-=a[l];
				num--;
				l++;
				if(l>r)break;
			}
			r++;
			if(num==n)break;
			if(l>n)break;
		}
		if(l>n)printf("-1\n");
		else printf("%d\n",l);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/yz467796454/article/details/82814719
今日推荐