๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/1015)
< ์์ด ์ ๋ ฌ >
๋ฌธ์ ํ์ด
์ค๋ฆ์ฐจ์์ผ๋ก ๋ง๋๋ ์์ด P๋ฅผ ๊ตฌํ๊ธฐ ์ํด์๋ ๋ฐฐ์ด A์ ์์ ์ค์์ ์์ ๊ฐ๋ถํฐ ์์ ๊ฐ์ ์ฃผ๋ฉด ๋๋ค.
๋ฌธ์ ์์ ์์ ์ฃผ์ด์ง ๊ฒ์ผ๋ก ์ค๋ช ํด ๋ณด๋ฉด
A [2,3,1]
P [1,2,0]
-> B [1]=2, B [2]=3, B [0]=1๋ก B [1,2,3]์ด ์์ฑ๋๋ค.
my solution (Java)
import java.io.*;
import java.util.*;
public class _1015_ { // ์์ด ์ ๋ ฌ
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 n = Integer.parseInt(bf.readLine());
st = new StringTokenizer(bf.readLine());
PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[0] == o2[0])
return o1[1] - o2[1];
return o1[0] - o2[0];
}
});
for (int i = 0; i < n; i++) {
queue.add(new int[] { Integer.parseInt(st.nextToken()), i });
}
int[] result = new int[n];
int idx = 0;
while (!queue.isEmpty()) {
int temp[] = queue.poll();
result[temp[1]] = idx++;
}
for (int i = 0; i < n; i++) {
bw.write(result[i] + " ");
}
bw.flush();
}
}
Main
๋ณ์)
n : ๋ฐฐ์ด A ํฌ๊ธฐ
queue : [๋ฐฐ์ด A ๊ฐ, index]๋ฅผ ์ ์ฅํ๋ฉฐ ๋ฐฐ์ด A ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ, ๋ฐฐ์ด A ๊ฐ์ด ๊ฐ๋ค๋ฉด index ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
result : ์์ด P
idx : ์์ด P ๊ฐ
- ๋ฐฐ์ด A ํฌ๊ธฐ(n) ์ ๋ ฅ
- ๋ฐฐ์ด A์ ์์๋ฅผ ์ ๋ ฅ๋ฐ์ ์ฐ์ ์์ ํ์ ์ ์ฅ
- ์ฐ์ ์์ ํ๊ฐ ๋น ๋๊น์ง ๋ฐ๋ณต
: ์์๋ฅผ ๋ฝ์ index ์์น์ 0๋ถํฐ ์ ์ฅ
- ์์ด P ์ถ๋ ฅ
'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 1058_์น๊ตฌ (1) | 2023.10.23 |
---|---|
[Baekjoon] 1956_์ด๋ (0) | 2023.10.20 |
[Baekjoon] 20044_Project Teams (0) | 2023.10.18 |
[Baekjoon] 2548_๋ํ ์์ฐ์ (0) | 2023.10.17 |
[Baekjoon] 1946_์ ์ ์ฌ์ (1) | 2023.10.16 |