슈콩

[SWEA] 코딩 토너먼트1 본문

Algorithms/SWEA

[SWEA] 코딩 토너먼트1

shukong 2025. 11. 15. 22:25

 

 

 

[문제]

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AW2Jldrqlo4DFASu

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

 

 

[소스 코드]

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));
		StringTokenizer st;
		int T = Integer.parseInt(br.readLine());
		for(int tc=1;tc<=T;tc++) {
			int k = (int)Math.pow(2,Integer.parseInt(br.readLine()));
			Queue<Integer> q = new LinkedList<>();
			st = new StringTokenizer(br.readLine());
			for(int i=0;i<k;i++) {
				q.offer(Integer.parseInt(st.nextToken()));
			}
			int size = q.size()/2;
			int result = 0;
			while(q.size()!=1) {
				for(int i=0;i<size;i++) {
					int a = q.poll();
					int b = q.poll();
					result += Math.abs(a-b);
					if(a<b) q.offer(b);
					else q.offer(a);
				}
				size = q.size()/2;
			}
			System.out.println("#"+tc+" "+result);
		}
	}
}

'Algorithms > SWEA' 카테고리의 다른 글

[SWEA] 구구단 걷기  (0) 2025.11.15
[SWEA] 공과 잡초  (0) 2025.11.15
[SWEA] 영준이의 카드 카운팅  (0) 2025.11.15
[SWEA] 민정이와 광직이의 알파벳 공부  (0) 2025.11.15
[SWEA] 이진수 표현  (0) 2025.11.15