#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <string>
#include <fstream>
#include <algorithm>
#include <map>
#include <utility>
#include <bitset>
using ll = long long;
void print(std::multiset<ll> const& fishCount)
{
for (auto const& fish : fishCount)
std::cout << fish << " ";
std::cout << std::endl;
}
void brute_force(std::multiset<ll>& fishCount)
{
int q;
std::cin >> q;
for (int i = 0; i<q; ++i)
{
int type;
std::cin >> type;
ll w1, w2;
if (type == 2)
{
std::cin >> w1;
fishCount.insert(w1);
} else if (type == 3)
{
std::cin >> w1;
fishCount.erase(fishCount.find(w1));
}
else {
std::cin >> w1 >> w2;
if (w1 == w2)
{
std::cout << 0 << std::endl;
continue;
}
std::multiset<ll> deleted;
ll count = 0;
while(w1<w2)
{
if (fishCount.empty())
{
count = -1;
break;
}
auto it = fishCount.lower_bound(w1);
if (it == fishCount.begin())
{
count = -1;
break;
}
else if (it == fishCount.end())
{
if (*fishCount.begin() >= w1)
{
count = -1;
break;
}
else
{
it = fishCount.end();
}
}
it--;
w1 += *it;
deleted.insert(*it);
fishCount.erase(it);
count++;
}
fishCount.insert(deleted.begin(), deleted.end());
std::cout << count << std::endl;
}
}
}
int main()
{
int n;
std::vector<ll> fishs;
std::cin >> n;
for (int i = 0; i<n;++i)
{
ll tmp;
std::cin >> tmp;
fishs.push_back(tmp);
}
std::multiset<ll> fishCount;
for (auto const& fish : fishs)
{
fishCount.insert(fish);
}
brute_force(fishCount);
return 0;
}
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | #include <iostream> #include <vector> #include <cmath> #include <set> #include <string> #include <fstream> #include <algorithm> #include <map> #include <utility> #include <bitset> using ll = long long; void print(std::multiset<ll> const& fishCount) { for (auto const& fish : fishCount) std::cout << fish << " "; std::cout << std::endl; } void brute_force(std::multiset<ll>& fishCount) { int q; std::cin >> q; for (int i = 0; i<q; ++i) { int type; std::cin >> type; ll w1, w2; if (type == 2) { std::cin >> w1; fishCount.insert(w1); } else if (type == 3) { std::cin >> w1; fishCount.erase(fishCount.find(w1)); } else { std::cin >> w1 >> w2; if (w1 == w2) { std::cout << 0 << std::endl; continue; } std::multiset<ll> deleted; ll count = 0; while(w1<w2) { if (fishCount.empty()) { count = -1; break; } auto it = fishCount.lower_bound(w1); if (it == fishCount.begin()) { count = -1; break; } else if (it == fishCount.end()) { if (*fishCount.begin() >= w1) { count = -1; break; } else { it = fishCount.end(); } } it--; w1 += *it; deleted.insert(*it); fishCount.erase(it); count++; } fishCount.insert(deleted.begin(), deleted.end()); std::cout << count << std::endl; } } } int main() { int n; std::vector<ll> fishs; std::cin >> n; for (int i = 0; i<n;++i) { ll tmp; std::cin >> tmp; fishs.push_back(tmp); } std::multiset<ll> fishCount; for (auto const& fish : fishs) { fishCount.insert(fish); } brute_force(fishCount); return 0; } |
English