Find the answer
Time Limit: 4000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1093 Accepted Submission(s): 314
Problem Description
Given a sequence of n integers called W and an integer m. For each i (1 <= i <= n), you can choose some elements Wk (1 <= k < i), and change them to zero to make ∑ij=1Wj<=m. So what's the minimum number of chosen elements to meet the requirements above?.
Input
The first line contains an integer Q --- the number of test cases.
For each test case:
The first line contains two integers n and m --- n represents the number of elemens in sequence W and m is as described above.
The second line contains n integers, which means the sequence W.
1 <= Q <= 15
1 <= n <= 2*105
1 <= m <= 109
For each i, 1 <= Wi <= m
Output
For each test case, you should output n integers in one line: i-th integer means the minimum number of chosen elements Wk (1 <= k < i), and change them to zero to make ∑ij=1Wj<=m.
Sample Input
2 7 15 1 2 3 4 5 6 7 5 100 80 40 40 40 60
Sample Output
0 0 0 0 0 2 3 0 1 1 2 3
Source
2019 Multi-University Training Contest 3
Recommend
chendu
思路:根据题意,我们需要对每个w[i],从前i-1个数求出使和小于等于m-w[i]的最大元素个数。朴素地可以想到我只要从前i-1个数里按小的拿,拿到刚好满足条件,那么此时拿的个数就是答案。但这个想法的复杂度是O(n),整体复杂度为O(n^2),肯定会TLE,所以需要优化。我们可以考虑以前i-1个元素建立一棵平衡二叉搜索树,每次拿左子树的和,直到拿到刚好满足条件时既是答案,复杂度为O(logn),满足时限。
AC代码:(用了非旋Treap树,其他平衡树应该也可以)
#include<bits/stdc++.h>
//#include<vector>
//#include<iostream>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define ms(a,b) memset(a, b, sizeof(a))
typedef long long ll;
const int MAXN = 2e5 + 50;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
//const ll MOD = 1e9+7;
template <class T>
inline bool scan_d(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class C>
void deb(const char *name, C val){
cout << name << val <<endl;
}
struct node{
int l, r;
ll val, sum;
int key, sz;
}Tree[MAXN*2];
int tot;
void add(ll val){
++tot;
Tree[tot].key = rand();
Tree[tot].val = val;
Tree[tot].sz = 1;
Tree[tot].sum = val;
Tree[tot].l = Tree[tot].r = 0;
}
void update(int now){
int left = Tree[now].l;
int right = Tree[now].r;
Tree[now].sum = Tree[left].sum + Tree[right].sum + Tree[now].val;
Tree[now].sz = Tree[left].sz + Tree[right].sz + 1;
}
void split_s(int now, int &a, int &b, ll sum){
if(now == 0){
a = b = 0;
return;
}
if(Tree[now].sum - Tree[Tree[now].r].sum <= sum){
a = now;
split_s(Tree[now].r, Tree[now].r, b, sum - (Tree[now].sum - Tree[Tree[now].r].sum));
}else{
b = now;
split_s(Tree[now].l, a, Tree[now].l, sum);
}
update(now);
}
void split_v(int now, int &a, int &b, ll val){
if(now == 0){
a = b = 0;
return;
}
if(Tree[now].val <= val){
a = now;
split_v(Tree[now].r, Tree[now].r, b, val);
}else{
b = now;
split_v(Tree[now].l, a, Tree[now].l, val);
}
update(now);
}
void merge(int &now, int a, int b){
if(a == 0 || b == 0){
now = a+b;
return;
}
if(Tree[a].key <= Tree[b].key){
now = a;
merge(Tree[now].r, Tree[a].r, b);
}else{
now = b;
merge(Tree[now].l, a, Tree[b].l);
}
update(now);
}
ll t, n, m;
int main() {
//ios::sync_with_stdio(false);
scanf("%lld", &t);
ll tmp;
srand(time(nullptr));
int x, y, root;
while(t--){
scanf("%lld%lld",&n,&m);
tot = root = 0;
rep(i, 1, n){
scan_d(tmp);
x = y = 0;
split_s(root, x, y, m-tmp);
printf("%lld ", (ll)i-Tree[x].sz-1);
merge(root, x, y);
split_v(root, x, y, tmp);
add(tmp);
merge(root, x, tot);
merge(root, root, y);
}
putchar('\n');
}
return 0;
}
/*
9 2
5 3 2 1 4 2 1 4 6
*/