[POI2008]KLO-Building blocks
题目描述
N柱砖,希望有连续K柱的高度是一样的. 你可以选择以下两个动作 1:从某柱砖的顶端拿一块砖出来,丢掉不要了. 2:从仓库中拿出一块砖,放到另一柱.仓库无限大. 现在希望用最小次数的动作完成任务.你还要求输出结束状态时,每柱砖的高度。
Solution
这题相当于我们滑动一个大小为k的窗口,我们可以任意改动每摞砖的高度,那么最优高度为多少呢,很显然是中位数。
所以这题变成了一道大水题,维护一个数据结构,支持插入,删除,查K大。
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 100004
using namespace std;
int n,a[N],num[N<<],k,top,jue;
long long tr[N<<],ans=0x7f7f7f7f,b[N],ansn;
long long q(int cnt,int l,int r,int L,int R){
if(L>R)return ;
if(l>=L&&r<=R)return tr[cnt];
int mid=(l+r)>>;
long long ans=;
if(mid>=L)ans+=q(cnt<<,l,mid,L,R);
if(mid<R)ans+=q(cnt<<|,mid+,r,L,R);
return ans;
}
int qsum(int cnt,int l,int r,int L,int R){
if(L>R)return ;
if(l>=L&&r<=R)return num[cnt];
int mid=(l+r)>>;
int ans=;
if(mid>=L)ans+=qsum(cnt<<,l,mid,L,R);
if(mid<R)ans+=qsum(cnt<<|,mid+,r,L,R);
return ans;
}
void add(int cnt,int l,int r,int x,int tag){
if(l==r){tr[cnt]+=b[x]*tag;num[cnt]+=tag;return;}
int mid=(l+r)>>;
if(mid>=x)add(cnt<<,l,mid,x,tag);
else add(cnt<<|,mid+,r,x,tag);
tr[cnt]=tr[cnt<<]+tr[cnt<<|];
num[cnt]=num[cnt<<]+num[cnt<<|];
}
int find(int cnt,int l,int r,int k){
if(l==r)return l;
int mid=(l+r)>>;
if(num[cnt<<]>=k)return find(cnt<<,l,mid,k);
else return find(cnt<<|,mid+,r,k-num[cnt<<]);
}
int main(){
scanf("%d%d",&n,&k);
for(int i=;i<=n;++i)scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b+n+);
top=unique(b+,b+n+)-b-;
for(int i=;i<=n;++i)a[i]=lower_bound(b+,b+top+,a[i])-b;
for(int i=;i<=k;++i)add(,,top,a[i],);
ans=2e18;
long long sum=(k+)/;
for(int i=k;i<=n;++i){
int x=find(,,top,sum);
long long aa=b[x]*qsum(,,top,,x-)-q(,,top,,x-)+q(,,top,x+,top)-b[x]*qsum(,,top,x+,top);
if(aa<ans){
ans=aa;
jue=i;
ansn=b[x];
}
add(,,top,a[i+],);add(,,top,a[i-k+],-);
}
printf("%lld\n",ans);
for(int i=;i<jue-k+;++i)printf("%d\n",b[a[i]]);
for(int i=jue-k+;i<=jue;++i)printf("%d\n",ansn);
for(int i=jue+;i<=n;++i)printf("%d\n",b[a[i]]);
return ;
}
[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 ...
随机推荐
- Prometheus & SoundCloud
Prometheus 系统监控方案 一 - Vovolie - 博客园https://www.cnblogs.com/vovlie/p/Prometheus_CONCEPTS.html Prometh ...
- spring datasource jdbc 密码 加解密
spring datasource 密码加密后运行时解密的解决办法 - 一号门-程序员的工作,程序员的生活(java,python,delphi实战)http://www.yihaomen.com/a ...
- 启动Tomcat的时候8080被占用
异常来源:启动Tomcat服务器报错: Several ports (8080, 8009) required by Tomcat v7.0 Server at localhost are alrea ...
- 配置router列表
import Vue from "vue"; import VueRouter from 'vue-router'; import Star from '../components ...
- anaconda + VSCode + 生产环境配置
1. 修改jupyter notebook 默认路径: 进入anaconda 命令行, jupyter notebook --generate-config 生成配置文件, 该文件在 本机用 ...
- python爬虫之线程池和进程池
一.需求 最近准备爬取某电商网站的数据,先不考虑代理.分布式,先说效率问题(当然你要是请求的太快就会被封掉,亲测,400个请求过去,服务器直接拒绝连接,心碎),步入正题.一般情况下小白的我们第一个想到 ...
- 莫烦scikit-learn学习自修第二天【算法地图】
1. 算法地图
- 莫烦keras学习自修第三天【回归问题】
1. 代码实战 #!/usr/bin/env python #!_*_ coding:UTF-8 _*_ import numpy as np # 这句话不知道是什么意思 np.random.seed ...
- How to convert mkv to mp4 lossless
ffmpeg -i example.mkv -vcodec copy -acodec copy example.mp4
- 【python练习题】程序17
#题目:输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. s = input('请输入字符串:') alf = 0 space = 0 digi = 0 other = 0 for ...