【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 ...
随机推荐
- ruby的next if boolean
next相当于continue
- php中的构造函数与析构函数
PHP面向对象——构造函数.析构函数 __construct.__destruct__construct 构造方法,当一个对象创建时调用此方法,使用此方法的好处是:可以使构造方法有一个独一无二的名称, ...
- EntityFramework经典数据访问层基类——增删改查
namespace StudentSys.DAL { public class BaseService<T>:IDisposable where T:BaseEntity,new() { ...
- Bootstrap 学习笔记7 模态框插件
网站弹出框使用: 基本使用: <!-- 模态框的声明 --> <div class="modal" id="myModal" tabindex ...
- python pip报错pip._ vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
AttributeError: module 'pip' has no attribute 'main报错 找到安装目录下 helpers/packaging_tool.py文件,找到如下代码: de ...
- vue配置域名访问
vue配置域名访问其实很简单,新建一个vue的项目,不废话,直接上截图 红色框住的两行就是设置访问的域名以及端口,默认是host: localhost port: 8080 像这样修改之后,重启项目, ...
- Codeforces 1091C (数学)
题面 传送门 分析 假设k是固定的,那访问到的节点编号就是\(1+(a·k \mod n )\),其中a为正整数. 通过找规律不难发现会出现循环. 通过题目中的图片我们不难发现 只有k=1,2,3,6 ...
- emacs配置&博客界面源代码
emacs配置 如果想要考场简单配置也可以去下面看,需要别的考场配置可以自己在下面比较全的里面找 考试备忘录(有新的就会更的...) By Junlier (global-set-key [f9] ' ...
- python中序列类型
Python中的序列类型使用 元组类型 一旦被创建,就无法被修改. 创建 使用()或者tuple()创建 creater1=('cat', 'dog', 'tiger', 'human') creat ...
- cookie和session的联系与区别
Cookie 当你在浏览网站时,WEB服务器会先送一小小的资料放在你的计算机上,Cookie会帮你在网站上所打的文字或是一些选择都记录下来.当你下次再光临同一个网站时,WEB服务器会先看看有没有它上次 ...