# 유형 : 시뮬레이션, 구현

# 난이도 : 실버 3

# 시뮬레이션은 개인마다 푸는 방식이 다른 것 같다. 나의 경우는 매초마다 신호등을 갱신해주면서, 다음 신호등에 도착하였을 때 빨간 불이냐 아니냐를 따지면서 진행하면 된다. 만약 빨간 불인 경우 빨간불의 잔여시간만큼 time을 더해주고 *또 이 time 동안 신호등을 갱신해주면서 진행하면 된다.

 

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package bj;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
 
public class p2980 {
    static int N,L;
    static ArrayList<Po> arrList = new ArrayList<>();
    static ArrayList<Po> tmpList = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        L = Integer.parseInt(st.nextToken());
        
        for(int n=0; n<N; n++) {
            st = new StringTokenizer(br.readLine());
            int w = Integer.parseInt(st.nextToken());
            int r = Integer.parseInt(st.nextToken());
            int g = Integer.parseInt(st.nextToken());
            arrList.add(new Po(w,r,g));
            tmpList.add(new Po(w,r,g));
        }
        int time=0;
        int len = 0;
        int index=0;
        while(len<&& index<N) {
            
            int next = arrList.get(index).where;
            int tmp = len;
            for(int current=tmp+1; current<=next; current++) {
                
                for(int l=0; l<arrList.size(); l++) {
                    if(arrList.get(l).isRed) {
                        if(arrList.get(l).red == 1) {
                            arrList.get(l).red = tmpList.get(l).red;
                            arrList.get(l).isRed = false;
                        }else {
                            arrList.get(l).red--;
                        }    
                    }else {
                        if(arrList.get(l).green == 1) {
                            arrList.get(l).green = tmpList.get(l).green;
                            arrList.get(l).isRed = true;
                        }else {
                            arrList.get(l).green--;
                        }
                    }
                }
                
            }
            
            if(arrList.get(index).isRed) {
                int val = next - len;
                len += val;
                time += val;
                time += arrList.get(index).red;
                int iter = arrList.get(index).red;
                
                for(int i=0; i<iter; i++) {
                    for(int l=0; l<arrList.size(); l++) {
                        if(arrList.get(l).isRed) {
                            if(arrList.get(l).red == 1) {
                                arrList.get(l).red = tmpList.get(l).red;
                                arrList.get(l).isRed = false;
                            }else {
                                arrList.get(l).red--;
                            }    
                        }else {
                            if(arrList.get(l).green == 1) {
                                arrList.get(l).green = tmpList.get(l).green;
                                arrList.get(l).isRed = true;
                            }else {
                                arrList.get(l).green--;
                            }
                        }
                    }
                    
                }
            }else {
                int val = next - len;
                len += val;
                time += val;
            }
            index++;
        }
        time += (L-len);
        System.out.println(time);
    }
    
    public static class Po{
        int where;
        int red;
        int green;
        boolean isRed = true;
        public Po(int w, int r, int g) {
            this.where = w;
            this.red = r;
            this.green = g;
        }
    }
}
 
cs

+ Recent posts