杭电2019多校第三场 HDU-6609 Find the answer(权值线段树+离散化)

传送门

Find the answer

Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1537    Accepted Submission(s): 488


 

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

 思路:

要想使删掉的个数尽可能的少,就是使保留下来的数尽可能的多,我们能留下的数的和最大为m-a[i]。那么题目转化为用最多的权值线段树中的数凑出m-a[i]这个数。那么我们凑的时候,如果左子树上的数的和已经够用,那么肯定只用左子树上的数;否则,我们肯定要全用左子树上的数+右子树凑出m-a[i]-左子树上数的和。注意当递归到叶节点时,那么表明我们只能用这个数去凑val,取min(tree[cur].num,val/b[tree[cur].l])即可。查询完再更新个数。

代码如下:

#include<iostream>
#include <algorithm>
#include <math.h>

#define ll long long
using namespace std;
const int N=200010;
int a[N],b[N];
struct node
{
    int l,r,num;
    ll val;
}tree[N<<2];
void build(int l,int r,int o)
{
    tree[o].l=l;
    tree[o].r=r;
    tree[o].num=0;
    tree[o].val=0;
    if(l==r)
        return ;
    int m=(l+r)>>1;
    build(l,m,o<<1);
    build(m+1,r,o<<1|1);
    return ;
}
int q(int o,int val)
{
    if(tree[o].val<=val)
        return tree[o].num;
    if(tree[o].l==tree[o].r)
        return min(tree[o].num,val/b[tree[o].l]);
    if(val<=tree[o<<1].val)
        return q(o<<1,val);
    else
        return tree[o<<1].num+q(o<<1|1,val-tree[o<<1].val);
}
void update(int o,int val)
{
    if(tree[o].r==tree[o].l)
    {
        tree[o].num++;
        tree[o].val+=b[val];
        return;
    }

    if(val<=tree[o<<1].r)update(o<<1,val);
    else update(o<<1|1,val);

    tree[o].num=tree[o<<1].num+tree[o<<1|1].num;
    tree[o].val=tree[o<<1].val+tree[o<<1|1].val;
    return ;
}
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            cin>>a[i],b[i]=a[i];
        sort(b+1,b+1+n);
        int nn=unique(b+1,b+1+n)-(b+1);
        build(1,nn,1);

        for(int i=1;i<=n;i++)
        {
            cout<<i-1-q(1,m-a[i])<<" ";
            int pos=lower_bound(b+1,b+1+n,a[i])-b;
            update(1,pos);
        }
        cout<<endl;
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值