【HDU6609】Find the answer【线段树】

题目大意:给你一个序列,对于每个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【线段树】的更多相关文章
- 2019杭电多校第三场hdu6609 Find the answer(线段树)
Find the answer 题目传送门 解题思路 要想变0的个数最少,显然是优先把大的变成0.所以离散化,建立一颗权值线段树,维护区间和与区间元素数量,假设至少减去k才能满足条件,查询大于等于k的 ...
- [2019杭电多校第三场][hdu6609]Find the answer(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6609 大致题意是求出每个位置i最小需要将几个位置j变为0(j<i),使得$\sum_{j=1}^ ...
- hdu多校第三场 1007 (hdu6609) Find the answer 线段树
题意: 给定一组数,共n个,第i次把第i个数扔进来,要求你删掉前i-1个数中的一些(不许删掉刚加进来这个数),使得前i个数相加的和小于m.问你对于每个i,最少需要删掉几个数字. 题解: 肯定是优先删大 ...
- snnu(1110) 传输网络 (并查集+路径压缩+离线操作 || 线段树)
1110: 传输网络 Time Limit: 3 Sec Memory Limit: 512 MBSubmit: 43 Solved: 18[Submit][Status][Web Board] ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
- 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 ...
- 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 ...
- 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 ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
随机推荐
- RabbitMq(7)消息延时推送
应用场景 目前常见的应用软件都有消息的延迟推送的影子,应用也极为广泛,例如: 淘宝七天自动确认收货.在我们签收商品后,物流系统会在七天后延时发送一个消息给支付系统,通知支付系统将款打给商家,这个过程持 ...
- Learn Python the hard way, ex35 分支和函数
#!/usr/bin/python #coding:utf-8 from sys import exit def gold_room(): print "this room is full ...
- 使用TensorFlow的基本步骤
学习任务 学习使用TensorFlow,并以california的1990年的人口普查中的城市街区的房屋价值中位数作为预测目标,使用均方根误差(RMSE)评估模型的准确率,并通过调整超参数提高模型的准 ...
- SQL查询返回去除重复数据的结果集
方法一: select * from tablename where id in (select id from tablename group by id havin ...
- Test Case Design Method - OATS
[转载] OATS:即Orthogonal Array Testing Strategy,正交表测试策略. 1 OATS的概念: 次数(Runs):简单的说,就是次数是多少,就有多少个用例. ...
- jmeter对响应数据做断言
单独校验某个接口中的某个字段时,断言就相当于检查点 添加http请求,输入路径url
- word中迅速将表格一分为二 拆分表格快捷键ctrl+shift+enter 重复上一个命令快捷键f4
这里说的是将一个表格拆分为两个表格 选择要拆分的行,快捷键ctrl+shift+enter,就拆分为两个表格了,是不是很快! 在多个表格需要拆分的时候,做一次这样的操作,然后不停的移动.F4,就可以了 ...
- 【洛谷p2089】 烤鸡
烤鸡[题目链接] 感觉我超废 关于算法:有很多很多算法吧,但自我感觉最重要的是递归的算法: SOLUTION: 首先忍不住要吐槽这个神仙数据: 嗯???定睛一看,它这数据范围莫不是白瞎了,因为每种配料 ...
- [Codeforces 163D]Large Refrigerator (DFS+剪枝)
[Codeforces 163D]Large Refrigerator (DFS+剪枝) 题面 已知一个长方体的体积为V,三边长a,b,c均为正整数,求长方体的最小表面积S V以质因数分解的形式给出 ...
- 搞定Oracle SCN -system change number
SCN是Oracle的内部时钟,用来反映数据库中所有变化,在运行过程中不断更新.SCN种类包括: (1)系统当前SCN (2)Checkpoint SCN ...