-
Notifications
You must be signed in to change notification settings - Fork 0
/
C. Game with Multiset.cpp
58 lines (36 loc) · 1.37 KB
/
C. Game with Multiset.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <map>
#include <algorithm>
#include <cmath>
// Link of the problem : https://codeforces.com/problemset/problem/1913/C
const bool findCombination(std::map<int, int> &ocorrencias, int target) {
if (target == 0) return true;
else if(target < 0 || ocorrencias.empty()) return false;
for (std::map<int,int>::reverse_iterator rit = ocorrencias.rbegin(); rit != ocorrencias.rend(); ++rit)
{
if(target >= (*rit).first) target -= (*rit).first * std::min((*rit).second, target/(*rit).first);
if(!target) return true;
}
return false;
}
int main() {
int command,n, number;
std::map<int,int> potencias, ocorrencias;
std::cin >> n;
for (int i = 0; i < n; i++)
{
std::cin >> command >> number;
if (command == 1)
{
if(potencias.find(number) == potencias.end()) potencias.insert(std::pair<int, int>(number, pow(2,number)));
if(ocorrencias.find(potencias[number]) == ocorrencias.end()) ocorrencias.insert(std::pair<int, int>(potencias[number], 1));
else ocorrencias[potencias[number]]++;
}
else if (command == 2)
{
if(findCombination(ocorrencias,number)) std::cout << "YES" << std::endl;
else std::cout << "NO" << std::endl;
}
}
return 0;
}