|
| 1 | +### [C++] |
| 2 | +```cpp |
| 3 | + |
| 4 | +#include <iostream> |
| 5 | +#include <set> |
| 6 | +#include <tuple> |
| 7 | +#include <bitset> |
| 8 | +#include <functional> |
| 9 | +using namespace std; |
| 10 | +// 난이도, 번호, 종류, id |
| 11 | +using problem = tuple<int, int, int, int>; |
| 12 | + |
| 13 | +int main() { |
| 14 | + cin.tie(0)->sync_with_stdio(0); |
| 15 | + |
| 16 | + int N; |
| 17 | + cin >> N; |
| 18 | + set<problem> Total, Type[101]{}, Rank[101]{}; |
| 19 | + |
| 20 | + bitset<200001> solved; |
| 21 | + |
| 22 | + int id = 0, numToId[200001]{}; |
| 23 | + |
| 24 | + for (int i = 0, p, l, g; i < N; i++) { |
| 25 | + cin >> p >> l >> g; |
| 26 | + problem pr = { l,p,g,numToId[p]=id++ }; |
| 27 | + Total.insert(pr); |
| 28 | + Type[g].insert(pr); |
| 29 | + Rank[l].insert(pr); |
| 30 | + } |
| 31 | + |
| 32 | + function isSolved = [&](problem pr) -> bool {return solved[get<3>(pr)]; }; |
| 33 | + |
| 34 | + int M; |
| 35 | + for (cin >> M; M--;) { |
| 36 | + string op; |
| 37 | + cin >> op; |
| 38 | + if (op == "recommend") { |
| 39 | + int g, x; |
| 40 | + cin >> g >> x; |
| 41 | + while (1) { |
| 42 | + problem pr = x == 1 ? *Type[g].rbegin() : *Type[g].begin(); |
| 43 | + if (isSolved(pr)) Type[g].erase(pr); |
| 44 | + else break; |
| 45 | + } |
| 46 | + cout << get<1>(x == 1 ? *Type[g].rbegin() : *Type[g].begin()) << '\n'; |
| 47 | + } |
| 48 | + if (op == "recommend2") { |
| 49 | + int x; |
| 50 | + cin >> x; |
| 51 | + while (1) { |
| 52 | + problem pr = x == 1 ? *Total.rbegin() : *Total.begin(); |
| 53 | + if (isSolved(pr)) Total.erase(pr); |
| 54 | + else break; |
| 55 | + } |
| 56 | + cout << get<1>(x == 1 ? *Total.rbegin() : *Total.begin()) << '\n'; |
| 57 | + } |
| 58 | + if (op == "recommend3") { |
| 59 | + int x, l; |
| 60 | + cin >> x >> l; |
| 61 | + bool f = 0; |
| 62 | + if (x == 1) { |
| 63 | + for (int i = l; i <= 100; i++) { |
| 64 | + while (!Rank[i].empty()) { |
| 65 | + problem pr = *Rank[i].begin(); |
| 66 | + if (isSolved(pr)) Rank[i].erase(pr); |
| 67 | + else { f = 1; break; } |
| 68 | + } |
| 69 | + if (f) { cout << get<1>(*Rank[i].begin()) << '\n'; break; } |
| 70 | + } |
| 71 | + if (!f) cout << "-1\n"; |
| 72 | + } |
| 73 | + else { |
| 74 | + for (int i = l - 1; i > 0; i--) { |
| 75 | + while (!Rank[i].empty()) { |
| 76 | + problem pr = *Rank[i].rbegin(); |
| 77 | + if (isSolved(pr)) Rank[i].erase(pr); |
| 78 | + else { f = 1; break; } |
| 79 | + } |
| 80 | + if (f) { cout << get<1>(*Rank[i].rbegin()) << '\n'; break; } |
| 81 | + } |
| 82 | + if (!f) cout << "-1\n"; |
| 83 | + } |
| 84 | + } |
| 85 | + if (op == "add") { |
| 86 | + int p, l, g; |
| 87 | + cin >> p >> l >> g; |
| 88 | + problem pr = { l,p,g,numToId[p] = id++ }; |
| 89 | + Total.insert(pr); |
| 90 | + Type[g].insert(pr); |
| 91 | + Rank[l].insert(pr); |
| 92 | + } |
| 93 | + if (op == "solved") { |
| 94 | + int p; |
| 95 | + cin >> p; |
| 96 | + solved[numToId[p]] = 1; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | +} |
| 101 | + |
| 102 | +``` |
| 103 | + |
| 104 | +### [Java] |
| 105 | + |
| 106 | +```java |
| 107 | + |
| 108 | +import java.util.*; |
| 109 | +import java.io.*; |
| 110 | + |
| 111 | +class Problem{ |
| 112 | + int num, type, rank, id; |
| 113 | + Problem(int num, int type, int rank, int id){ |
| 114 | + this.num = num; |
| 115 | + this.type = type; |
| 116 | + this.rank = rank; |
| 117 | + this.id = id; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +class Main { |
| 122 | + |
| 123 | + // IO field |
| 124 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 125 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 126 | + static StringTokenizer st; |
| 127 | + |
| 128 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 129 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 130 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 131 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 132 | + |
| 133 | + // Additional field |
| 134 | + static TreeSet<Problem> Total = new TreeSet<>((a,b) -> { |
| 135 | + if(a.rank == b.rank) return a.num-b.num; |
| 136 | + return a.rank-b.rank; |
| 137 | + }); |
| 138 | + static TreeSet<Problem>[] Type = new TreeSet[101]; |
| 139 | + static TreeSet<Problem>[] Rank = new TreeSet[101]; |
| 140 | + static boolean[] vis = new boolean[200001]; |
| 141 | + static int id = 0; |
| 142 | + static int N; |
| 143 | + static int[] numToId = new int[200001]; |
| 144 | + |
| 145 | + public static void main(String[] args) throws Exception { |
| 146 | + |
| 147 | + ready(); |
| 148 | + solve(); |
| 149 | + |
| 150 | + bwEnd(); |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + static void ready() throws Exception{ |
| 155 | + |
| 156 | + N = Integer.parseInt(br.readLine()); |
| 157 | + for(int i=1;i<=100;i++) { |
| 158 | + Type[i] = new TreeSet<>((a,b) -> { |
| 159 | + if(a.rank == b.rank) return a.num-b.num; |
| 160 | + return a.rank-b.rank; |
| 161 | + }); |
| 162 | + Rank[i] = new TreeSet<>((a,b) -> { |
| 163 | + if(a.rank == b.rank) return a.num-b.num; |
| 164 | + return a.rank-b.rank; |
| 165 | + }); |
| 166 | + } |
| 167 | + for(int i=0;i<N;i++) { |
| 168 | + nextLine(); |
| 169 | + int p = nextInt(), l = nextInt(), g = nextInt(); |
| 170 | + Problem now = new Problem(p, g, l, id); |
| 171 | + numToId[p] = id++; |
| 172 | + Total.add(now); |
| 173 | + Type[g].add(now); |
| 174 | + Rank[l].add(now); |
| 175 | + } |
| 176 | + |
| 177 | + |
| 178 | + } |
| 179 | + |
| 180 | + static void solve() throws Exception{ |
| 181 | + |
| 182 | + int M = Integer.parseInt(br.readLine()); |
| 183 | + while(M-- > 0) { |
| 184 | + nextLine(); |
| 185 | + String op = st.nextToken(); |
| 186 | + if(op.equals("recommend")) { |
| 187 | + int g = nextInt(), x = nextInt(); |
| 188 | + if(x == 1) { |
| 189 | + while(true) { |
| 190 | + Problem now = Type[g].last(); |
| 191 | + if(vis[now.id]) Type[g].pollLast(); |
| 192 | + else break; |
| 193 | + } |
| 194 | + bw.write(Type[g].last().num + "\n"); |
| 195 | + } |
| 196 | + else { |
| 197 | + while(true) { |
| 198 | + Problem now = Type[g].first(); |
| 199 | + if(vis[now.id]) Type[g].pollFirst(); |
| 200 | + else break; |
| 201 | + } |
| 202 | + bw.write(Type[g].first().num + "\n"); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + if(op.equals("recommend2")) { |
| 207 | + int x = nextInt(); |
| 208 | + if(x == 1) { |
| 209 | + while(true) { |
| 210 | + Problem now = Total.last(); |
| 211 | + if(vis[now.id]) Total.pollLast(); |
| 212 | + else break; |
| 213 | + } |
| 214 | + bw.write(Total.last().num + "\n"); |
| 215 | + } |
| 216 | + else { |
| 217 | + while(true) { |
| 218 | + Problem now = Total.first(); |
| 219 | + if(vis[now.id]) Total.pollFirst(); |
| 220 | + else break; |
| 221 | + } |
| 222 | + bw.write(Total.first().num + "\n"); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + if(op.equals("recommend3")) { |
| 227 | + int x = nextInt(), l = nextInt(); |
| 228 | + boolean find = false; |
| 229 | + if(x == 1) { |
| 230 | + for(int i=l;i<=100;i++) { |
| 231 | + while(!Rank[i].isEmpty()) { |
| 232 | + Problem now = Rank[i].first(); |
| 233 | + if(vis[now.id]) Rank[i].pollFirst(); |
| 234 | + else { |
| 235 | + find = true; |
| 236 | + break; |
| 237 | + } |
| 238 | + } |
| 239 | + if(find) { |
| 240 | + bw.write(Rank[i].first().num + "\n"); |
| 241 | + break; |
| 242 | + } |
| 243 | + } |
| 244 | + if(!find) bw.write("-1\n"); |
| 245 | + } |
| 246 | + else { |
| 247 | + for(int i=l-1;i>0;i--) { |
| 248 | + while(!Rank[i].isEmpty()) { |
| 249 | + Problem now = Rank[i].last(); |
| 250 | + if(vis[now.id]) Rank[i].pollLast(); |
| 251 | + else { |
| 252 | + find = true; |
| 253 | + break; |
| 254 | + } |
| 255 | + } |
| 256 | + if(find) { |
| 257 | + bw.write(Rank[i].last().num + "\n"); |
| 258 | + break; |
| 259 | + } |
| 260 | + } |
| 261 | + if(!find) bw.write("-1\n"); |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + if(op.equals("add")) { |
| 266 | + int p = nextInt(), l = nextInt(), g = nextInt(); |
| 267 | + Problem pr = new Problem(p, g, l, id); |
| 268 | + numToId[p] = id++; |
| 269 | + Total.add(pr); |
| 270 | + Type[g].add(pr); |
| 271 | + Rank[l].add(pr); |
| 272 | + } |
| 273 | + |
| 274 | + if(op.equals("solved")) { |
| 275 | + int p = nextInt(); |
| 276 | + vis[numToId[p]] = true; |
| 277 | + } |
| 278 | + |
| 279 | + } |
| 280 | + |
| 281 | + |
| 282 | + } |
| 283 | + |
| 284 | +} |
| 285 | + |
| 286 | +``` |
0 commit comments