Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ade73a1

Browse files
committed
고다혜: [BOJ] 28707 배열 정렬_241126
1 parent e8991d7 commit ade73a1

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

‎BOJ/30000-35000번/DH_28707.java‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 배열 정렬
6+
*/
7+
8+
public class DH_28707 {
9+
static final int INF = Integer.MAX_VALUE;
10+
static class Node implements Comparable<Node> {
11+
int[] arr;
12+
int c;
13+
public Node(int[] arr, int c) {
14+
this.arr = arr;
15+
this.c = c;
16+
}
17+
18+
@Override
19+
public int compareTo(Node o) {
20+
return Integer.compare(this.c, o.c);
21+
}
22+
}
23+
static class Calculator {
24+
int l, r, c;
25+
public Calculator(int l, int r, int c) {
26+
this.l = l;
27+
this.r = r;
28+
this.c = c;
29+
}
30+
}
31+
static int N, M;
32+
static int[] A; // 초기 입력되는 배열
33+
static HashMap<String, Integer> cost; // key: Arrays.toString(int[] arr), value: int[] arr을 만들때까지 비용
34+
static ArrayList<Calculator> cal; // 조작에 대한 정보 저장
35+
36+
public static void main(String[] args) throws Exception {
37+
initInput();
38+
solution();
39+
}
40+
41+
static void solution() {
42+
PriorityQueue<Node> pq = new PriorityQueue();
43+
cost = new HashMap();
44+
45+
pq.add(new Node(A.clone(), 0));
46+
cost.put(Arrays.toString(A), 0);
47+
48+
while(!pq.isEmpty()) {
49+
Node current = pq.poll();
50+
51+
for(Calculator ca: cal) {
52+
int[] copyArr = current.arr.clone();
53+
int l = ca.l, r = ca.r, c = ca.c;
54+
55+
int tmp = copyArr[l];
56+
copyArr[l] = copyArr[r];
57+
copyArr[r] = tmp;
58+
59+
String key = Arrays.toString(copyArr);
60+
61+
int nextCost = current.c + c;
62+
if(!cost.containsKey(key)) cost.put(key, INF);
63+
if(nextCost < cost.get(key)) {
64+
cost.put(key, nextCost);
65+
pq.add(new Node(copyArr, nextCost));
66+
}
67+
}
68+
}
69+
70+
Arrays.sort(A); // A의 비내림차순 결과
71+
String key = Arrays.toString(A);
72+
73+
// cost의 키에 A의 비내림차순 결과가 있으면 비용 get하고, 아니라면 -1 출력
74+
System.out.println(cost.containsKey(key) ? cost.get(key) : -1);
75+
}
76+
77+
static void initInput() throws Exception {
78+
System.setIn(new FileInputStream("./input/BOJ28707.txt"));
79+
80+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
81+
StringTokenizer st;
82+
83+
N = Integer.parseInt(br.readLine());
84+
A = new int[N];
85+
86+
st = new StringTokenizer(br.readLine());
87+
for(int i = 0; i < A.length; i++) A[i] = Integer.parseInt(st.nextToken());
88+
89+
M = Integer.parseInt(br.readLine());
90+
91+
// 조작 list 저장
92+
cal = new ArrayList();
93+
for(int i = 0; i < M; i++) {
94+
st = new StringTokenizer(br.readLine());
95+
int l = Integer.parseInt(st.nextToken()) - 1;
96+
int r = Integer.parseInt(st.nextToken()) - 1;
97+
int c = Integer.parseInt(st.nextToken());
98+
cal.add(new Calculator(l, r, c));
99+
}
100+
}
101+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /