P3466 [POI2008]KLO-Building blocks
题目
luogu
csdn好像限制了展开博客次数,真的好xx
思路
显然一段区间内的值一定是他的中位数
少一点比多一点好
然后就可以枚举区间了
区间答案为
val[mid]-小于val[mid]的+大于val[mid]-val[mid]的所有值
就是size[x]val[mid] - tot_l + tot_r - size[y]val[mid]
然后你随便写个treap(fhq)就好了
错误
一开始siz[x]直接写成mid
但这是错误的,因为如果有多个数字都等于中位数,那size[x]!=mid
记得开ll
代码
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define ll long long
using namespace std;
const int maxn=100001;
int read() {
int x=0,f=1;char s=getchar();
for(;s<'0'||s>'9';s=getchar()) if(s=='-') f=-1;
for(;s>='0'&&s<='9';s=getchar()) x=x*10+s-'0';
return x*f;
}
int ch[maxn][2],val[maxn],pri[maxn],siz[maxn],sz;
ll tot[maxn];
void update(int x) {
siz[x]=1+siz[ch[x][0]]+siz[ch[x][1]];
tot[x]=val[x]+tot[ch[x][0]]+tot[ch[x][1]];
}
int new_node(int v) {
siz[++sz]=1;val[sz]=v;pri[sz]=rand();tot[sz]=v;
return sz;
}
int merge(int x,int y) {
if(!x||!y) return x+y;
if(pri[x]<pri[y]) {
ch[x][1]=merge(ch[x][1],y);
update(x);
return x;
} else {
ch[y][0]=merge(x,ch[y][0]);
update(y);
return y;
}
}
void split(int now,int k,int &x,int &y) {
if(!now) x=y=0;
else {
if(val[now]<=k)
x=now,split(ch[now][1],k,ch[now][1],y);
else
y=now,split(ch[now][0],k,x,ch[now][0]);
update(now);
}
}
int k_th(int now,int k) {
while(1) {
if(k==siz[ch[now][0]]+1)return now;
if(k<=siz[ch[now][0]]) now=ch[now][0];
else k-=siz[ch[now][0]]+1,now=ch[now][1];
}
}
int root,n,k,a[maxn];
void insert(int a) {
int x,y;
split(root,a,x,y);
root=merge(merge(x,new_node(a)),y);
}
void delet(int a) {
int x,y,z;
split(root,a,x,z);
split(x,a-1,x,y);
y=merge(ch[y][0],ch[y][1]);
root=merge(merge(x,y),z);
}
int main() {
srand(time(NULL));
n=read(),k=read();
int mid=(k+1)>>1;
pair<ll,pair<int,int> > pp;
pp.first=0x3f3f3f3f3f3f3f3fLL;
FOR(i,1,n) {
a[i]=read();
if(i<k) insert(a[i]);
else {
insert(a[i]);
int x,y,get=k_th(root,mid);
split(root,val[get],x,y);
if(pp.first > ((ll)siz[x]*val[get]-(ll)tot[x]+(ll)tot[y]-(ll)siz[y]*val[get])) {
pp.first=(ll)siz[x]*val[get]-(ll)tot[x]+(ll)tot[y]-(ll)siz[y]*val[get];
pp.second.first=i;
pp.second.second=val[get];
}
root=merge(x,y);
delet(a[i-k+1]);
}
}
cout<<pp.first<<"\n";
FOR(i,1,n) {
if(i<=pp.second.first&&i>=pp.second.first-k+1)
cout<<pp.second.second<<"\n";
else
cout<<a[i]<<"\n";
}
return 0;
}
P3466 [POI2008]KLO-Building blocks的更多相关文章
- Intel® Threading Building Blocks (Intel® TBB) Developer Guide 中文 Parallelizing Data Flow and Dependence Graphs并行化data flow和依赖图
https://www.threadingbuildingblocks.org/docs/help/index.htm Parallelizing Data Flow and Dependency G ...
- bc.34.B.Building Blocks(贪心)
Building Blocks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- DTD - XML Building Blocks
The main building blocks of both XML and HTML documents are elements. The Building Blocks of XML Doc ...
- 企业架构研究总结(35)——TOGAF架构内容框架之构建块(Building Blocks)
之前忙于搬家移居,无暇顾及博客,今天终于得闲继续我的“政治课”了,希望之后至少能够补完TOGAF方面的内容.从前面文章可以看出,笔者并无太多能力和机会对TOGAF进行理论和实际的联系,仅可对标准的文本 ...
- TOGAF架构内容框架之构建块(Building Blocks)
TOGAF架构内容框架之构建块(Building Blocks) 之前忙于搬家移居,无暇顾及博客,今天终于得闲继续我的“政治课”了,希望之后至少能够补完TOGAF方面的内容.从前面文章可以看出,笔者并 ...
- HDU—— 5159 Building Blocks
Problem Description After enjoying the movie,LeLe went home alone. LeLe decided to build blocks. LeL ...
- [翻译]Review——How JavaScript works:The building blocks of Web Workers
原文地址:https://blog.sessionstack.com/how-javascript-works-the-building-blocks-of-web-workers-5-cases-w ...
- 四、Implementation: The Building Blocks 实现:构件
四.Implementation: The Building Blocks 实现:构件 This is the essential part of this guide. We will introd ...
- 2.3 Core Building Blocks 核心构件
Core Building Blocks 核心构件 DDD mostly focuses on the Domain & Application Layers and ignores the ...
- 解题:POI2008 Building blocks
题面 显然我们需要考虑每一个区间,而这个问题显然我们都会做,这不就是这道题么,也就是说假如中位数是$mid$,区间和是$sum$,那么代价就是$\sum\limits_{i=l}^r |mid-num ...
随机推荐
- LoadRunner-关联问题(栏目列表较多关联不了想要的id)
新建了课程后之后有很多栏目,每个栏目对应一个partid,但我只想要期中一个. http://*********/course/work/workInfo.action?hwid=1547&c ...
- Github常用命令【转】
本地仓库(local repository) 创建一个本地仓库的流程: 为本地仓库创建一个目录 在目录中执行 git init 对本地仓库所做的改变(例如添加.删除文件等)首先加入到本地仓库的 Ind ...
- C++ 指向数组的指针
如果您对 C++ 指针的概念有所了解,那么就可以开始本章的学习.数组名是一个指向数组中第一个元素的常量指针.因此,在下面的声明中: double balance[50]; balance 是一个指向 ...
- PAT 1015 Reversible Primes[求d进制下的逆][简单]
1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...
- TileMap地图
参考资料: http://8287044.blog.51cto.com/5179921/1045274 TileMap编辑器使用 1.认识TileMap TileMap是一款开源的地图编辑 ...
- myeclipse自定义JSP模板
- liferay中如何获取实例的id和portletId
在Portlet中request分为两种renderRequet和actionRequest而portlet需要取得实例Id的时候都在renderRequest的时候才可以取到,如下例子 Portle ...
- toFixed()与toPrecision()
toFixed(n): 返回一个字符串,代表一个以定点表示法表示的数字. n在0~20之间 var g=1.023; var f=g.toFixed(2); f的值为:1.02, typeof ...
- c语言中strcpy与strlen函数对字符串最后的'\0'的处理
对于strcpy来说,它会把字符串最后的‘\0’一起拷贝 对于strlen来说,它计算字符串长度的时候不会把最后的‘\0’计算进去
- Perl的子程序(二)
在Perl中可以自己创建子程序(Subroutine): 关键字sub,子程序名以及用花括号封闭起来的代码块. sub marine { ... } 子程序名与标量的命名空间是不同的两个部分. 子程 ...