【HDOJ6609】Find the answer(线段树)
题意:给定一个n个正整数的数列,第i项为w[i],对于每个i,你要从[1,i-1]中选择一些变成0,使得变化后[1,i]的总和小于m,每次询问最少要变几个
n<=2e5,m<=1e9,1<=w[i]<=m
思路:显然每次贪心删最大的,直接开权值线段树,每次询问就在直接树上二分
开始交了几发TLE+MLE,是没有离散化的锅,把w[i]离散化就行
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> Pll;
typedef vector<int> VI;
#define N 210000
#define M 1100000
#define fi first
#define se second
#define MP make_pair
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define lowbit(x) x&(-x)
#define Rand (rand()*(1<<16)+rand())
#define id(x) ((x)<=B?(x):m-n/(x)+1)
#define ls p<<1
#define rs p<<1|1 const ll MOD=,inv2=(MOD+)/;
double eps=1e-;
ll INF=1e14; struct node
{
int num;
ll sum;
}t[N<<]; struct arr
{
int x,id;
}a[N]; bool cmp(arr a,arr b)
{
return a.x<b.x;
} int b[N],c[N],q[N]; int cnt,root,n,m; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} void build(int l,int r,int p)
{
t[p].num=t[p].sum=;
if(l==r) return;
int mid=(l+r)>>;
build(l,mid,ls);
build(mid+,r,rs);
} int query(int l,int r,ll s,int p)
{
if(p==) return ;
if(l==r)
{
if(s<=m) return ;
ll t=(s-m)/c[l];
if((s-m)%c[l]) t++;
return t;
}
int mid=(l+r)>>;
ll tmp=t[rs].sum;
if(s-tmp<=m) return query(mid+,r,s,rs);
else
{
return t[rs].num+query(l,mid,s-tmp,ls);
}
} void update(int l,int r,int x,int p)
{
t[p].num++;
t[p].sum+=c[x];
if(l==r) return;
int mid=(l+r)>>;
if(x<=mid) update(l,mid,x,ls);
else update(mid+,r,x,rs);
} int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
int cas=read();
cnt=;
while(cas--)
{
n=read(),m=read();
rep(i,,n)
{
a[i].x=read();
a[i].id=i;
}
rep(i,,n) q[i]=a[i].x;
sort(a+,a+n+,cmp);
b[a[].id]=; c[]=a[].x;
int t=;
rep(i,,n)
if(a[i].x==a[i-].x) b[a[i].id]=t;
else
{
t++;
b[a[i].id]=t;
c[t]=a[i].x;
}
build(,t,);
ll s=;
rep(i,,n)
{
s+=q[i];
printf("%d ",query(,t,s,));
update(,t,b[i],);
}
//printf("cnt=%d\n",cnt);
printf("\n");
}
return ;
}
【HDOJ6609】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 ...
随机推荐
- Linux运维工程师前景
什么是Linux运维 如果我们是一辆高速行驶在高速公路上的汽车,那运维工程师就是司机兼维修工,这个司机可不简单,有时需要在高速行驶过程中更换轮胎.并根据道路情况换档位.当汽车速度越来越快时,汽车本身不 ...
- 第 2 章 前端基础之CSS
一.CSS语法 CSS规则由两个主要的部分构成:选择器,以及一条或多条声明. ''' selector { property: value; property: value; ... property ...
- 标准标签库JSTL(JSP Standard Tag Library)
1, 核心标签(最常用, 最重要的) 表达式控制标签 out 输出常量 value---直接赋值 输出变量 default---默认值 escapeXml---控制转义字符(默认为true, 如果需要 ...
- Java语言的特点与工作原理
Java语言的特点 1.简单性 Java语言与我们常听到的C++语言很像,但是没有C++那么繁琐.因为Java就是在C++之上设计出来的,设计者把C++的一些特性去掉了,这些特性在实际开发中,程序员也 ...
- LibreOJ 6177 题解(状压DP)
题面 传送门 分析 刚看到这道题时想的是跟最短哈密顿路类似的二进制状压DP,先用floyd处理距离 但是此题用二进制不够,应该用三进制 0,1,2分别表示未送,正在送,已送完 dp[s][i]表示当前 ...
- Python : Data Encapsulation
Python : Data Encapsulation The following table shows the different behaviour: Name Notation Behavio ...
- hdu1423 最长公共上升子序列
题目传送门 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- spring bean-- autowired的正确用法
这两天用idea写spring注入的时候每一次 @Autowired Worker worker; 都会报黄,用过这个ide的都知道,说明你代码需要重构了. 然后提示的信息是 Spring Team ...
- SpringCloud-Eureka-服务注册是如何发起的
原文:https://xsxy007.github.io Spring Cloud环境下,服务提供者和消费者启动后都会将自身注册到Eureka 一.将服务注册到Eureka 一个SpringBoot应 ...
- IIS 添加二级应用程序
1.在原有的站点上添加虚拟目录 2.转换成应用程序