백준 21944 문제 추천 시스템 Version 2

supway·2022년 2월 26일
0

백준

목록 보기
40/62

백준 21944
백준 21939 이 문제에서 recommend,recommend3가 추가된 형태이다.

#include <bits/stdc++.h>
#include<set>
#include<unordered_map>
using namespace std;
int INF = 987654321;
map<int, pair<int,int>> mp;
set<pair<int, int>> s[101]; // recommend1
set<pair<int, int>>s1;//recommend2, recommend3
int main() {
	ios::sync_with_stdio(0); cin.tie(0);

	int n;
	cin >> n;

	while (n--) {
		int a, b, c;
		cin >> a >> b >> c; //문제 번호, 난이도, 알고리즘 분류
		s[c].insert({ b,a });
		s1.insert({ b,a });
		mp[a] = { b,c };
	}

	int m;
	cin >> m;
	while (m--) {
		string x;
		cin >> x;
		if (x == "add") {
			int a, b, c;
			cin >> a >> b >> c;
			s[c].insert({ b,a });
			s1.insert({ b,a });
            mp[a]={b,c};
		}
		else if (x == "solved") {
			int p;
			cin >> p;
			s[mp[p].second].erase({ mp[p].first,p });
			s1.erase({ mp[p].first,p });
		}
		else {
			int a, b;
			if (x == "recommend") {
				cin >> a >> b;
				if (b == 1)
					cout << prev(s[a].end())->second << '\n';
				else
					cout << s[a].begin()->second << '\n';
			}
			else if (x == "recommend2") {
				cin >> a;
				if (a == 1) {
					cout << prev(s1.end())->second << '\n';
				}
				else {	
					cout << s1.begin()->second << '\n';
				}
			}
			else { // recommend3
				cin >> a>>b;
				if (a == 1) {
					auto it = s1.upper_bound({ b - 1,100001 });
					if (it != s1.end()) {
						cout << it->second << '\n';
					}
					else cout << -1 << '\n';
				}
				else {
					auto it = (s1.upper_bound({ b,0}));
					if (it != s1.begin()) {
						cout << prev(it)->second << '\n';
					}
					else cout << -1 << '\n';
				}
			}
		}
	}
}
profile
개발잘하고싶은사람

0개의 댓글