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 ...
随机推荐
- Nhibernate系列学习之(二) 简单增删改查
实例中解决方案简单的创建三层架构,符合开发过程中最简单的运用: 1:首先在数据库中创建一个表T_School,脚本如下: USE [TestDb] GO /****** 对象: Table [dbo] ...
- java 网页 保存上传文件
网页请求提交到另外一个jsp 进行处理 index.jsp <%@ page language="java" import="java.util.*" p ...
- 西瓜书概念整理(chapter 1-2)熟悉机器学习术语
括号表示概念出现的其他页码, 如有兴趣协同整理,请到issue中认领章节 完整版见我的github:ahangchen 觉得还不错的话可以点个star ^_^ 第一章 绪论 Page2: 标记(lab ...
- 10个 NPM 使用技巧
对于一个项目,常用的一些npm简单命令包含的功能有:初始化一个文件夹( npm init ),下载npm模块( npm install ),创建测试( npm tese ) 和自定义脚本( npm r ...
- Weblogic-unable to get file lock, will retry …问题解决
weblogic部署应用出现如下报错: <2017-8-15 下午05时08分44秒 CST> <Info> <Management> <BEA-141281 ...
- think python chapter2
1.An assignment statement creates a new variable and gives it a value: Variable names can be as long ...
- Xcode工具特性
1.注释 #pragma mark 注释说明#pragma mark - 分类/分组注释说明 2.自定义代码块. 3.多文本编辑框 View>>Assistant Editor
- ES之二:Elasticsearch原理
Elasticsearch是最近两年异军突起的一个兼有搜索引擎和NoSQL数据库功能的开源系统,基于Java/Lucene构建.最近研究了一下,感觉 Elasticsearch 的架构以及其开源的生态 ...
- UE4模型导入基础教程
转自:http://www.unrealchina.net/portal.php?mod=view&aid=290
- 2015.5.21 VS2010中引用Word组件后提示 类型“Microsoft.Office.Interop.Word.ApplicationClass”未定义构造函数 解决方法
wordApp = new Word.ApplicationClass();//这句在VS2005中没问题,在2010中会报错. 解决方法:在资源管理器 “引用”项的"Microsoft.O ...