[BOJ/C++] 2980 도로와 신호등

Hanbi·2022년 4월 4일
0

Problem Solving

목록 보기
12/108
post-thumbnail

문제

https://www.acmicpc.net/problem/2980

풀이

처음 신호등 만날 때까지 가는 시간 -> 신호등에서 기다리는 시간 -> 다음 신호등까지 가는 시간 -> ··· -> 목적지까지 가는 시간

신호등에서 기다리는 시간

사이클에서 지난 시간 : 신호등 도착시간 % (빨간불 지속시간+초록불 지속시간)

🟢안 기다림 : 사이클에서 지난 시간 >= 빨간불 시속시간
🔴기다림 : 사이클에서 지난 시간 < 빨간불 시속시간

기다리는 시간 : 빨간불 지속시간 - 사이클에서 지난 시간

코드

#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

int main(void) {
	int N, L;
	vector<pair<int, pair<int, int> > > info;
	
	cin >> N >> L;
	
	int d, r, g;
	for(int i=0; i<N; i++) {
		cin >> d >> r >> g;
		info.push_back(make_pair(d, make_pair(r,g)));
	}
	
	int time = 0;
	time += info[0].first;
	
	for(int i=0; i<N; i++) {
		if(time % (info[i].second.first+info[i].second.second) >= info[i].second.first) { //초록불 
			;
		}
		else { //빨간불 
			time += info[i].second.first - (time % (info[i].second.first+info[i].second.second)); //빨간불 지속 시간 - 사이클에서 지난 시간 
		}	
		
		if(i == N-1) break;
		time += info[i+1].first - info[i].first;
	}

	time += L - info[N-1].first;
	
	printf("%d", time);
	
	return 0;
}
profile
👩🏻‍💻

0개의 댓글