문제 출처: https://www.acmicpc.net/problem/1966



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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
 
public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(reader.readLine());
        LinkedList<Integer> queue = new LinkedList<>();
        StringBuilder builder = new StringBuilder();
        
        for(int n=; n<T ; n++){
            String[] input = reader.readLine().split(" ");
            String[] priority = reader.readLine().split(" ");
            int N = Integer.parseInt(input[0]);
            int M = Integer.parseInt(input[1]);
            int count = 0;
            queue.clear(); // 큐 초기화
            
            for(int i=; i<N ; i++)
                queue.add(Integer.parseInt(priority[i])); // 큐에 중요도 데이터 추가
            
            while(!queue.isEmpty()){
                boolean isPriority = true;
                
                for(int i=; i<queue.size() ; i++){ // 맨 앞 데이터의 중요도가 가장 높은지 확인
                    if(queue.peek() < queue.get(i)){
                        isPriority = false;
                        break;
                    }
                }
                
                if(isPriority){ // 가장 높다면 큐에서 제거, 구하려는 값이 아니라면 M값 갱신
                    count++;
                    queue.poll();
                    
                    if(M == 0)
                        break;
                    else 
                        M -= 1;
                }
                else { // 중요도가 가장 높은 문서가 아니라면 뒤로 보내고 M값 갱신
                    int temp = queue.poll();
                    queue.add(temp);
                    M = (M==0) ? queue.size()---M;
                }
            }
            builder.append(count).append("\n");
        }
        System.out.println(builder);
    }
}
cs


'Algorithm' 카테고리의 다른 글

백준 7576번: 토마토  (0) 2018.10.26
백준 11866번: 조세퍼스 문제 0  (0) 2018.10.25
백준 10845번: 큐  (0) 2018.10.25
백준 6064번: 카잉 달력  (0) 2018.10.24
백준 1475번: 방 번호  (0) 2018.10.24

+ Recent posts