ZOJ 2656 (题目忘了)

来源:互联网 发布:软件不支持中文字体 编辑:程序博客网 时间:2024/06/10 20:04
 

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:

210 1010 10210 2020 10210 2010 20

Sample Output:

01impossible

题意大概是某人开车环球旅行(一个圆),有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;elsecout<<"impossible"<<endl; } return 0;}


 

 

0 0
原创粉丝点击