ZOJ 2656 Travel Around Country

Travel Around Country(贪心)

题目链接:点击跳转

Travel Around Country

Time Limit: 10000 msMemory Limit: 32768 KB

Driving a car around the country with PPMM is a one of Vivid��s Dreams. Now he is thinking about which city could be his start point, although he hasn��t got a car yet��

There are N main cities forming a loop in our country. Vivid wants to choose one city as his start point, then goes around the loop, and at last back to the city which he started from.

Of course, the car can��t finish the loop without refueling. Fortunately there always has a gas station in each city, and Vivid will take all of the oil from the gas station. You can assume the car consumes one unit of oil per one unit of distance. At the start point, the car has no oil at all, and will take all of the oil from the gas station.

Given N cities in order, with the amount of the oil it contains, and the distance to the next city. Please help Vivid to choose his start point.

Input

There are multiple test cases. Each test case begins with a Integer N (the number of the city, 2 <= N <=10000), then N lines followed with two Integers O (the amount of oil this city has) and D (the distance to the next city).

Output

Each test case has a line with a Integer, indicate which city could be the start point for Vivid. We consider the first city as 0, the second city as 1, and so on. If there is no such city can be found, just output impossible. If there are more than one city can be found, output the first city in the input.

Sample Input

2
10 10
10 10
2
10 20
20 10
2
10 20
10 20

Sample Output

0
1
impossible

解题思路:暴力从0开始枚举能不能走完即可,注意找到就要跳出

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 10005
int a[N],b[N];
int main(){
	int n;
	while(scanf("%d",&n)!=EOF){
		for(int i=0;i<n;i++){
			scanf("%d %d",&a[i],&b[i]);
		}
		int ans=-1;
		for(int i=0;i<n;i++){
			if(ans!=-1)break;
			int o=a[i],cnt=i;
			for(int j=0;j<n;j++){
				if(o<b[cnt])break;
				o-=b[cnt];
				cnt++;
				if(cnt==n){
					cnt=0;
				}
				o+=a[cnt];
				if(j==n-1){
					ans=i;
					break;
				}
			}
		}
		if(ans!=-1)printf("%d\n",ans);
		else printf("impossible\n");
	}
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值