-
Notifications
You must be signed in to change notification settings - Fork 0
/
0134.gas_station.cpp
48 lines (41 loc) · 1.03 KB
/
0134.gas_station.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
using std::vector;
int can_complete_circuit(vector<int>& gas, vector<int>& cost)
{
int n = gas.size();
int sum = 0;
int msum = 0;
int pos = 0;
// 因为题目中说了,保证答案唯一
// 那么找到剩余汽油最多的站即可
for (int i = 0; i < n; ++i) {
sum += (gas[i] - cost[i]);
if (sum < msum) {
msum = sum;
pos = i + 1;
}
}
// 用 sum 来判断究竟能够环绕一圈
if (sum < 0) return -1;
// 环形的话,最后一站的下一站是 0
return pos == n ? 0 : pos;
}
int main () {
#ifdef LOCAL
freopen("0134.in", "r", stdin);
#endif
int n = 0;
while (std::cin >> n) {
vector<int> gas(n, 0);
vector<int> cost(n, 0);
for (int i = 0; i < n; ++i) {
std::cin >> gas[i];
}
for (int i = 0; i < n; ++i) {
std::cin >> cost[i];
}
std::cout << can_complete_circuit(gas, cost) << std::endl;
}
return 0;
}