#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<long long> A;
int szukajindex(long long a, vector<long long> C)
{
int b = 0, e = C.size()-1;
while (e>b)
{
int x = (e+b)/2;
if (C[x] < a) b = x+1;
else e = x;
}
if (C[e] > a)return e-1;
return e;
}
void dodaj()
{
long long a;
int b;
cin >>a;
A.push_back(a);
b = A.size()-1;
while (b >=1 && A[b-1] > A[b]){swap(A[b-1],A[b]);b--;}
if (A[0] > A[b])swap(A[0],A[b]);
}
void odejmij()
{
long long a;
cin >>a;
A.push_back(a);
A.erase(remove(A.begin(),A.end(),a), A.end());
}
void atak()
{
long long s,k;
int a=0;
cin >> s >> k;
vector<long long> B = A;
if (s <=1)cout << -1<<'\n';
else
{
while (s < k && B.size() != 0)
{
if (B[0] >= s){cout << -1<<'\n';return;}
long long temp = B[szukajindex(s-1,B)];
B.erase(B.begin() + szukajindex(s-1,B));
s += temp;
a++;
}
if (s<k && B.size() == 0)cout << -1<<'\n';
else cout << a << '\n';
}
}
int main()
{
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio(0);
int x;
cin >> x;
A.resize(x);
for (int lgbt = 0;lgbt<x;lgbt++)cin >> A[lgbt];
sort(A.begin(),A.end());
int y;
cin >> y;
for (int lgbt=0;lgbt<y;lgbt++)
{
int z = 0;
cin >> z;
if (z==1) atak();
else if(z==2) dodaj();
else if(z==3) odejmij();
}
}
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; vector<long long> A; int szukajindex(long long a, vector<long long> C) { int b = 0, e = C.size()-1; while (e>b) { int x = (e+b)/2; if (C[x] < a) b = x+1; else e = x; } if (C[e] > a)return e-1; return e; } void dodaj() { long long a; int b; cin >>a; A.push_back(a); b = A.size()-1; while (b >=1 && A[b-1] > A[b]){swap(A[b-1],A[b]);b--;} if (A[0] > A[b])swap(A[0],A[b]); } void odejmij() { long long a; cin >>a; A.push_back(a); A.erase(remove(A.begin(),A.end(),a), A.end()); } void atak() { long long s,k; int a=0; cin >> s >> k; vector<long long> B = A; if (s <=1)cout << -1<<'\n'; else { while (s < k && B.size() != 0) { if (B[0] >= s){cout << -1<<'\n';return;} long long temp = B[szukajindex(s-1,B)]; B.erase(B.begin() + szukajindex(s-1,B)); s += temp; a++; } if (s<k && B.size() == 0)cout << -1<<'\n'; else cout << a << '\n'; } } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); int x; cin >> x; A.resize(x); for (int lgbt = 0;lgbt<x;lgbt++)cin >> A[lgbt]; sort(A.begin(),A.end()); int y; cin >> y; for (int lgbt=0;lgbt<y;lgbt++) { int z = 0; cin >> z; if (z==1) atak(); else if(z==2) dodaj(); else if(z==3) odejmij(); } } |
English