题目

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. Chrome百度不显示中文字体

    chrome://flags/ 启用DirectWrite 关闭就可以了

  2. oracle(四) 常用语句

    1.分页 select t2.* from (select rownum row, t1.*  from your_table where rownum < ?) t2 where t2.row ...

  3. [py][mx]operation模型设计

    用户表 UserMesasage 用户消息 UserFavorite 用户收藏 UserAsk 用户咨询 UserCourse 用户学习的课程 CourseComments 用户评论 收藏有3种类型 ...

  4. linq to sql 左联接出错,未将对象引用设置到实例

    var result = from a in model join b in orderDetailModel on a.FoodMenuID equals b.FoodMenuID into g f ...

  5. django的分页器

    Django中分页器的使用 django分页器模块 #分页器 from django.core.paginator import Paginator,EmptyPage,PageNotAnIntege ...

  6. http协议基础(八)请求首部字段

    请求首部字段 定义:请求首部字段是从客户端到服务器发送请求报文中所使用的字段,里面包含了附加信息.客户端信息以及对响应内容相关的优先级等内容 1.Accept 通知服务器用户代理可处理的媒体类型及媒体 ...

  7. entity framework 新增,更新,事务

    protected void Button1_Click(object sender, EventArgs e) { yyEntities _db; _db = new yyEntities(); t ...

  8. 019-centos的yum用法

    1.检测系统是否已经安装过mysql或其依赖:# yum list installed | grep mysql(当然也可以用 rpm -qa | grep mysql) 2.卸载已经存在的mysql ...

  9. mongodb权限

    1.在无密码模式下添加账号 db.createUser( { user: "user", pwd: "pwd", roles: [ { role: " ...

  10. jq table页二级联动

    <div class="layerRtb layerRtb-threecolumn"> <div class="clearfix layerRtb-he ...