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的更多相关文章

  1. 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 ...

  2. bc.34.B.Building Blocks(贪心)

    Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. DTD - XML Building Blocks

    The main building blocks of both XML and HTML documents are elements. The Building Blocks of XML Doc ...

  4. 企业架构研究总结(35)——TOGAF架构内容框架之构建块(Building Blocks)

    之前忙于搬家移居,无暇顾及博客,今天终于得闲继续我的“政治课”了,希望之后至少能够补完TOGAF方面的内容.从前面文章可以看出,笔者并无太多能力和机会对TOGAF进行理论和实际的联系,仅可对标准的文本 ...

  5. TOGAF架构内容框架之构建块(Building Blocks)

    TOGAF架构内容框架之构建块(Building Blocks) 之前忙于搬家移居,无暇顾及博客,今天终于得闲继续我的“政治课”了,希望之后至少能够补完TOGAF方面的内容.从前面文章可以看出,笔者并 ...

  6. HDU—— 5159 Building Blocks

    Problem Description After enjoying the movie,LeLe went home alone. LeLe decided to build blocks. LeL ...

  7. [翻译]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 ...

  8. [Luogu P3469] [POI2008]BLO-Blockade (割点)

    题面 传送门:https://www.luogu.org/problemnew/show/P3469 Solution 先跟我大声念: poi! 然后开始干正事. 首先,我们先把题目中的点分为两类:去 ...

  9. 四、Implementation: The Building Blocks 实现:构件

    四.Implementation: The Building Blocks 实现:构件 This is the essential part of this guide. We will introd ...

随机推荐

  1. model1 就是jsp+javabean

  2. poj3268 Silver Cow Party (SPFA求最短路)

    其实还是从一个x点出发到所有点的最短路问题.来和回只需分别处理一下逆图和原图,两次SPFA就行了. #include<iostream> #include<cstdio> #i ...

  3. 【LeetCode】029. Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  4. JavaScript6 新语法 let 有什么优势

    最近看国外的前端代码时,发现ES6的新特性已经相当普及,尤其是 let,应用非常普遍 虽然 let 的用法与 var 相同,但不管是语法语义上,还是性能上,都提升了很多,下面就从这两方面对比一下 语法 ...

  5. Windows_Server_2008远程桌面多用户登陆的配置方法

    开启远程桌面后,Windows Vista(或Windows 2008)下默认只支持一个administrator用户登陆,一个登录后另一个就被踢掉了,下面提供允许同一个用户名同时多个用户登录的配置方 ...

  6. 2015.4.25利用UIAutomation 替代API函数,解决了ListView无法读数据的难题,顺便实现了鼠标模拟滚轮

    UIAutomation比API的优点是类似于消息处理机制,而不是主要靠模拟鼠标键盘发送消息 首先添加引用UIAutomationClient和UIAutomationTypes,在安装.net3.5 ...

  7. oracle 密码默认180天过期

    alter profile default limit password_life_time unlimited; alter user username identified by 'pwd';

  8. 使用pip一次升级所有安装的Python包(太牛了)

    import pip from subprocess import call for dist in pip.get_installed_distributions(): call("pip ...

  9. python获得当前工作目录和修改

    import os  curDir = os.getcwd() 最近使用Python 写了很多脚本,想导入脚本,发现不知道如何查看python 的默认工作目录,并修改默认工作目录. 方法/步骤   查 ...

  10. FMX 模态窗体

    FMX 模态窗体 dlg := TForm2.Create(nil);  dlg.ShowModal(procedure(ModalResult: TModalResult)  begin       ...