Skip to content

Commit 9395cfc

Browse files
authored
Merge pull request #1067 from AlgorithmWithGod/0224LJH
[20251008] PGM / LV3 / n+1 카드게임 / 이종환
2 parents 889e882 + 3aa78b8 commit 9395cfc

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
/*
6+
결국 N+1을 내야한다면, 1~N에서 파트너를 맞춰야함. ->
7+
결국 종료는
8+
9+
1. N/2라운드가 지나서 모든 카드를 다 냈거나,
10+
2. N+1의 합을 못내서 종료
11+
12+
cards와 coin은 모두 1000 이하
13+
14+
우선 정리하자면
15+
- N+1 합을 만드는 것 사이의 우선순위는 X
16+
- 무조건 한 라운드에 하나씩은 만들어야함.
17+
18+
-> 기존에 가지고 있는 카드의 파트너라면 반드시 가져와야함.
19+
20+
모든 페어가, 어느 라운드에서 완성될 수 있는 지를 확인하자.
21+
그래서 빨리 완성되는 것을 우선순위로 하여서 PQ에 넣고, 꺼내면됨.
22+
이때 처음부터 둘 다 있었으면 -0, 하나만 있었으면 -1, 둘 다 없었으면 -2
23+
그리고 그렇게 완성되는 라운드가, 이전에 나온 페어 개수보다 적으면 도달할 수 없으니 탈락.
24+
25+
*/
26+
27+
class Solution {
28+
29+
static int coinLeft, cardCnt, pairCnt,curRound;
30+
static Pair[] pairs;
31+
static PriorityQueue<Pair> pq = new PriorityQueue<>();
32+
33+
34+
static class Pair implements Comparable<Pair>{
35+
int small,big;
36+
37+
int minIdx = Integer.MAX_VALUE;
38+
int maxIdx = Integer.MIN_VALUE;
39+
40+
public Pair(int small){
41+
this.small = small;
42+
this.big = cardCnt+1 -small;
43+
}
44+
45+
public void putIdx(int idx){
46+
minIdx = Math.min(minIdx, idx);
47+
maxIdx = Math.max(maxIdx, idx);
48+
}
49+
50+
public int cost(){
51+
if (minIdx == 0 && maxIdx == 0) return 0;
52+
else if (minIdx != 0 && maxIdx != 0) return 2;
53+
else return 1;
54+
}
55+
56+
@Override
57+
public int compareTo(Pair p){
58+
if (p.maxIdx == this.maxIdx){
59+
return Integer.compare(this.minIdx, p.minIdx);
60+
}
61+
return Integer.compare(this.maxIdx, p.maxIdx);
62+
}
63+
}
64+
65+
public int solution(int coin, int[] cards) {
66+
init(coin, cards);
67+
process();
68+
69+
70+
int answer = curRound;
71+
return answer;
72+
}
73+
74+
private void process(){
75+
curRound = 1;
76+
int maxRound = pairCnt*2 / 3;
77+
while(curRound <= maxRound ){
78+
int idx = -1;
79+
int curCost = 3;
80+
for (int i =1 ; i <= pairCnt; i++){
81+
Pair p =pairs[i];
82+
if (p == null) continue;
83+
if (p.maxIdx > curRound) continue;
84+
85+
int cost = p.cost();
86+
if (cost <= coinLeft && cost <= curCost){
87+
curCost = cost;
88+
idx = i;
89+
}
90+
}
91+
if (idx == -1) return;
92+
93+
curRound++;
94+
pairs[idx] = null;
95+
coinLeft -= curCost;
96+
97+
}
98+
99+
}
100+
101+
private void init(int coin, int[] cards){
102+
coinLeft = coin;
103+
cardCnt = cards.length;
104+
pairCnt = cardCnt/2;
105+
pairs = new Pair[pairCnt+1];
106+
107+
for (int i = 1; i <= pairCnt; i++){
108+
pairs[i] = new Pair(i);
109+
}
110+
111+
int zeroRoundCardCnt = cardCnt/3;
112+
int idx = 0;
113+
int round = 0;
114+
// System.out.println(zeroRoundCardCnt);
115+
for (int i = 0; i < zeroRoundCardCnt; i++){
116+
int num = cards[idx++];
117+
if (num > pairCnt) num = cardCnt+1 - num;
118+
119+
pairs[num].putIdx(0);
120+
}
121+
122+
round++;
123+
while(idx < cardCnt){
124+
for (int i = 0; i < 2; i++){
125+
int num = cards[idx++];
126+
if (num > pairCnt) num = cardCnt+1 - num;
127+
// System.out.println(num +" "+ round);
128+
pairs[num].putIdx(round);
129+
}
130+
round++;
131+
}
132+
133+
134+
}
135+
}
136+
```

0 commit comments

Comments
 (0)