题目传送门

死因:不认真读题


首先,我们可以想到暴力枚举每段长度为\(k\)的区间,对于这一段区间,求将它变为同一高度的最小操作次数。显然,当我们取区间的中位数时,这段区间变为同一高度的次数最小。

操作次数为:
对于大于中位数的数,求和,减去中位数乘它们的个数
对于小于等于中位数的数,中位数乘它们的个数减去它们的和
最后两项求和即可

所以我们需要支持的操作有:

  1. 求这段区间的中位数
  2. 求小于中位数的数的个数
  3. 求小于中位数的数的和

可以使用平衡树,在这里我是用的是权值线段树,比较方便。

最后要注意的是,高度可以为0

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define LL long long
#define ls p << 1
#define rs p << 1 | 1
#define mid ((l + r) >> 1)
using namespace std;
LL read() {LL k = 0, f = 1; char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9')k = k * 10 + c - 48, c = getchar();return k * f;
}
LL a[100010], n, k, maxn, sum[100010];
struct zzz {LL cnt[1000010 << 2]; LL sum[1000010 << 2];void up(LL p) {cnt[p] = cnt[ls] + cnt[rs];sum[p] = sum[ls] + sum[rs];}void insert(LL x, LL p = 1, LL l = 0, LL r = maxn) {if(l == r) {++cnt[p]; sum[p] += l;//cout << cnt[p] << ' ' << l << endl;return ;}if(x <= mid) insert(x, ls, l, mid);else insert(x, rs, mid+1, r);up(p);}void earse(LL x, LL p = 1, LL l = 0, LL r = maxn) {if(l == r) {--cnt[p]; sum[p] -= l; return ;}if(x <= mid) earse(x, ls, l, mid);else earse(x, rs, mid+1, r);up(p);}LL k_th(LL x, LL p = 1, LL l = 0, LL r = maxn) {if(l == r) return l;if(x <= cnt[ls]) return k_th(x, ls, l, mid);else return k_th(x - cnt[ls], rs, mid+1, r);}LL summ(LL nr, LL p = 1, LL l = 0, LL r = maxn, LL nl = 0) {LL ans = 0;if(nl <= l && nr >= r) return sum[p];if(nl <= mid) ans += summ(nr, ls, l, mid, nl);if(nr > mid) ans += summ(nr, rs, mid+1, r, nl);return ans;}LL numm(LL nr, LL p = 1, LL l = 0, LL r = maxn, LL nl = 0) {LL ans = 0;if(nl <= l && nr >= r) return cnt[p];if(nl <= mid) ans += numm(nr, ls, l, mid, nl);if(nr > mid) ans += numm(nr, rs, mid+1, r, nl);return ans;}
}tree;
int main() {n = read(), k = read();LL anss = -1, pos, num;for(LL i = 1; i <= n; ++i) a[i] = read(), maxn = max(a[i], maxn), sum[i] = a[i] + sum[i-1];for(LL i = 1; i <= n; ++i) {tree.insert(a[i]);if(i > k) tree.earse(a[i-k]);if(i >= k) {LL x = tree.k_th((k+1) >> 1);LL y = tree.summ(x), summ = sum[i] - sum[i-k], numl = tree.numm(x);//cout << y << ' ' << numl << ' ' << summ << endl;if(summ-y-x*(k-numl)+x*numl-y < anss || anss == -1) {anss = summ-y-x*(k-numl)+x*numl-y, pos = i; num = x;}}}printf("%lld\n", anss);for(LL i = 1; i <= n; ++i) {if(i >= pos-k+1 && i <= pos) {printf("%lld\n", num);}else printf("%lld\n", a[i]);}return 0;
}

转载于:https://www.cnblogs.com/wxl-Ezio/p/11024478.html

POI2008 KLO-Building blocks相关推荐

  1. Intel Thread Building Blocks (TBB) 入门篇

    一.什么是TBB TBB(Thread Building Blocks)是英特尔发布的一个库,全称为 Threading Building Blocks.TBB 获得过 17 届 Jolt Produ ...

  2. TBB(Intel Threading Building Blocks)学习笔记

    TBB(Intel Threading Building Blocks)学习笔记 并行与并发是相对的,OS里讲的是并发而在 架构 方面更多的是说并行.并行是分多个层面的,个人认为基本上可以分为这么几个 ...

  3. TBB(Intel Threading Building Blocks)并行化

    并行与并发是相对的,OS里讲的是并发而在架构方面更多的是说并行.并行是分多个层面的,个人认为基本上可以分为这么几个层面:1.指令级的并行:即所谓的微程序.指令流水线等,现在cpu的一级缓存.二级缓存都 ...

  4. 乐高机器人骨奥_乐高机器人-Building blocks robot

    乐高机器人-Building blocks robot 一款趣味性强又能触发儿童动手动脑的玩具可以在玩耍的过程中无形开发大脑智慧.乐高机器人就是这样一款玩具.集合了可编程主机.电动马达.传感器.Leg ...

  5. The Building Blocks of Interpretability

    Interpretability techniques are normally studied in isolation. We explore the powerful interfaces th ...

  6. iText in Action 2nd5.2节(Events for basic building blocks)读书笔记

    前言 在我们将一些基本构建块(Chunk,Paragraph,Chapter等)添加到Document对象的实例中是,基本的构建块是由PdfWriter对象转换为pdf语法.在这个过程中,有一个我们很 ...

  7. 《Patterns, Principles, and Pract》— chapter14 Introducing the Domain Modeling Building Blocks

    在该章节里面有如下内容: 从战术的角度创建高效的面向对象的领域模型 介绍value objects, entities, domain services, modules ,通过这些为domain和行 ...

  8. 海老师的技术博客: OCA 考试 准备笔记(一): Java Building Blocks

    第一张为Java基础, 主要包含了 定义变量 定义Java类 引用包 数据类型 首先, 在Java中,类是基本的块.类是对象的定义: 对象是类在内存中运行的具体实例. 1. 属性与方法 Java 的类 ...

  9. linux tbb 安装_Linux安装Intel Threading Building Blocks(TBB)

    编译安装: wget https://codeload.github.com/01org/tbb/tar.gz/2019_U3 tar zxvf 2019_U3 cd tbb-2019_U3 make ...

  10. Mobileye高级驾驶辅助系统(ADAS)

    Mobileye高级驾驶辅助系统(ADAS) Mobileye is the global leader in the development of vision technology for Adv ...

最新文章

  1. 使用GNS3简单模拟帧中继环境
  2. SVN switch 用法详解
  3. @value 数组_数据结构与算法:12 数组与稀疏矩阵
  4. c++控制台应用每一列数据如何对齐_懂Excel就能轻松入门Python数据分析包pandas(十六):合并数据...
  5. FIR数字滤波器的设计及应用——MATLAB
  6. ipv4地址是几位二进制数_知识点| ip地址详解,小学生都看的懂
  7. palm基础----8 国际化
  8. TrueBit白皮书解读
  9. 原生 遍历_ECMAScript 6 入门教程—异步遍历器
  10. s一般怎么称呼自己的m_上海平面设计工资一般是多少,我该怎么提升自己的平面设计能力?...
  11. Java_泛型练习题
  12. [PED07]Feature Selection for Clustering:A Review聚类特征选择综述
  13. 博图安装msi失败_西门子软件WIN7系统安装须知
  14. 7.3.6 导航之激光雷达
  15. ipad上能够编辑python_10 个可以在平板电脑上使用的 Python 编辑器
  16. 20200229小白自学Python之路00
  17. web前端开发技术实验与实践(第三版)储久良编著 项目6 文本与段落标记的应用
  18. php完美pdo类封装,PDO类的封装
  19. 很好用的界面设计工具——Balsamiq(转载)
  20. PHp勾股定理,2.6 探索勾股定理(1)

热门文章

  1. java 实现根据学号搜索学生信息
  2. Flutter 基础目录结构介绍、入口自定义widget、等相关组件使用
  3. 基于ise14.7中的cmd设计
  4. Eth-Trunk的知识体系
  5. DDOS和DOS区别
  6. 初探Ultra96-v2
  7. layout_marginRight不起作用
  8. 第十届“中国电机工程学会杯”全国大学生电工数学建模竞赛 A 题:微电网日前优化调度
  9. 火车头免登录php代码,织梦dedecms火车头采集器批量在线发布文章免登陆模块教程...
  10. 来自Window Presentation Foundation Program Design的读书笔记 (第四篇 下)