解题报告 之 UVA11093 Just Finish it up

解题报告 之 UVA11093 Just Finish it up

Description 

06

Problem J: Just Finish it up

Input: standard input

Output: standard output

 

Along a circular track, there are N gas stations, which are numbered clockwise from 1 up to N. At station i, there are i gallons of petrol available. To race from station to its clockwise neighbor one need qigallons of petrol. Consider a race where a car will start the race with an empty fuel tank. Your task is to find whether the car can complete the race from any of the stations or not. If it can then mention the smallest possible station i from which the lap can be completed.

 

Input

First line of the input contains one integer T the number of test cases. Each test case will start with a line containing one integer N, which denotes the number of gas stations. In the next few lines contain 2*Nintegers. First N integers denote the values of is (petrol available at station i), subsequent N integers denote the value of is (amount of patrol needed to go to the next station in the clockwise direction).

 

Output

For each test case, output the case number in the format “Case c: ”, where c is the case number starting form 1.  Then display whether it is possible to complete a lap by a car with an empty tank or not. If it is not possible to complete the lap then display “Not possible”. If possible, then display “Possible from station X”, where X is the first possible station from which the car can complete the lap.

 

Constraints

-           T < 25

-           N < 100001

 

Sample Input

Output for Sample Input

2

5

1 1 1 1 1

1 1 2 1 1

7

1 1 1 10 1 1 1

2 2 2 2 2 2 2

Case 1: Not possible

Case 2: Possible from station 4

 

Problemsetter: Md. Bahlul Haider

Judge Solution: Istiaque Ahmed

 

题目大意:一个环形的跑道上有n个加油站,编号1~n,每个加油站可以加pi单位油,而从这个加油站到下一个加油站需要qi单位油,假设油箱是无限大,问是否可以选择一个加油站为起点,使得可以绕这个跑道一周?

分析:这个题从0开始走,模拟即可,唯一注意的就是如果走到 i 发现油为负了,即走不到i,那么下一次选起点就要从i开始了。因为从1、2、3……i-1出发的话到达 i 时的油绝不可能比0还多。总的来说就是跳过中间的点。如果起点已经选到了最后一个都还是不行那么就不可能存在这样的起点了,输出not possible即可。
注意如何判断已经走过了一圈,我这个写的比较残是因为一开始没有想清楚。

上代码:
#include <iostream>
using namespace std;

int p[100010];
int q[100010];


int main()
{
	int kase;
	cin >> kase;
	for (int k = 1; k <= kase; k++)
	{
		int n;
		cin >> n;
		for (int i = 0; i < n; i++)
			cin >> p[i];
		for (int i = 0; i < n; i++)
			cin >> q[i];
		int tar = 0;
		int tem = 0;
		int gas = 0;
		bool flag = false;
		cout << "Case " << k << ": ";

		while (1)
		{
			gas += p[tem];
			gas -= q[tem];
			
			if (gas < 0)
			{
				tar = tem = tem + 1;
				if (tem >= n)
				{
					tem %= n;
					flag = true;
				}
				tar %= n;
				gas = 0;
				if (flag)
				{
					cout << "Not possible" << endl;
					break;
				}
			}
			else
			{
				if (tem == tar&&flag)
				{
					cout << "Possible from station " << tar + 1 << endl;
					break;
				}

				tem++;
				if (tem >= n)
				{
					tem %= n;
					flag = true;
				}
			}	
			
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值