HDU 6609 (2019杭电多校三 1007)Find the answer (权值线段树 + 二分 查询前 k 大的和)

Find the answer

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

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

 

题意:给一个数组,对每一个下标 i ,问至少删除 1 ~ i - 1 中多少个数才能使得 1 到 i 的和 <= m .. (和这个题如出一辙:codeforces 1185 C2 题解

思路:显然要删除最少个数,肯定是从大到小进行删除,关键就是怎么确定从大到小的数的和,这就是需要解决的问题。那么我是用权值线段树来进行维护的如整个区间第 k 大一样,然后查询的时候求前 k 大的数的和,然后对于每一个 i ,直接二分删除多少个数即可。

Code:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define INT(t) int t; scanf("%d",&t)

using namespace std;

const int maxn = 2e5 + 10;
LL sum[maxn << 2];
LL C[maxn << 2];
int t[maxn],K[maxn];

void update(int l,int r,int p,int i){
    if(l == r){
        ++ sum[i];
        C[i] = C[i] + K[p];
        return ;
    }
    int mid = (l + r) >> 1;
    if(p <= mid) update(l,mid,p,i << 1);
    if(p > mid) update(mid + 1,r,p,i << 1 | 1);
    sum[i] = sum[i << 1] + sum[i << 1 | 1];
    C[i] = C[i << 1] + C[i << 1 | 1];
}

LL query(int l,int r,int k,int i){
    if(l == r) return k >= sum[i] ? C[i] : C[i] / sum[i] * k;
    LL ans = 0;
    int mid = (l + r) >> 1;
    if(sum[i << 1 | 1] >= k){
        ans += query(mid + 1,r,k,i << 1 | 1);
    }
    else{
        ans += C[i << 1 | 1];
        ans += query(l,mid,k - sum[i << 1 | 1],i << 1);
    }
    return ans;
}

int en;
int getId(int x){
    return lower_bound(K + 1,K + 1 + en,x) - K;
}

int main() {
    int T; scanf("%d",&T);
    int n,M;
    while(T --){
        scanf("%d%d",&n,&M);
        rep(i,0,(n + 5) * 4) sum[i] = C[i] = 0;

        for(int i = 1;i <= n;++ i){
            scanf("%d",&t[i]);
            K[i] = t[i];
        }
        sort(K + 1,K + 1 + n);
        en = unique(K + 1,K + 1 + n) - K - 1;
        LL sum1 = 0;
        for(int i = 1;i <= n;++ i){
            if(i == 1){
                if(t[i] > M) printf("1 ");
                else printf("0 ");
                update(1,en,getId(t[i]),1);
                sum1 = sum1 + t[i];
                continue;
            }
            int l = 0,r = i - 1;
            int ans = r;
            while(l <= r){
                int mid = (l + r) >> 1;
                if(sum1 - query(1,en,mid,1) + t[i] <= M){
                    ans = min(ans,mid);
                    r = mid - 1;
                }
                else l = mid + 1;
            }
            printf("%d ",ans);
            update(1,en,getId(t[i]),1);
            sum1 = sum1 + t[i];
        }
        printf("\n");
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值