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 ...
随机推荐
- hdoj-1036-Average is not Fast Enough!(水题,坑)
题目链接:Average is not Fast Enough! #include <iostream> #include <cstring> #include <st ...
- 网络编程基础--IO模型
一 IO模型介绍: 背景 是 Linux环境下 的 network IO , Third Edition: The Sockets Networking ”,.2节“I/O Models ”,Stev ...
- vector map迭代器失效解决方案
vector : iter = container.erase(iter); //erase的返回值是删除元素下一个元素的迭代器 vector<int>::iterator it = ...
- MPEG4、XVID、AVC有什么区别
MPEG-4包含XviD和DivX,而AVC优于二者mpeg4 DVD用的多,101mpeg4有AVC格式(加强版MP4)AVC/H.264是一种最新且技术含量最高的视频编码格式,由MPEG-4标准进 ...
- [独孤九剑]Oracle知识点梳理(四)SQL语句之DML和DDL
本系列链接导航: [独孤九剑]Oracle知识点梳理(一)表空间.用户 [独孤九剑]Oracle知识点梳理(二)数据库的连接 [独孤九剑]Oracle知识点梳理(三)导入.导出 [独孤九剑]Oracl ...
- laravel redis的使用
学习源头: https://www.cnblogs.com/redirect/p/6185228.html
- 上传文件csv 导入功能
HTML代码: <script> function uploadCsv() { $('#chooseCsvFile').click(); } function doUploadCsv() ...
- 通过maven的jar包库找到对应的jar包。
查找连接: http://search.maven.org/ 查找实例 http://search.maven.org/#search|ga|1|a%3A%22log4j%22
- java代码随机数组合,随机号码产生器
总结:还是掌握方法的运用++++++ package com.c2; //随机数组合,随机号码产生器 //随机号码 import java.util.Random; public class rfe ...
- java代码异常普通的====
总结:对于各种流类, package com.da; //包括运行异常,和非运行异常 import java.io.*; public class ryl { public static void m ...