题目大意:给你一个序列,对于每个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. java 为啥可打印date

    打印一个对象的时候,会打印出它的toString方法的返回值,Date重写了toString方法.

  2. druid spring监控配置

    方法一: <bean id="seckillServiceImpl" class="org.seckill.service.impl.SeckillServiceI ...

  3. xenserver添加静态路由

    xe network-list name-label= xe network-param-set uuid=48a64512-69e8-6534-f276-8d0c4555f946 other-con ...

  4. postgresql中实现按周统计详解

    SELECT EXTRACT(DOW FROM CURRENT_DATE);   执行结果如下. 这个SQL语句的意思就是计算当前日期是一周中的第几天. EXTRACT(DOW FROM CURREN ...

  5. python常用的时间方法

    from time import strftime setTime = strftime("%Y_%m_%d_%H_%M_%S", time.localtime()) // In ...

  6. struts2 基础学习

      Struts 2是在WebWork2基础发展而来的. 注意:struts 2和struts 1在代码风格上几乎不一样. Struts 2 相比Struts 1的优点: 1.在软件设计上Struts ...

  7. XSLT学习(九)通过JavaScript转化xml

    如果您的浏览器支持 XSLT,那么在浏览器中它可被用来将文档转换为 XHTML. JavaScript 解决方案 在前面的章节,我们已向您讲解如何使用 XSLT 将某个 XML 文档转换为 XHTML ...

  8. python中序列类型

    Python中的序列类型使用 元组类型 一旦被创建,就无法被修改. 创建 使用()或者tuple()创建 creater1=('cat', 'dog', 'tiger', 'human') creat ...

  9. Thymeleaf入门——入门与基本概述

    https://www.cnblogs.com/jiangbei/p/8462294.html 一.概述 1.是什么 简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类 ...

  10. python常用函数 B

    B bin(int) 将整数x转换为二进制字符串,如果x不为Python中int类型,x必须包含方法__index__()并且返回值为integer. 例子: