Uva - 11093 - Just Finish it up

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 qi gallons 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*N integers. 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


先考虑从1号加油站出发,在这里加p1 gallons,一直这样走,如果失败了,则走不到的那一站之前的站都不能做起点了。因为之前的那些站走过的时候可能还有剩余多的,都失败了,从这些店出发肯定也失败。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset> 
#include <cassert> 
#include <cmath>
#include <functional>

using namespace std;

const int maxn = 100005;
int p[maxn], q[maxn];
int n;

int run(int s)
{
	int fuel = p[s] - q[s];
	for (int i = (s + 1) % n; i != s; i = (i + 1) % n) {
		if (fuel < 0) { // 在上一站加油,车上的总油量不足以走到此站
			return i;
		}
		fuel += p[i] - q[i]; // 加满油,往下一站走
	}
	if (fuel < 0) {
		return -1; //  走了一圈,sum[p] < sum[q],所以这种情况肯定是不可能的,直接返回-1
	}
	return s; // 成功返回起点
}

// 枚举起点
int solve()
{
	int start = 0;
	while (1) {
		int finish = run(start);
		if (finish < start) {
			return -1; // 失败
		}
		if (finish == start) { // 成功了
			return start;
		}
		start = finish; // 从断开的地方继续
	}
}

int main()
{
	ios::sync_with_stdio(false);
	int T;
	cin >> T;
	int kase = 0;
	while (T--) {
		cin >> n;
		for (int i = 0; i < n; i++) {
			cin >> p[i];
		}
		for (int i = 0; i < n; i++) {
			cin >> q[i];
		}

		int ans = solve();
		cout << "Case " << ++kase << ": ";
		if (ans >= 0) {
			cout << "Possible from station " << ans + 1 << endl;
		}
		else {
			cout << "Not possible\n";
		}
	}

	return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值