알고리즘/SwExpert recipe

[SWEA] 코딩토너먼트1 [D3]

컵라면만두세트 2021. 3. 11. 01:31
package D3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class 코딩토너먼트1 {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int t = Integer.parseInt(br.readLine());
		
		for(int tc=1; tc<=t; tc++) {
			int k = Integer.parseInt(br.readLine());
			
			int num = (int) Math.pow(2, k); // 거듭제곱
			List<Integer> list = new ArrayList<>();
			st = new StringTokenizer(br.readLine());
			
			for(int i =0; i<num; i++) {
				list.add(Integer.parseInt(st.nextToken())); // 7 1 4 3
			}
			
			int ans = 0; // 결과
			num /=2 ;
			
			for(int i =0; i<k; i++) { // i=0,1,2
				for(int j =0; j<num; j++) { // num 은 토너먼트 시킬 개수
					int a = list.get(j);
					int b = list.get(j+1);
					
					ans += Math.abs(a-b);
					
					if(a<b) {
						list.remove(j);
					}else {
						list.remove(j+1);
					}
				}
				num /= 2; // 올라갈수록 토너먼트 형식이니까
			}
			System.out.println("#" + tc + " " + ans);
			
		}
	}
}

'알고리즘 > SwExpert recipe' 카테고리의 다른 글

[SWEA] Summation D3  (0) 2021.03.18
[SWEA] 수영장 D3  (0) 2021.03.11
[SWEA] 다양성측정 D3  (0) 2021.03.07
[SWEA] 쥬스나누기 D3  (0) 2021.03.05
[SWEA] 소득 불균형 D3  (0) 2021.03.01