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 ...
随机推荐
- 网站编辑须知9个SEO技巧
1. 文章当中最好需要出现一个网站核心关键词所谓的网站核心的关键词就是指与网站内容相关性比较高并且是比较受欢迎的关键词,当然还有相当高的转化率 2. 文章标题当中需要出现关键词.关键字在标题标签< ...
- javaScript高级教程(七)正则表达式中括号三种常见作用
括号用来将子表达式标记起来,以区别于其他表达式 比如很多的命令行程序都提供帮助命令,键入 h 和键入 help 的意义是一样的,那么就会有这样的表达式: h(elp)? 字符h之后的elp可有可无这里 ...
- mysql 用户与权限
1.用户 1)创建用户 "create user '用户'@'host' identified by '密码';" 在5.7以后的版本中要求密码包含至少一个大写字母,一个小写字 ...
- DeepMind提出空间语言集成模型SLIM,有效编码自然语言的空间关系
前不久,DeepMind 提出生成查询网络 GQN,具备从 2D 画面到 3D 空间的转换能力.近日.DeepMind 基于 GQN 提出一种新模型.可以捕捉空间关系的语义(如 behind.left ...
- 了解Linux的进程与线程
了解Linux的进程与线程 http://timyang.net/linux/linux-process/ 上周碰到部署在真实服务器上某个应用CPU占用过高的问题,虽然经过tuning, 问题貌似已经 ...
- vue中定义多重样式
- PAT 1045 Favorite Color Stripe[dp][难]
1045 Favorite Color Stripe (30)(30 分) Eva is trying to make her own color stripe out of a given one. ...
- [LeetCode] 292. Nim Game_Easy tag: Math
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- iOS 网易彩票-1框架搭建
仿网易彩票,最终要做成的效果如下: 一.分层搭建 1.新建一个项目,Lottery.只支持7.1以上坚屏. 2.将素材全部图片全部拉到相应的文件夹里. 3.选中Lottery--右键Show in F ...
- MySQL实现SQL Server排名函数
最近在MySQL中遇到分组排序查询时,突然发现MySQL中没有row_number() over(partition by colname)这样的分组排序.并且由于MySQL中没有类似于SQL Ser ...