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 ...
随机推荐
- hammerjs wabapp h5 触屏手势一网打尽
hammerjs官网 http://hammerjs.github.io/ 学习文章1 http://www.cnblogs.com/vajoy/p/4011723.html 学习文章2 htt ...
- 【Python】小练习
1.python爬虫 (1)抓取一个新闻网上含有某一关键字的新闻,http://internasional.kompas.com/就是这个网站上面所有内容含有THAAD这个关键词的新闻 (2)爬取大众 ...
- SQL SERVER 聚集索引 非聚集索引 区别
转自http://blog.csdn.net/single_wolf_wolf/article/details/52915862 一.理解索引的结构 索引在数据库中的作用类似于目录在书籍中的作用,用来 ...
- 腾讯云的云数据库MYSQL配置
腾讯云的云数据库MYSQL配置
- Spring boot Security 用于权限管理,用户添加等。
1:添加依赖: <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thy ...
- CentOS6.5安装RHadoop
1.首先安装依赖包(各个节点都要安装) [root@Hadoop-NN-01 ~]$ yum install gcc-gfortran #否则报”configure: error: No F77 co ...
- oracle怎么恢复被覆盖的存储过程
在oracle数据库中,如果覆盖了之前的存储过程,那得赶紧闪回,时长越长闪回的可能性越小. 原理很简单,存储过程的定义就是数据字典,修改数据字典跟修改普通表的数据没有区别,此时会把修改前的内容放到un ...
- myeclipse自带的数据库查看文件
jdbc:mysql://localhost:3306/videocms?useUnicode=true&characterEncoding=utf8
- LeetCode7.反转整数
给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: 假 ...
- git克隆代码
1.vs--team explorer-clone,或者team-connect to tfs-clone 2.1输入git的url,2输入本地放代码的文件夹,3点clone,克隆出4.双击4 3.点 ...