SWEA_8673) 코딩 토너먼트1

8673) 코딩 토너먼트1 (19.10.20)


문제 링크 (SW Expert Academy)

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
import java.io.*;
import java.util.*;

public class Solution {
    public static void main(String args[]) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer tk;
        
        int test = Integer.parseInt(br.readLine());
        for(int i=1; i<=test; i++) {
            int k = Integer.parseInt(br.readLine());
            
            int num = 1;
            for(int j=0; j<k; j++) {
                num = num*2;
            }
            
            tk = new StringTokenizer(br.readLine());
            ArrayList<Integer> list = new ArrayList<>();
            
            for(int j=0; j<num; j++) {
                list.add(Integer.parseInt(tk.nextToken()));
            }
            
            num = num/2;
            int ans = 0;
            for(int j=0; j<k; j++) {
                for(int r=0; r<num; r++) {
                    int a = list.get(r);
                    int b = list.get(r+1);
                    
                    ans = ans+ Math.abs(a-b);
                    
                    if(a<b) {
                        list.remove(r);
                    }else {
                        list.remove(r+1);
                    }
                }
                num = num/2;
            }
            
            bw.write("#" + i + " " + ans + "\n");
        }
        
        bw.flush();
        bw.close();
    }
}
cs

댓글

이 블로그의 인기 게시물