문제 링크 : https://www.acmicpc.net/problem/1966


그냥 문제에서 하라는 대로 그대로 구현하였다.


1. 우선 '중요도'와 '초기 인덱스' 값을 갖는 Doc클래스를 하나 만들었다.


2. ArrayList를 만들고 그안에 입력값을 Doc객체를 만들어 넣어주었다.


3. ArrayList의 가장 앞에 있는 원소의 '중요도'를 확인하고

 뒤의 원소중 가장 앞에있는 원소보다 중요도가 높은 원소가 하나라도 있다면, 맨앞의 원소를 뒤로 보냈다.

 만약 그렇지 않다면 맨앞의 원소가 찾는 원소인지 확인하여 찾는 원소가 아니라면 삭제하였고, 찾는 원소라면 몇번째 원소인지 출력하고 삭제한 후 반복을 종료했다.


아래는 소스코드이다.


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
import java.util.ArrayList;
import java.util.Scanner;
 
public class Main {
    
    public static void main(String[] args) {
        
        int t, n, m, count;
        Scanner sc = new Scanner(System.in);
        t = sc.nextInt();
        
        for(int i=0; i<t; i++) {
            n = sc.nextInt();
            m = sc.nextInt(); //찾을 인덱스
            count = 0;
            ArrayList<Doc> ar = new ArrayList<Doc>();
            
            for(int j=0; j<n; j++) {
                int imp = sc.nextInt();
                ar.add(new Doc(imp, j));
            }
            
            while(!ar.isEmpty()) {
                boolean find = false;
                for(int k=1; k<ar.size(); k++) {
                    if(ar.get(k).importance> ar.get(0).importance) {
                        find = true;
                        break;
                    }
                }
                
                Doc first = ar.get(0);
                if(find) {
                    ar.add(first);
                    ar.remove(0);
                }
                else {
                    count++;
                    if(first.index==m) {
                        System.out.println(count);
                        break;
                    }
                    else {
                        ar.remove(0);
                    }
                }
            }
        }
    }
}
 
class Doc {
    int importance;
    int index;
    
    Doc(int importance, int index) {
        this.importance = importance;
        this.index = index;
    }
}
cs



'Algorithm > 백준' 카테고리의 다른 글

백준 2468번 안전영역  (0) 2019.01.22
백준 1012번 유기농 배추  (0) 2019.01.21
백준 15686번 치킨 배달  (0) 2019.01.17
백준 14502번 연구소  (0) 2019.01.16
백준 6603번 로또  (0) 2019.01.14

+ Recent posts