๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/10442)
< Rank Order >
๋ฌธ์ ํ์ด
๋ค์ ์กฐ๊ฑด์ ํ์ฉํ์ฌ ๋ ๋ช ์ ์ฌ์ฌ์์์ ์ ์๋ฅผ ์ ์ฅํด, ์์๋ฅผ ๊ตฌํ๋ค.
๊ฐ์ ํ๋ ์ด์์ ๊ณต๋ฐฑ ๋๋ ์ ์ค๋ก ๊ตฌ์ฑ๋๋ค.
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.Comparator;
import java.util.StringTokenizer;
public class _10442_ { // Rank Order
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;
String str;
int idx = 1;
st = new StringTokenizer(bf.readLine());
while (true) {
if (!st.hasMoreTokens()) {
str = bf.readLine();
if (str == null) {
break;
}
if (str.equals("")) {
continue;
}
st = new StringTokenizer(str);
}
if (idx > 1) {
bw.write("\n");
}
bw.write("Case " + (idx++) + ": ");
int n = Integer.parseInt(st.nextToken());
ArrayList<int[]> first = new ArrayList<>();
int cnt = 0;
while (first.size() < n) {
if (!st.hasMoreTokens()) {
str = bf.readLine();
if (str.equals("")) {
continue;
}
st = new StringTokenizer(str);
}
first.add(new int[] { Integer.parseInt(st.nextToken()), cnt++ });
}
Collections.sort(first, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o2[0] - o1[0];
}
});
ArrayList<int[]> second = new ArrayList<>();
cnt = 0;
while (second.size() < n) {
if (!st.hasMoreTokens()) {
str = bf.readLine();
if (str.equals("")) {
continue;
}
st = new StringTokenizer(str);
}
second.add(new int[] { Integer.parseInt(st.nextToken()), cnt++ });
}
Collections.sort(second, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o2[0] - o1[0];
}
});
boolean flag = false;
for (int i = 0; i < n; i++) {
if (first.get(i)[1] != second.get(i)[1]) {
flag = true;
bw.write((i + 1) + "");
break;
}
}
if (!flag) {
bw.write("agree");
}
}
bw.flush();
}
}
๋ณ์)
str : ์ ๋ ฅ๊ฐ
idx : ์ผ์ด์ค ๋ฒํธ
n : ์ฐธ๊ฐ์ ์
first, second : ์ฒซ ๋ฒ์งธ ์ฌ์ฌ์์์ ์ ์, ๋ ๋ฒ์งธ ์ฌ์ฌ์์์ ์ ์
cnt : ์ฐธ๊ฐ์ ๋ฒํธ
์ ๋ ฅ๋ฐ์ ๊ฐ์ด ์๋ค๋ฉด ์๋ก ์ ๋ ฅ์ ๋ฐ๋๋ค. ์ด๋, ์ ๋ ฅ์ด ๋์ด๋ผ๋ฉด ์ข ๋ฃํ๋ค. ๋์ด ์๋๋ผ๋ฉด ์ฐธ๊ฐ์ ์๋ฅผ ์ ์ฅํ๊ณ ๊ฐ ์ฐธ๊ฐ์ ์์ ๋ง๊ฒ ์ฒซ ๋ฒ์งธ ์ฌ์ฌ์์์ ์ ์์ ๋ ๋ฒ์งธ ์ฌ์ฌ์์์ ์ ์๋ฅผ ๊ฐ ArrayList์ [์ ์, ์ฐธ๊ฐ์ ๋ฒํธ]๋ฅผ ์ ์ฅํ๋ค. ๊ฐ ArrayList๋ฅผ ์ ์ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ค. ์ ๋ ฌํ ArrayList ๋ ๊ฐ๋ฅผ ์์ฐจ ํ์ํ๋ฉฐ ์ฐธ๊ฐ์ ๋ฒํธ๋ฅผ ๋น๊ตํ๋ค. ๋ชจ๋ ์ผ์นํ๋ฉด "agree"๋ฅผ ์ถ๋ ฅํ๊ณ , ์ผ์นํ์ง ์๋ค๋ฉด ๊ทธ ์์๋ฅผ ์ถ๋ ฅํ๋ค.
'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 9491_Politics (2) | 2025.07.28 |
---|---|
[Baekjoon] 15198_NKD (2) | 2025.07.25 |
[Baekjoon] 15426_GlitchBot (3) | 2025.07.23 |
[Baekjoon] 30949_Equal Schedules (2) | 2025.07.22 |
[Baekjoon] 9400_Calculate the Fence Needed (3) | 2025.07.21 |