-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBOJ2980_kang.cpp
41 lines (37 loc) · 1.06 KB
/
BOJ2980_kang.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
#include <iostream>
#include <map>
using namespace std;
int n, l;
map<int, pair<int, int> > trafficLight;
// figure out the truck can pass right away or not
int canPass(int time, pair<int, int> light){
int cycle = light.first+light.second;
int curLight = time%cycle;
//returns waiting time. if can pass right away cuz of green light, then return value is minus.
return light.first - curLight;
}
int main(){
cin >> n >> l;
for(int i = 0 ; i < n ; i++){
int loc, red, green;
cin >> loc >> red >> green;
trafficLight[loc] = make_pair(red, green);
}
int timer = 0;
int location = 0;
while(location <= l){
//time increase every loop
timer ++;
//if there is a traffic light in location
if(trafficLight.find(location) != trafficLight.end()){
//get waiting time
int iter = canPass(timer, trafficLight[location]);
//if waiting time is under zero, no adding.
//if waiting time is over zero, add waiting time to timer.
timer = iter < 0 ? timer : timer+iter;
}
//if out of checking traffic light, move location + 1.
location++;
}
cout << timer;
}