// Try.cpp : This file contains the 'main' function. Program execution begins and ends there. //Mateusz Wasilewski #include <bits/stdc++.h> #define ll long long using namespace std; int N; ll a,b; int act; map <ll, int> mapa,org; void Add(ll x); void Minus(ll x); void Act(int ac); void Szczupak(ll x, ll y); map <ll, int>::iterator binSearch(); int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N; //szprotki poczatkowe for (int i = 0; i < N; i++) { cin >> a; Add(a); } cin >> N; //wydarzenia on the go for (int i = 0; i < N; i++) { cin >> act; Act(act); } } void Szczupak(ll x, ll y) { int wynik = 0; while (x < y) { Add(x); auto poz = mapa.find(x); if (poz == mapa.begin()) { wynik = -1; break; } poz--; Minus(x); wynik ++; x += poz->first; Minus(poz->first); } cout << wynik<<endl; } void Add(ll x) { auto poz = mapa.find(x); if (poz == mapa.end()) { mapa[x] = 1; } else { poz->second++; } } void Minus(ll x) { auto poz = mapa.find(x); if (poz == mapa.end()) { cout << "Something went wrong with map" << endl; return; } poz->second--; if (poz->second == 0) { mapa.erase(poz->first); } } void Act(int ac=1) { if (ac == 1) { cin >> a >> b; org = mapa; Szczupak(a, b); mapa = org; } else if (ac == 2 || ac==3) { cin >> a; if (ac == 2) { Add(a); } else { Minus(a); } } } // Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu
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 | // Try.cpp : This file contains the 'main' function. Program execution begins and ends there. //Mateusz Wasilewski #include <bits/stdc++.h> #define ll long long using namespace std; int N; ll a,b; int act; map <ll, int> mapa,org; void Add(ll x); void Minus(ll x); void Act(int ac); void Szczupak(ll x, ll y); map <ll, int>::iterator binSearch(); int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N; //szprotki poczatkowe for (int i = 0; i < N; i++) { cin >> a; Add(a); } cin >> N; //wydarzenia on the go for (int i = 0; i < N; i++) { cin >> act; Act(act); } } void Szczupak(ll x, ll y) { int wynik = 0; while (x < y) { Add(x); auto poz = mapa.find(x); if (poz == mapa.begin()) { wynik = -1; break; } poz--; Minus(x); wynik ++; x += poz->first; Minus(poz->first); } cout << wynik<<endl; } void Add(ll x) { auto poz = mapa.find(x); if (poz == mapa.end()) { mapa[x] = 1; } else { poz->second++; } } void Minus(ll x) { auto poz = mapa.find(x); if (poz == mapa.end()) { cout << "Something went wrong with map" << endl; return; } poz->second--; if (poz->second == 0) { mapa.erase(poz->first); } } void Act(int ac=1) { if (ac == 1) { cin >> a >> b; org = mapa; Szczupak(a, b); mapa = org; } else if (ac == 2 || ac==3) { cin >> a; if (ac == 2) { Add(a); } else { Minus(a); } } } // Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu |