http://uoj.ac/problem/112

先扣掉在同一侧的情况。

当\(k=1\)时,桥建在所有位置的中位数。

当\(k=2\)时,对于每个居民\((S_i,T_i)\),这个居民只会走离\(\frac{S_i+T_i}2\)最近的桥,那么对所有\(\frac{S_i+T_i}2\)排序,最优方案一定满足排序后的居民从中间分开,左边的居民走左边的桥,右边的居民走右边的桥。

从左往右扫,不断加入“左边的居民”,“左边的桥”建在当前“左边的居民”的所有\(S_i\)和\(T_i\)的中位数上,动态维护这个中位数就可以了。右边同理,最后合并答案即可。

对于动态维护中位数,考虑到每次加两个数,中位数只会在相邻的位置间左右横跳,所以维护前驱和后继就可以了。这里可以用树状数组/权值线段树维护前驱后继(前缀最大值,后缀最小值)。

时间复杂度\(O(n\log n)\)。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll; const int N = 100003; struct node {
int x, y;
bool operator < (const node &A) const {
return x + y < A.x + A.y;
}
} Q[N]; ll ans = 0;
int H[N << 1], cnt = 0, n, k, Si, Ti, tot = 0;
char pi, qi; namespace hahaha {
void solve() {
cnt = 0;
for (int i = 1; i <= tot; ++i)
H[++cnt] = Q[i].x, H[++cnt] = Q[i].y;
stable_sort(H + 1, H + cnt + 1); ll sum, ret = 0;
for (int i = cnt >> 1; i >= 1; --i) ret += H[i];
sum = ret;
for (int i = (cnt >> 1) + 1; i <= cnt; ++i) sum += H[i]; ans += sum - (ret << 1);
printf("%lld\n", ans);
}
} template <typename T> void check_max(T &a, T b) {if (b > a) a = b;}
template <typename T> void check_min(T &a, T b) {if (b < a) a = b;} namespace miaomiaomiao {
ll f1[N], f2[N];
int bitsl[N << 1], bitsr[N << 1], id[N << 1]; bool cmp(int x, int y) {return (x > tot ? Q[x - tot].y : Q[x].x) < (y > tot ? Q[y - tot].y : Q[y].x);} void insl(int x) {
for (int t = x; t <= cnt; t += t & -t)
check_max(bitsl[t], x);
} void insr(int x) {
for (int t = x; t; t -= t & -t)
check_min(bitsr[t], x);
} int findl(int x) {
int ret = 0;
for (int t = x - 1; t; t -= t & -t)
check_max(ret, bitsl[t]);
return ret;
} int findr(int x) {
int ret = cnt + 1;
for (int t = x + 1; t <= cnt; t += t & -t)
check_min(ret, bitsr[t]);
return ret;
} void work(ll *f) {
int mid = 0; ll ret = 0, sum = 0;
for (int i = 1; i <= cnt; ++i) bitsl[i] = 0, bitsr[i] = cnt + 1;
for (int i = 1; i <= tot; ++i) {
int x = Q[i].x, y = Q[i].y;
insl(x); insl(y);
insr(x); insr(y);
sum += H[x]; sum += H[y];
if (x < mid && y > mid || y < mid && x > mid)
ret += H[min(x, y)];
else if (x < mid) {
ret -= H[mid];
ret += H[x]; ret += H[y];
mid = findl(mid);
} else {
mid = findr(mid);
ret += H[mid];
}
f[i] = sum - (ret << 1);
}
} void solve() {
for (int i = 1; i <= (tot << 1); ++i) id[i] = i;
stable_sort(Q + 1, Q + tot + 1);
stable_sort(id + 1, id + (tot << 1) + 1, cmp);
cnt = 0;
for (int i = 1; i <= (tot << 1); ++i) {
++cnt;
if (id[i] > tot) H[cnt] = Q[id[i] - tot].y, Q[id[i] - tot].y = cnt;
else H[cnt] = Q[id[i]].x, Q[id[i]].x = cnt;
} work(f1);
reverse(Q + 1, Q + tot + 1);
work(f2); ll ra = f2[tot];
for (int i = 1; i <= tot; ++i)
check_min(ra, f1[i] + f2[tot - i]);
printf("%lld\n", ans + ra);
}
} int main() {
scanf("%d%d", &k, &n);
for (int i = 1; i <= n; ++i) {
for (pi = getchar(); pi != 'A' && pi != 'B'; pi = getchar());
scanf("%d", &Si);
for (qi = getchar(); qi != 'A' && qi != 'B'; qi = getchar());
scanf("%d", &Ti);
if (qi == pi) ans += abs(Ti - Si);
else {
Q[++tot] = (node) {Si, Ti};
++ans;
}
} if (k == 1) hahaha::solve();
else miaomiaomiao::solve(); return 0;
}

【UOJ #112】【APIO 2015】Palembang Bridges的更多相关文章

  1. 【CTSC 2015】&【APIO 2015】酱油记

    蒟蒻有幸参加了神犇云集的CTSC & APIO 2015,感觉真是被虐成傻逼了……这几天一直没更新博客,今天就来补一下吧~~(不过不是题解……) Day 0 从太原到北京现在坐高铁只需3小时= ...

  2. 【UOJ #110】【APIO 2015】Bali Sculptures

    http://uoj.ac/problem/110 这道题subtask4和subtask5是不同的算法. 主要思想都是从高位到低位贪心确定答案. 对于subtask4,n比较小,设\(f(i,j)\ ...

  3. 【BZOJ 4070】【APIO 2015】雅加达的摩天楼

    http://www.lydsy.com/JudgeOnline/problem.php?id=4070 分块建图. 对每个\(P_i\)分类讨论,\(P_i>\sqrt N\)则直接连边,边数 ...

  4. 【MyEclipse 2015】 逆向破解实录系列【终】(纯研究)

    声明 My Eclipse 2015 程序版权为Genuitec, L.L.C所有. My Eclipse 2015 的注册码.激活码等授权为Genuitec, L.L.C及其付费用户所有. 本文只从 ...

  5. 【MyEclipse 2015】 逆向破解实录系列【2】(纯研究)

    声明 My Eclipse 2015 程序版权为Genuitec, L.L.C所有. My Eclipse 2015 的注册码.激活码等授权为Genuitec, L.L.C及其付费用户所有. 本文只从 ...

  6. 【CEDEC 2015】【夏日课堂】制作事宜技术篇,新手职员挑战VR Demo开发的真相

    日文原文地址 http://www.4gamer.net/games/277/G027751/20150829002/ PS:CEDEC 2015的PPT有些要到10月才有下载,目前的都是记者照片修图 ...

  7. 【SIGGRAPH 2015】【巫师3 狂猎 The Witcher 3: Wild Hunt 】顶级的开放世界游戏的实现技术。

    [SIGGRAPH 2015][巫师3 狂猎 The Witcher 3: Wild Hunt ]顶级的开放世界游戏的实现技术 作者:西川善司 日文链接  http://www.4gamer.net/ ...

  8. 【UOJ】67 新年的毒瘤 &【BZOJ】1123 BLO

    [UOJ 67] 题目链接: 传送门 题解: 第一眼很懵逼……这什么鬼. 思考什么点复合条件……(o(>﹏<)o 1.树,也就是说还剩n-2条边,等价于要删去一个度数为m-n+2的点. 2 ...

  9. 【UOJ#236】[IOI2016]railroad(欧拉回路,最小生成树)

    [UOJ#236][IOI2016]railroad(欧拉回路,最小生成树) 题面 UOJ 题解 把速度看成点,给定的路段看成边,那么现在就有了若干边,然后现在要补上若干边,以及一条\([inf,\) ...

随机推荐

  1. webDriver检索table数据

    最近在做爬虫相关工作,用到了webdriver,记录一些遇到的问题和解决方法: 如何查找 table中的行 例如: <div id="a"> <table cla ...

  2. 使用CSS3+JQuery打造自定义视频播放器

    简介 HTML5的<video>标签已经被目前大多数主流浏览器所支持,包括还未正式发布的IE9也声明将支持<video>标签,利用浏览器原生特性嵌入视频有很多好处,所以很多开发 ...

  3. 【LibreOJ】#6257. 「CodePlus 2017 12 月赛」可做题2

    [题意]数列满足an=an-1+an-2,n>=3.现在a1=i,a2=[l,r],要求满足ak%p=m的整数a2有多少个.10^18. [算法]数论(扩欧)+矩阵快速幂 [题解]定义fib(i ...

  4. WordPress友情链接插件的安装

    插件:link manager 这样就安装成功! 在外观小工具里 把右边即可 这样在前台页面上就可看见添加的友情链接了!!!

  5. Git HTTPS 方式自动保存用户名密码

    一行命令搞定: git config --global credential.helper wincred 第一次输入用户名和密码提交,第二次就不需要了 参考: https://help.github ...

  6. Mysql储存过程6: in / out / inout

    in 为向函数传送进去的值 out 为函数向外返回的值 intout 传送进去的值, 并且还返回这个值 )) begin then select 'true'; else select 'false' ...

  7. 如何通过掩码计算可用的IP数量

    假设掩码是28,28也就是28个1.本身掩码是255.255.255.255那么转换成二进制也就是 11111111,11111111,11111111,11111111 那么28个1也就是: 111 ...

  8. 64_n2

    nodejs-from-0.1.3-4.fc26.noarch.rpm 11-Feb-2017 15:01 9982 nodejs-from2-2.1.0-6.fc26.noarch.rpm 11-F ...

  9. python批量替换文件名

    替换关键字 #-*-coding:utf-8-*- import os import re filepath = u'E:\\CMMI4\\07_测试文档' files = os.walk(filep ...

  10. (正则表达式)linux shell 字符串操作(长度,查找,替换,匹配)详解

    在做shell批处理程序时候,经常会涉及到字符串相关操作.有很多命令语句,如:awk,sed都可以做字符串各种操作. 其实shell内置一系列操作符号,可以达到类似效果,大家知道,使用内部操作符会省略 ...