2019 Multi-University Training Contest 3 树状数组上的二分

Find the answer
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

题意:
n个数,有一个上限m,问前i个数的和不大于m的时候需要在前i-1个数中最少删除几个数

分析:
让求的是最少需要删除几个数,那么从暴力的角度来说,肯定是删除的数值越大越好;
因此一开始想想怎么去排序,这样就可以从已排好的序列里面找到最大的一个或者多个数删去,那么每次sort肯定是会超时的,我一开始是用的优先级队列,每次删去的数就不再用了,但是虽然没超时,WA了!!!
确实思路不太合适,那么问题就是怎么求这个前i个数里面最大的那几个序列,然后就学到了一个新的sao操作,将排好序的序列用树状数组去维护(代码中我是用树状数组去维护的从小到大的序列,当然还可以反过来去维护),那么这样就可以去在这个树状数组上面二分前缀和了

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
typedef long long ll;
using namespace std;
const int N=2e5+100;
//cnt数组是用于记录当前下标为i的元素个数有多少
//sum数组是用于记录当前小标为i的元素和是多少
ll a[N],cnt[N<<1],sum[N<<1];
int n;
int d[N];
struct node{
    ll val;
    int id;
    bool operator < (const node & a)const{
        return val < a.val;//从小到大排序
    }
}num[N];
bool cmp(node a,node b){
    return a.val<b.val;
}
int lowbit(int x){
    return x&-x;
}
void add_cnt(int x,int val){
    while(x<=n){
        cnt[x]+=val;
        x+=lowbit(x);
    }
}
void add_sum(int x,ll val){
    while(x<=n){
        sum[x]+=val;
        x+=lowbit(x);
    }
}
ll query_sum(int x){
    ll ans=0;
    while(x){
        ans+=sum[x];
        x-=lowbit(x);
    }
    return ans;
}
int query_cnt(int x){
   int ans=0;
   while(x) {
    ans+=cnt[x];
    x-=lowbit(x);
   }
   return ans;
}
int Fen(ll val){
    int lb=0,ub=n;
    while(ub>=lb){
        int mid=(lb+ub)/2;
        if(query_sum(mid)<=val)
            lb=mid+1;
        else
            ub=mid-1;
    }
    return lb-1;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int T;
    scanf("%d",&T);
    while(T--){
        ll m;
        memset(cnt,0,sizeof cnt);
        memset(sum,0,sizeof sum);
        scanf("%d%lld",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
            num[i].val=a[i];
            num[i].id=i;
        }
        sort(num+1,num+1+n);//根据权值大小排序,进而离散化

        for(int i=1;i<=n;i++){
            d[num[i].id]=i;//得到排完序的序列在新序列中的位置编号
        }
   //在这里我们是进行将建立一颗从小到大的树,这样小的权值就在树状数组的左边,大的在右边
        for(int i=1;i<=n;i++){
            int k=Fen(m-a[i]);//返回需要留下的区间和的右端点
            printf("%d ",i-1-query_cnt(k));//前i-1个中减去需要留下的个数,那么剩下的就是需要去掉的个数
            add_cnt(d[i],1);//向树状数组添加个数
            add_sum(d[i],a[i]);
        }
        printf("\n");
    }
    return 0;
}


当然也可以将序列从大到小去维护

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
typedef long long ll;
using namespace std;
const int N=2e5+100;
//cnt数组是用于记录当前下标为i的元素个数有多少
//sum数组是用于记录当前小标为i的元素和是多少
ll a[N],cnt[N<<1],sum[N<<1];
int n;
int d[N];
struct node{
    ll val;
    int id;
    bool operator < (const node & a)const{
        return val > a.val;//从大到小排序
    }
}num[N];
bool cmp(node a,node b){
    return a.val<b.val;
}
int lowbit(int x){
    return x&-x;
}
void add_cnt(int x,int val){
    while(x<=n){
        cnt[x]+=val;
        x+=lowbit(x);
    }
}
void add_sum(int x,ll val){
    while(x<=n){
        sum[x]+=val;
        x+=lowbit(x);
    }
}
ll query_sum(int x){
    ll ans=0;
    while(x){
        ans+=sum[x];
        x-=lowbit(x);
    }
    return ans;
}
int query_cnt(int x){
   int ans=0;
   while(x) {
    ans+=cnt[x];
    x-=lowbit(x);
   }
   return ans;
}
int Fen(ll val){
    int lb=0,ub=n;
    while(ub>=lb){
        int mid=(lb+ub)/2;
        if(query_sum(mid)>=val)
            ub=mid-1;
        else
            lb=mid+1;
    }
    return ub+1;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int T;
    scanf("%d",&T);
    while(T--){
        ll m;
        memset(cnt,0,sizeof cnt);
        memset(sum,0,sizeof sum);
        scanf("%d%lld",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
            num[i].val=a[i];
            num[i].id=i;
        }
        sort(num+1,num+1+n);//根据权值大小排序,进而离散化

        for(int i=1;i<=n;i++){
            d[num[i].id]=i;//得到排完序的序列在新序列中的位置编号
        }
        ll sum=0;
        for(int i=1;i<=n;i++){
            sum+=a[i];
            int k=Fen(sum-m);//sum-m就是需要去减去的和,因为树状数组是从大到小的维护
            printf("%d ",query_cnt(k));//前i-1个中找到有多少个
            add_cnt(d[i],1);//向树状数组添加个数
            add_sum(d[i],a[i]);
        }
        printf("\n");
    }
    return 0;
}


这题是运用了树状数组的求和快的特点,这样就实现了二分求和的快速和求个数的快速,第一种方法其实算是用反向的解题思路来的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值