题目大意:给你一个序列,对于每个i,你可以选择1~i-1中任意多的数并将它删去,剩余的数(包括i)∑≤m,问对于每个i最少删几个数可以达到要求

题解:

考虑朴素的思想,对于每个i,我只需要删去最大的若干个使得∑≤m即可,时间复杂度O(n^2)

显然不可接受,考虑优化

显然可以看出,因为只需要删去最大的若干数,于是想到前K大,于是很自然想到用线段树

先离散化,只需要记录这个数是第几大,之后线段树维护区间和,每新加入一个点时加入这个点第几大的下标

询问时在线段树上二分出前K大即可,时间复杂度O(nlogn)

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#define ll long long
using namespace std;
int TT,n;
int ans[];
ll m;
struct node
{
ll v;
int bh,rk;
}a[];
bool cmp(const node &T1,const node &T2){return T1.v<T2.v;}
bool cmp2(const node &T1,const node &T2){return T1.bh<T2.bh;}
ll sum[*],cnt[*];
int ask(int l,int r,ll v,int pos,ll tot)
{
if(sum[pos]+tot<=v)return cnt[pos];
else
{
int mid=l+r>>;
int t=ask(l,mid,v,pos<<,tot);
if(t==cnt[pos<<])t+=ask(mid+,r,v,pos<<|,tot+sum[pos<<]);
return t;
}
}
void insert(int l,int r,ll v,int p,int pos)
{
if(l==r && l==p)
{
sum[pos]=v;cnt[pos]=;
return;
}
int mid=l+r>>;
if(p<=mid)insert(l,mid,v,p,pos<<);
else insert(mid+,r,v,p,pos<<|);
sum[pos]=sum[pos<<]+sum[pos<<|];
cnt[pos]=cnt[pos<<]+cnt[pos<<|];
}
int main()
{
scanf("%d",&TT);
while(TT--)
{
memset(sum,,sizeof(sum));
memset(cnt,,sizeof(cnt));
memset(ans,,sizeof(ans));
scanf("%d%lld",&n,&m);
for(int i=;i<=n;i++){scanf("%lld",&a[i].v);a[i].bh=i;}
sort(a+,a++n,cmp);
for(int i=;i<=n;i++)a[i].rk=i;
sort(a+,a++n,cmp2);
for(int i=;i<=n;i++)
{
int t=ask(,n,m-a[i].v,,);
ans[i]=i--t;
insert(,n,a[i].v,a[i].rk,);
}
for(int i=;i<=n;i++)printf("%d ",ans[i]);
printf("\n");
}
return ;
}

心得:考场上很自然的想到,说明该部分知识掌握不错,继续加油

【HDU6609】Find the answer【线段树】的更多相关文章

  1. 2019杭电多校第三场hdu6609 Find the answer(线段树)

    Find the answer 题目传送门 解题思路 要想变0的个数最少,显然是优先把大的变成0.所以离散化,建立一颗权值线段树,维护区间和与区间元素数量,假设至少减去k才能满足条件,查询大于等于k的 ...

  2. [2019杭电多校第三场][hdu6609]Find the answer(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6609 大致题意是求出每个位置i最小需要将几个位置j变为0(j<i),使得$\sum_{j=1}^ ...

  3. hdu多校第三场 1007 (hdu6609) Find the answer 线段树

    题意: 给定一组数,共n个,第i次把第i个数扔进来,要求你删掉前i-1个数中的一些(不许删掉刚加进来这个数),使得前i个数相加的和小于m.问你对于每个i,最少需要删掉几个数字. 题解: 肯定是优先删大 ...

  4. snnu(1110) 传输网络 (并查集+路径压缩+离线操作 || 线段树)

    1110: 传输网络 Time Limit: 3 Sec  Memory Limit: 512 MBSubmit: 43  Solved: 18[Submit][Status][Web Board] ...

  5. GSS4 2713. Can you answer these queries IV 线段树

    GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...

  6. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

  7. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

  8. spoj gss2 : Can you answer these queries II 离线&&线段树

    1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...

  9. SPOJ GSS1_Can you answer these queries I(线段树区间合并)

    SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...

随机推荐

  1. Eclipse总是自动跳到ThreadPoolExecutor

    出现这种状况是因为Eclipse默认开启挂起未捕获的异常(Suspend execution on uncaught exceptions),只要关闭此项就可以了.解决方法:在eclipse中选择Wi ...

  2. 2019/12/22 TZOJ

    4986 Team Formation http://www.tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=498 ...

  3. iframe父页面和子页面高度自适应

    父页HTML: <iframe  id="mainframe" name="mainframe"  style="width:100%;&quo ...

  4. 微信小程序列表时间戳转换

    第一步先写js   随便命名为times.js function toDate(number){     var n=number * 1000;     var date = new Date(n) ...

  5. docker hub 本地镜像登录

    docker的登录信息存放在home目录下的.docker文件夹下,查看 cat ~/.docker/config.json { "auths": { "gcyimgs. ...

  6. resultType和resultMap一对一查询小结

    resultType和resultMap一对一查询小结 SELECT orders.*, USER .username,USER.birthday,USER.sex,USER.address FROM ...

  7. CentOS vim的使用

    安装vim工具 [root@bogon ~]# yum install -y vim-enhanced 卸载vim工具 [root@bogon ~]# yum remove -y vim* vim常用 ...

  8. 学习servlet时出现的一些问题

    此篇用来记录学习servlet时遇到的一些问题,谨防以后再犯. 问题1.导入的web项目,servlet中导入的包名报错. (1)缺少相关包,推荐一个网站下载jar包很方便http://mvnrepo ...

  9. 【问题解决方案】关于Python中的语句 ' %matplotlib inline '

    跟进小项目#GirlsInAI#-可视化时遇到的语句,之前没有遇到过 在Stack Overflow上看到了一个解释: IPython有一组预定义的"魔术函数",您可以使用命令行样 ...

  10. Python-编码这趟浑水

    最近听Alex讲到python编码,还特意用博客讲解,觉得问题严重了,于是翻看各种博客,先简单的对编码错误做一个总结,其他的后续慢慢补上,还得上班.还得学习.还得写博客?感觉有点吃不消了.各位大神不喜 ...