Link to the original question: the Find at The answer
c ++ in, multiset is a library in a very useful type, it can be seen as a sequence, insert a number, delete a number can be completed in time O (logn), and he can always guarantee number in the sequence is sequence, and the sequence number of repeats may be present.
For details, see: multiset usage summary
Problem-solving ideas: For 1007 this question, when put a number x, to simulate the judge, and then insert sequence, a legal judgment, if a number is not legitimate, then deleting it, why can it be deleted? Because if a number is judged illegal, then the next he would no longer be used.
code show as below:
#include<set>
#include<iostream>
using namespace std;
typedef long long ll;
multiset<int> st;
ll sum;
void erase(ll x){
st.erase(st.find(x));
sum -= x;
}
void insert(ll x){
st.insert(x);
sum += x;
}
void solve(){
sum = 0;
st.clear();
int n, m, tot = 0;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++){
int x, num = 0;
scanf("%d", &x);
auto it = st.end();
ll tmp = sum;
while(tmp + x > m){
it--;
tmp -= (*it);
num++;
}
printf("%d ", tot + num);
insert(x);
while(sum > m){
auto it = st.end();
it--;
erase((*it));
tot++;
}
}
printf("\n");
}
int main(){
int t;
scanf("%d", &t);
while(t--){
solve();
}
return 0;
}
Reference: HDU 6609 at The answer the Find