#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
const int MAX = 400000+1;
long long int t[MAX];
int binar(int first, int last, long long int val)
{
int it;
int count, step;
count = last-first;
while (count>0)
{
it = first;
step=count/2;
it+=step;
if (!(val<t[it])){
first=++it;
count-=step+1;
}
else count=step;
}
return first;
}
int binar_low(int first, int last, long long int val)
{
int it;
int count, step;
count = last-first;
while (count>0)
{
it = first;
step=count/2;
it+=step;
if (t[it]<val) {
first=++it;
count-=step+1;
}
else count=step;
}
return first;
}
map<int, int> znal;
int compress(int pos){
int tmp = pos;
if(znal.find(tmp)!=znal.end() && tmp >= 0){
int nextindex = compress(znal[pos]);
znal[pos]= nextindex - 1;
tmp=nextindex;
}
return tmp;
}
int main(){
int n = 0;
int q = 0;
int p = 0;
long long int w_p = 0;
long long int w_k = 0;
long long int w_tmp = 0;
cin >> n;
for(int i=0; i<n; ++i){
cin >> t[i];
}
sort(t, t+n);
cin >> q;
for(int i=0; i<q; ++i){
cin >> p;
if(p==1){
cin >> w_p;
cin >> w_k;
w_tmp = w_p;
int k = 0;
while(w_tmp < w_k){
int pos = binar(0, n, w_tmp-1);
pos--;
pos = compress(pos);
if(pos < 0){
k=-1;
break;
}
w_tmp+=t[pos];
k++;
znal[pos] = pos-1;
}
znal.clear();
cout << k << "\n";
}
if(p==2){
cin >> t[n];
n++;
int x=n-1;
long long int tmp = t[x];
while((x-1) >= 0 && tmp < t[x-1]){
t[x] = t[x-1];
x--;
}
t[x] = tmp;
}
if(p==3){
long long int usu;
cin >> usu;
int pos = binar_low(0, n, usu);
long long int *pos2 = lower_bound(t, t+n, usu);
n--;
for(int x = pos; x < n; ++x){
t[x]=t[x+1];
}
}
}
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | #include <cstdio> #include <iostream> #include <algorithm> #include <map> using namespace std; const int MAX = 400000+1; long long int t[MAX]; int binar(int first, int last, long long int val) { int it; int count, step; count = last-first; while (count>0) { it = first; step=count/2; it+=step; if (!(val<t[it])){ first=++it; count-=step+1; } else count=step; } return first; } int binar_low(int first, int last, long long int val) { int it; int count, step; count = last-first; while (count>0) { it = first; step=count/2; it+=step; if (t[it]<val) { first=++it; count-=step+1; } else count=step; } return first; } map<int, int> znal; int compress(int pos){ int tmp = pos; if(znal.find(tmp)!=znal.end() && tmp >= 0){ int nextindex = compress(znal[pos]); znal[pos]= nextindex - 1; tmp=nextindex; } return tmp; } int main(){ int n = 0; int q = 0; int p = 0; long long int w_p = 0; long long int w_k = 0; long long int w_tmp = 0; cin >> n; for(int i=0; i<n; ++i){ cin >> t[i]; } sort(t, t+n); cin >> q; for(int i=0; i<q; ++i){ cin >> p; if(p==1){ cin >> w_p; cin >> w_k; w_tmp = w_p; int k = 0; while(w_tmp < w_k){ int pos = binar(0, n, w_tmp-1); pos--; pos = compress(pos); if(pos < 0){ k=-1; break; } w_tmp+=t[pos]; k++; znal[pos] = pos-1; } znal.clear(); cout << k << "\n"; } if(p==2){ cin >> t[n]; n++; int x=n-1; long long int tmp = t[x]; while((x-1) >= 0 && tmp < t[x-1]){ t[x] = t[x-1]; x--; } t[x] = tmp; } if(p==3){ long long int usu; cin >> usu; int pos = binar_low(0, n, usu); long long int *pos2 = lower_bound(t, t+n, usu); n--; for(int x = pos; x < n; ++x){ t[x]=t[x+1]; } } } return 0; } |
English