HDU-6609 Find the answer (2019杭电多校第三场1007)

题目链接
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

题意: 长度为n的序列,对于第i个数,需要从1~i-1中选取多个数,使得它们的和sum+ai <=m.

思路: 对于 ai 来说,我们只需要在1~i-1中从小到大枚举,使得他们的和sum <= ans(ans = m - ai)即可。但是,一一枚举必然是会T的,所以可以用线段树优化。
对数组离散化,按照离散化后的数组建立线段树,每个节点存区间和和当前节点插入数字个数,每次查询左儿子是否小于ans;若小于跟新ans = ans-sum,反之进入左儿子,并记录个数。

总结反思: 之前自己每次进入左儿子后再判断当前节点是否达到ans,然后再进入右儿子,被T自闭了。结束后看了一下朋友代码,改了区间查询方式过了。想了一下,这样好像直接节省了一半时间,自闭…

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson (rt<<1)
#define rson ((rt<<1)|1)
/**********************************************Head-----Template****************************************/
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b){ll gg=gcd(a,b);a/=gg;if(a<=LLONG_MAX/b) return a*b;return LLONG_MAX;}
/********************************Head----Temlate**********************************************/
const int MAXN = 250100;
struct TREE
{
    ll sum;
    int num;
}tree[MAXN<<2];
int n,m,num,sum;
int a[MAXN],t[MAXN];
map<int,int>mp;
void init()
{
    for(int i=0;i<=n*4+10;i++) tree[i].sum = tree[i].num = 0;
    mp.clear();
}

void update(int rt,int L,int R,int x,int v)
{
    tree[rt].sum += v;
    tree[rt].num++;
    if(L == R)  return ;
    int Mid = (L+R)>>1;
    if(x<=Mid)  update(lson,L,Mid,x,v);
    else update(rson,Mid+1,R,x,v);
}

void query(int rt,int L,int R)
{
    if(L == R){
        if(tree[rt].sum > sum){
            int t = min((int)sum/mp[L],tree[rt].num);
            sum -= t * mp[L];
            num += t;
        }
        else{
            sum -= tree[rt].sum;
            num += tree[rt].num;
        }
        return ;
    }
    int Mid = (L+R)>>1;
    if(tree[lson].sum>sum)  query(lson,L,Mid);
    else{
        sum-=tree[lson].sum;  num += tree[lson].num;
        query(rson,Mid+1,R);
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    for(int kk=1;kk<=T;kk++){
        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=n;i++)   read(a[i]),t[i] = a[i];
        sort(t+1,t+1+n);
        int k=1;
        for(int i=2;i<=n;i++){
            if(t[k]!=t[i])  t[++k]=t[i];
        }
        for(int i=1;i<=n;i++){
            sum=m-a[i];num=0;
            sum=max(sum,0);
            int temp = lower_bound(t+1,t+1+k,a[i])-t;
            mp[temp] = a[i];
            if(tree[1].sum <= sum)  write(0);
            else{
                query(1,1,k);
                write(i-num-1);
            }
            putchar(' ');
            update(1,1,k,temp,a[i]);
        }putchar('\n');
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值