-
Notifications
You must be signed in to change notification settings - Fork 0
/
gas.cpp
58 lines (50 loc) · 1.14 KB
/
gas.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
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <fstream>
#include <vector>
#include <string.h>
using namespace std;
int canCompleteCircuit(int gas[],int cost[]){
int N = sizeof(gas)/sizeof(gas[0]);
vector<int> diff(N, 0);
for (int i = 0; i < N; i++) {
diff[i] = gas[i] - cost[i];
}
int i = 0;
int j;
int k;
int S;
while (i < N) {
if (diff[i] < 0) {
i += 1;
continue;
}
S = diff[i];
k = 1;
while (k < N) {
j = (i + k) % N;
if (S + diff[j] >= 0) {
k += 1;
S += diff[j];
} else {
break;
}
}
if (k == N) {
return i;
} else {
i += k;
}
}
return -1;
}
int main(){
int gas[100];
for (int i = 0; i < 2; i++) {
cin >> gas[i];
}
int cost[100];
for (int i = 0; i < 2; i++) {
cin >> cost[i];
}
cout<<canCompleteCircuit (gas,cost);
}