题目大意:给你一个序列,对于每个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. pycharm常用快捷键 --- keymap==ecplise

    F5 进入函数逐行调试 F6 执行完一行代码 F8 执行到下一个断点处 ctrl + d    --删除一行代码 ALT + 上/下 箭头   代码上下移动 ALT + 左/右 箭头    左--返回 ...

  2. pojPseudoprime numbers (快速幂)

    Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...

  3. Support Vector Machine(4):SMO算法

    经过上一篇的推导,我们的优化问题已经化为了如下等价形式: 我们在不考虑任何约束条件的情况下去想这个minimize的问题,可以将其抽象为: SMO算法的思想是,每次迭代,只改变一个参数,而将n-1个参 ...

  4. vmware内部错误

    今天不知道怎么回事 wmware workstation重启的时候总是报内部错误 差点重装了 幸亏找到了这个 原来只要以管理员的身份运行vwware就ok了 http://www.xiaoluobok ...

  5. 希希敬敬对Alpha阶段测试报告

    已经完成的功能:1 GUI界面效果已经达到了设计要求,经过review代码完成度较好,GUI.PY代码可以使用,完成了“贴吧名字关键字与URL关联”. 2 能够实现"贴吧名字关键字与URL关 ...

  6. Struts2之处理结果集

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...

  7. for in 和for of的区别

    for in 和for of的区别:https://www.jianshu.com/p/c43f418d6bf0 1 遍历数组通常用for循环 ES5的话也可以使用forEach,ES5具有遍历数组功 ...

  8. java_第一年_JavaWeb(7)

    JSP执行过程 客户端发出请求访问JSP文件 JSP Container将要访问的JSP文件转译为Servlet的源代码(转译时期),并将其编译成.class文件(编译时期): 执行编译后的.clas ...

  9. ac自动机(tree+kmp模板)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  10. P4542 [ZJOI2011]营救皮卡丘(Floyd+网络流)

    P4542 [ZJOI2011]营救皮卡丘 乍一看似乎没啥题相似的 仔细一看,$N<=150$ 边又是双向边,似乎可以用Floyd搞   先跑一遍Floyd处理出$dis[i][j]$ 注意到走 ...