ZOJ 2656 (题目忘了)

 

zoj的一道水题,本来是比赛的,我这个水平比较菜,第一次比赛啊有木有。。结果还看错时间了,等啊等啊为什么发现只有我一人在那提交? 仔细一看昨天晚上的比赛。。orz

蛋疼的很,很水的一道题,做了老长时间,。。sad

Description

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

题意大概是某人开车环球旅行(一个圆),有N个城市,每个城市都有一个加油站,问起点在哪能游遍全球?

思路就是遍历1~N个城市,判断当前城市是否可以循环一圈,可以则break

#include <iostream>  
#include <cstdio>
#include <cstring>
using namespace std;
int oil[10010];
int dis[10010];
int is_ok(int x,int n) //判断当前城市是否可以循环走一圈
{
 int i;int sum=0;
 for(i=x;i<n;i++) //x为起点
 {
  sum+=(oil[i]-dis[i]);
  if(sum<0)
   return 0;
 }
  for(i=0;i<x;i++)
  {
   sum+=(oil[i]-dis[i]);
  if(sum<0)
   return 0;
  }
 return 1;
}
int main()
{
 int n,i;
 while(cin>>n)
 {
  for(i=0;i<n;i++)
  cin>>oil[i]>>dis[i];
  int p=0;
  for(i=0;i<n;i++)
    if(is_ok(i,n)) //从第一个城市开始判断 如果可以循环一圈 break;
	{p++;break;}  
	if(p)
		cout<<i<<endl;
	else
		cout<<"impossible"<<endl;

 }
 return 0;
}


 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值