|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + String nextLine() throws Exception { |
| 17 | + String line = br.readLine(); |
| 18 | + st = new StringTokenizer(line); |
| 19 | + return line; |
| 20 | + } |
| 21 | + |
| 22 | + String nextToken() throws Exception { |
| 23 | + while (!st.hasMoreTokens()) nextLine(); |
| 24 | + return st.nextToken(); |
| 25 | + } |
| 26 | + |
| 27 | + int nextInt() throws Exception { |
| 28 | + return Integer.parseInt(nextToken()); |
| 29 | + } |
| 30 | + |
| 31 | + long nextLong() throws Exception { |
| 32 | + return Long.parseLong(nextToken()); |
| 33 | + } |
| 34 | + |
| 35 | + double nextDouble() throws Exception { |
| 36 | + return Double.parseDouble(nextToken()); |
| 37 | + } |
| 38 | + |
| 39 | + void close() throws Exception { |
| 40 | + bw.flush(); |
| 41 | + bw.close(); |
| 42 | + } |
| 43 | + |
| 44 | + void write(String content) throws Exception { |
| 45 | + bw.write(content); |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +public class Main { |
| 51 | + |
| 52 | + static IOController io; |
| 53 | + |
| 54 | + // |
| 55 | + |
| 56 | + static int N; |
| 57 | + static List<Integer>[] arr; |
| 58 | + |
| 59 | + public static void main(String[] args) throws Exception { |
| 60 | + |
| 61 | + io = new IOController(); |
| 62 | + |
| 63 | + init(); |
| 64 | + solve(); |
| 65 | + |
| 66 | + io.close(); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + static void init() throws Exception { |
| 71 | + |
| 72 | + N = io.nextInt(); |
| 73 | + arr = new List[N]; |
| 74 | + for(int i=0;i<N;i++) arr[i] = new ArrayList<>(); |
| 75 | + for(int i=0;i<2*N-1;i++) { |
| 76 | + int a = io.nextInt(); |
| 77 | + arr[a%N].add(a); |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + static void solve() throws Exception { |
| 83 | + |
| 84 | + for(int i:get(N,0)) io.write(i + " "); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + static List<Integer> get(int count, int mod) { |
| 89 | + if(count == 2) { |
| 90 | + List<Integer> res = new ArrayList<>(); |
| 91 | + // 짝수에서 찾기 |
| 92 | + for(int i=0;i<N;i+=2) if(arr[i].size() > 0) { |
| 93 | + int temp1 = arr[i].remove(arr[i].size()-1); |
| 94 | + for(int j=0;j<N;j+=2) if(arr[j].size() > 0) { |
| 95 | + int temp2 = arr[j].remove(arr[j].size()-1); |
| 96 | + if((temp1+temp2)%N != mod){ |
| 97 | + arr[j].add(temp2); |
| 98 | + continue; |
| 99 | + } |
| 100 | + res.add(temp1); |
| 101 | + res.add(temp2); |
| 102 | + return res; |
| 103 | + } |
| 104 | + arr[i].add(temp1); |
| 105 | + } |
| 106 | + for(int i=1;i<N;i+=2) if(arr[i].size() > 0) { |
| 107 | + int temp1 = arr[i].remove(arr[i].size()-1); |
| 108 | + for(int j=1;j<N;j+=2) if(arr[j].size() > 0) { |
| 109 | + int temp2 = arr[j].remove(arr[j].size()-1); |
| 110 | + if((temp1+temp2)%N != mod){ |
| 111 | + arr[j].add(temp2); |
| 112 | + continue; |
| 113 | + } |
| 114 | + res.add(temp1); |
| 115 | + res.add(temp2); |
| 116 | + return res; |
| 117 | + } |
| 118 | + arr[i].add(temp1); |
| 119 | + } |
| 120 | + return res; |
| 121 | + } |
| 122 | + |
| 123 | + List<Integer> res3 = get(count/2, mod/2); |
| 124 | + List<Integer> res4 = get(count/2, mod/2); |
| 125 | + if(res3.isEmpty() || res4.isEmpty()) { |
| 126 | + for(int i:res3) arr[i%N].add(i); |
| 127 | + for(int i:res4) arr[i%N].add(i); |
| 128 | + } |
| 129 | + else { |
| 130 | + for(int i:res4) res3.add(i); |
| 131 | + return res3; |
| 132 | + } |
| 133 | + |
| 134 | + List<Integer> res5 = get(count/2, (mod+N)/2); |
| 135 | + List<Integer> res6 = get(count/2, (mod+N)/2); |
| 136 | + if(res5.isEmpty() || res6.isEmpty()) { |
| 137 | + for(int i:res5) arr[i%N].add(i); |
| 138 | + for(int i:res6) arr[i%N].add(i); |
| 139 | + } |
| 140 | + else { |
| 141 | + for(int i:res6) res5.add(i); |
| 142 | + return res5; |
| 143 | + } |
| 144 | + |
| 145 | + return new ArrayList<>(); |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | +} |
| 150 | +``` |
0 commit comments