Luogu 3466 [POI2008]KLO-Building blocks
BZOJ 1112。
题意相当于在一个长度为$k$的区间内选择一个数$s$使$\sum_{i = 1}^{k}\left | a_i - s \right |$最小。
很显然是中位数。
然后只要写一个能查询长度为$k$的区间的中位数,以及小于和大于这个中位数的总和和个数的数据结构即可。
线段树平衡树对顶堆随便维护。
我选择权值线段树。
时间复杂度$O(nlogn)$。
Luogu上还需要输出方案。
Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll; const int N = 1e5 + ; int n, K;
ll mn = 0LL, a[N]; template <typename T>
inline void read(T &X) {
X = ; char ch = ; T op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} template <typename T>
inline void chkMax(T &x, T y) {
if(y > x) x = y;
} namespace SegT {
struct Node {
int lc, rc;
ll sum, cnt;
} s[N * ]; int root, nodeCnt = ; #define lc(p) s[p].lc
#define rc(p) s[p].rc
#define sum(p) s[p].sum
#define cnt(p) s[p].cnt
#define mid ((l + r) >> 1) void ins(int &p, ll l, ll r, ll x) {
if(!p) p = ++nodeCnt;
sum(p) += x, ++cnt(p);
if(l == r) return; if(x <= mid) ins(lc(p), l, mid, x);
else ins(rc(p), mid + , r, x);
} void del(int &p, ll l, ll r, ll x) {
sum(p) -= x, --cnt(p);
if(l == r) return; if(x <= mid) del(lc(p), l, mid, x);
else del(rc(p), mid + , r, x);
} ll getKth(int p, ll l, ll r, int k) {
if(l == r) return l; int now = cnt(lc(p));
if(k <= now) return getKth(lc(p), l, mid, k);
else return getKth(rc(p), mid + , r, k - now);
} int qCnt(int p, ll l, ll r, ll x, ll y) {
if(x <= l && y >= r) return cnt(p); int res = ;
if(x <= mid) res += qCnt(lc(p), l, mid, x, y);
if(y > mid) res += qCnt(rc(p), mid + , r, x, y);
return res;
} ll qSum(int p, ll l, ll r, ll x, ll y) {
if(x <= l && y >= r) return sum(p); ll res = 0LL;
if(x <= mid) res += qSum(lc(p), l, mid, x, y);
if(y > mid) res += qSum(rc(p), mid + , r, x, y);
return res;
} #undef mid } using namespace SegT; int main() {
read(n), read(K);
for(int i = ; i <= n; i++) {
read(a[i]);
chkMax(mn, a[i]);
} // ll sum = 0LL;
for(int i = ; i <= K; i++) {
// sum += a[i];
ins(root, , mn, a[i]);
} int pos = ;
ll mid = getKth(root, , mn, (K + ) / );
ll minCost = mid * qCnt(root, , mn, , mid) - qSum(root, , mn, , mid);
minCost += qSum(root, , mn, mid + , mn) - mid * qCnt(root, , mn, mid + , mn);
for(int i = K + ; i <= n; i++) {
del(root, , mn, a[i - K]);
ins(root, , mn, a[i]); ll nowMid = getKth(root, , mn, (K + ) / );
ll nowCost = nowMid * qCnt(root, , mn, , nowMid) - qSum(root, , mn, , nowMid);
nowCost += qSum(root, , mn, nowMid + , mn) - nowMid * qCnt(root, , mn, nowMid + , mn); if(nowCost < minCost) {
pos = i - K + ;
mid = nowMid;
minCost = nowCost;
}
} printf("%lld\n", minCost);
/* for(int i = 1; i <= n; i++) {
if(i >= pos && i <= pos + K - 1) printf("%lld\n", mid);
else printf("%lld\n", a[i]);
} */ return ;
}
Luogu 3466 [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 ...
- [Luogu P3469] [POI2008]BLO-Blockade (割点)
题面 传送门:https://www.luogu.org/problemnew/show/P3469 Solution 先跟我大声念: poi! 然后开始干正事. 首先,我们先把题目中的点分为两类:去 ...
- 四、Implementation: The Building Blocks 实现:构件
四.Implementation: The Building Blocks 实现:构件 This is the essential part of this guide. We will introd ...
随机推荐
- 省略setget方法
可以装一下这个插件再引入一个jar包实体类里不需要再写get/set方法了 maven坐标:<dependency> <groupId>org.projectlombok< ...
- New Concept English three (52)
My cousin, Harry, keeps a large curiously-shaped bottle on permanent display in his study. Despite t ...
- 自定义php(NON-CORE WORDPRESS FILE) 引用 wordpress
在文件头头添加下面代码,实现此页面可以调用wordpress的内置方法 <?php $parse_uri = explode ( 'wp-content', $_SERVER ['SCRIPT_ ...
- PHP数据结构之实现栈
接着前面PHP数据结构来学习,今天写的是实现栈. <?php class stack //定义一个栈的类 { private $size; //栈的空间大小 private $top; // 栈 ...
- 快速构建一个 Springboot
快速构建一个 Springboot 官网:http://projects.spring.io/spring-boot/ Spring Boot可以轻松创建可以“运行”的独立的,生产级的基于Spring ...
- mendeley 参考文献管理工具
本文由Suzzz原创,发布于http://www.cnblogs.com/Suzzz/p/4044144.html,转载请保留此声明 目录 介绍 功能 运行截图 安装方法 创建 Desktop Ent ...
- 最终还是选择了markdownpad2
markdownpad2使用 最终 哈哈,最后还是选择了markdownpad2,经过探索才知道这个玩意多么好用. 点击,下载. 碰到的问题 1.win10出现HTML无法渲染得对话框 结果是,官网有 ...
- 应用层-day01
主流应用程序体系结构:CS结构.P2P结构. CS结构:客户-服务器体系结构.有一台总是打开的主机称为服务器,它服务来自其他许多称为客户的主机的请求. P2P体系结构:应用程序在不同的主机间链接,被称 ...
- asp.net给按钮添加删除确认
if (!IsPostBack) { BtnDel.Attributes["onclick"] = "javascript:return window.confirm(' ...
- div+css制作带箭头提示框效果图(原创文章)
一直都在看站友们的作品,今天也来给大家分享一个小的效果,第一次发还有点小紧张呢,语言表达能力不是很好,还请见谅…^ 先来个简单点的吧,上效果图 刚开始在网上看到效果图的时候感觉好神奇,当我试着写出来的 ...