๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 10431_์ค„์„ธ์šฐ๊ธฐ

๋ฟŒ์•ผ._. 2024. 1. 29. 10:43

Silver V

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/10431)

< ์ค„์„ธ์šฐ๊ธฐ >

 

๋ฌธ์ œ ํ’€์ด 

 

ํ•œ ๋ช…์”ฉ ํ‚ค๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค ์•ž์—์„œ๋ถ€ํ„ฐ ์–ด๋””์— ์„ธ์›Œ์•ผ ํ• ์ง€ ํŒ๋‹จํ•œ๋‹ค.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class _10431_ { // ์ค„์„ธ์šฐ๊ธฐ

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st;

		int p = Integer.parseInt(bf.readLine());
		ArrayList<Integer> arr = new ArrayList<>();

		for (int i = 0; i < p; i++) {
			st = new StringTokenizer(bf.readLine());

			int t = Integer.parseInt(st.nextToken());
			int result = 0;

			for (int j = 0; j < 20; j++) {
				int num = Integer.parseInt(st.nextToken());
				if (j == 0) {
					arr.add(num);
				} else {
					boolean flag=false;

					for (int k = 0; k < j; k++) {
						if (arr.get(k) > num) {
							result += (j - k);
							flag=true;
							break;
						}
					}
					arr.add(num);
					if(flag) {
						Collections.sort(arr);
					}
				}
			}
			bw.write(t + " " + result + "\n");
			arr.clear();
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
p : ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜
arr : ๋ฐ˜ ์•„์ด๋“ค์˜ ํ‚ค
t : ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๋ฒˆํ˜ธ
result : ํ•™์ƒ๋“ค์ด ๋’ค๋กœ ๋ฌผ๋Ÿฌ๋‚œ ๊ฑธ์Œ ์ˆ˜์˜ ์ดํ•ฉ
num : ํ•™์ƒ์˜ ํ‚ค

 

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜๋งŒํผ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๋ฒˆํ˜ธ์™€ ๋ฐ˜ ์•„์ด๋“ค์˜ ํ‚ค ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๋ฐ˜ ์•„์ด๋“ค์˜ ํ‚ค๋ฅผ ํ•œ ๋ช…์”ฉ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค ์•ž์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜์—ฌ ์ž์‹ ๋ณด๋‹ค ํฐ ํ‚ค๊ฐ€ ์žˆ๋‹ค๋ฉด ์ž์‹ ์€ ๊ทธ ์ž๋ฆฌ์— ์„œ๊ณ  ๊ทธ ํ•™์ƒ๋ถ€ํ„ฐ ๊ทธ ๋’ค์˜ ๋ชจ๋“  ํ•™์ƒ์ด ํ•œ ๋ฐœ์”ฉ ๋’ค๋กœ ๋ฌผ๋Ÿฌ์„ ๋‹ค. ์ตœ์ข… ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๋ฒˆํ˜ธ์™€ ํ•™์ƒ๋“ค์ด ๋’ค๋กœ ๋ฌผ๋Ÿฌ๋‚œ ๊ฑธ์Œ ์ˆ˜์˜ ์ดํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.