题目

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的更多相关文章

  1. 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 ...

  2. bc.34.B.Building Blocks(贪心)

    Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. DTD - XML Building Blocks

    The main building blocks of both XML and HTML documents are elements. The Building Blocks of XML Doc ...

  4. 企业架构研究总结(35)——TOGAF架构内容框架之构建块(Building Blocks)

    之前忙于搬家移居,无暇顾及博客,今天终于得闲继续我的“政治课”了,希望之后至少能够补完TOGAF方面的内容.从前面文章可以看出,笔者并无太多能力和机会对TOGAF进行理论和实际的联系,仅可对标准的文本 ...

  5. TOGAF架构内容框架之构建块(Building Blocks)

    TOGAF架构内容框架之构建块(Building Blocks) 之前忙于搬家移居,无暇顾及博客,今天终于得闲继续我的“政治课”了,希望之后至少能够补完TOGAF方面的内容.从前面文章可以看出,笔者并 ...

  6. HDU—— 5159 Building Blocks

    Problem Description After enjoying the movie,LeLe went home alone. LeLe decided to build blocks. LeL ...

  7. [翻译]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 ...

  8. 四、Implementation: The Building Blocks 实现:构件

    四.Implementation: The Building Blocks 实现:构件 This is the essential part of this guide. We will introd ...

  9. 2.3 Core Building Blocks 核心构件

    Core Building Blocks 核心构件 DDD mostly focuses on the Domain & Application Layers and ignores the ...

  10. 解题:POI2008 Building blocks

    题面 显然我们需要考虑每一个区间,而这个问题显然我们都会做,这不就是这道题么,也就是说假如中位数是$mid$,区间和是$sum$,那么代价就是$\sum\limits_{i=l}^r |mid-num ...

随机推荐

  1. Python 字典 dict() 函数

    描述 Python 字典 dict() 函数用于创建一个新的字典,用法与 Pyhon 字典 update() 方法相似. 语法 dict() 函数函数语法: dict(key/value) 参数说明: ...

  2. mysql主从服务器的配置

    使用mysql主从复制的好处有: 1.采用主从服务器这种架构,稳定性得以提升.如果主服务器发生故障,我们可以使用从服务器来提供服务. 2.在主从服务器上分开处理用户的请求,可以提升数据处理效率. 3. ...

  3. CF359B Permutation 构造

    正解:构造 解题报告: 这个是传送门! 昂直接讲思路趴?毕竟这种构造题的话除了思路也没什么好说的只要想明白辽还是通常来说难度不大的QwQ 首先提供一个对正解毫无启发的由正解启发而来的想法QAQ 就首先 ...

  4. IIS7启用gzip

    压缩什么 服务器基于文件类型选择压缩什么,但这通常受限于对其进行的配置.很多网站就会压缩其HTML文档.压缩脚本和样式表也是非常值得的,压缩的内容包括XML和JSON在内的任何文本响应,但这里只关注脚 ...

  5. 手机e.pageX和e.pageY无效的原因

    手机端拖拽事件: touchstart事件:当手指触摸屏幕时候触发,即使已经有一个手指放在屏幕上也会触发. touchmove事件:当手指在屏幕上滑动的时候连续地触发.在这个事件发生期间,调用prev ...

  6. 最新版OpenWrt编译教程,解决依赖问题

    Install git , to conveniently download the OpenWrt source code, and build tools to do the cross-comp ...

  7. 如何让dedecms文章点击量增加一定的数值

    用dedecms建站都知道有一个文章点击量这个参数,我们可不可以用这个浏览量做些延伸扩展呢?比如加上一个固定值变成另外一个指标.很多朋友已经想到了,如下图,我们将本文浏览量286设为点击量,加上300 ...

  8. unity3d-编辑器结构

    1.Porject视图 Project视图主要存放游戏中用到的所有资源文件,常见的资源包括: 游戏脚本.预设.材质.动画.自定义字体.纹理.物理材质和GUI皮肤.这些资源需要 赋予Hierarchy视 ...

  9. c++多线程实例

    #include <windows.h> #include <stdio.h> #include <process.h> ; int g_thread_counte ...

  10. Android开发中,系统是如何区分不同的App的(转自csdn)

    转自:http://bbs.csdn.net/topics/391868975?page=1 1判断是否为同一个app,仅仅是包名,签名是保证你的app不被第三方恶意替换.当包名相同,但签名不同时,系 ...