ACM/ICPC 2018亞洲區預選賽北京賽站網絡賽 題目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

隊友的簡潔代碼:

#include <map>
#include <cstring>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cctype>
#include <stack>
#include <list>
#include <queue>
#include <cmath>
#include <vector>
typedef long long ll;
using namespace std;
const int INF=1e6+5;
int t,n,c,a[INF],b[INF];
ll sum;
int main()
{
	scanf("%d",&t);
	while (t--)
	{
		scanf("%d%d",&n,&c);
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		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++)
		{
			sum=c;
			sum+=a[i]-b[i];
			if(sum<0)
				continue;
			int ix=i+1;
			int cnt=1;
			while(sum>=0)
			{
				if(ix>n&&cnt<n)//進行回溯
				{
					ix=1;
				}
				sum+=a[ix]-b[ix];
				cnt++;//記錄加過的個數
				ix++;
				if(cnt==n)//每個數都加過了,跳出
				{
					break;
				}
			}
			if(sum>=0)//按從小到大的編號找,找到的第一個滿足條件的sum,其編號一定是最小的
			{
				printf("%d\n",i);
				break;
			}
		}
		if(sum<0)
			printf("-1\n");
	}
return 0;
}

我的又臭又長的代碼。。。想的太複雜了,果然T了:

#include<stdio.h>
#include<string>
#include<string.h>
#include<vector>
#include<list>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 1000005
int t,n,c,a[INF],b[INF],num,sum;
int x[INF],ans;
pair<int,int> pa[INF];
typedef pair<int,int> p;
queue<p> que;
bool cmp(const p &a,const p &b)
{
	if(a.first<b.first)
		return true;
	else
		return false;
}
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		while (!que.empty())
		{
			que.pop();
		}
		ans=INF;
		num=-1;
		scanf("%d%d",&n,&c);
		sum=c;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		for(int i=0;i<n;i++)
		{
			scanf("%d",&b[i]);
		}
		for(int i=0;i<n;i++)
		{
			x[i]=a[i]-b[i];
			pa[i]=make_pair(x[i],i+1);
		}
		sort(pa,pa+n,cmp);
		for(int i=0;i<n;i++)
		{
			que.push(pa[i]);
		}
		int nu=0;
 		while(1)
		{
			sum=c;
			for(int i=0;i<n;i++)
			{
				p tmp=que.front();
				if(sum>=0)
					sum+=tmp.first;
				que.pop();
				que.push(tmp);
			}
			p y=que.front();
			if(sum>=0)
				ans=min(ans,y.second);
			que.pop();
			que.push(y);
			nu++;
			if(nu==n)
			{
				if(ans==INF)
					printf("-1\n");
				else
					printf("%d\n",ans);
				goto end;
			}
		}
end:
		;
	}
	//system("puase");
return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章