-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgas.cpp
More file actions
77 lines (61 loc) · 1.25 KB
/
gas.cpp
File metadata and controls
77 lines (61 loc) · 1.25 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
vector<int> E, C;
int N;
int gas = 0;
int isEnableDrive() {
int index = 0;
int result = -1;
bool end = false;
int check = 0;
// 일단은 다 돌아보자
while (index < N) {
int check = index; // check랑 같으면 답
int i = index; // 인덱스는 크게 돌 것, i는 이번 회차에서 사용할 것
gas = 0; // 가스 초기화s
gas = gas + E[i]; // 먼저 가스 충전
while (gas >= C[i]) {
gas = gas - C[i]; // 가스 소모.
if (i == N-1) {
i = (i % (N-1));
}
else {
i++;
}
// 답 나옴
if (i == check) {
result = index;
end = true;
break;
}
gas = gas + E[i]; // 가스 충전
}
if (end) break;
index++; // 반복문을 나오면 가스가 부족하다는 뜻이므로 못 도달한다.
}
return result;
}
int main(int argc, const char *argv[]) {
int T = 0;
cin >> T;
for (int i = 0; i < T; i++) {
cin >> N;
for (int i = 0; i < N; i++) {
int energy = 0;
cin >> energy;
E.push_back(energy);
}
for (int i = 0; i < N; i++) {
int cost = 0;
cin >> cost;
C.push_back(cost);
}
cout << isEnableDrive() << endl;
gas = 0;
E.clear();
C.clear();
}
}