@hdu - 6329@ Problem K. Transport Construction
@description@
给定 n 个点,第 i 个点位于 (xi, yi)。
在第 i 个点与第 j 个点之间建边费用为 xi*xj + yi*yj。
求最小生成树。
Input
第一行一个整数 T (1≤T≤2000),表示数据组数。
每组数据给定一个整数 n(2≤n≤100000),表示点数。保证 ∑n≤10^6。
接下来 n 行,每行两个整数 xi, yi(1≤xi,yi≤10^6), 描述一个点的坐标。注意可以点可以重合。
Output
对于每组数据,输出最小生成树的权值和。
Sample Input
1
3
2 4
3 1
5 2
Sample Output
27
@solution@
完全图的最小生成树,套路般的 boruvka 算法(可以自己百度一下)。
boruvka 算法的其他部分都是套路,主要是考虑怎么求一个连通块的最小邻边。
注意到点积 \(x_i*x_j + y_i*y_j\) 其实可以写成斜率优化形式的式子 \(y_i*(\frac{x_i}{y_i}*x_j + y_j)\)。当 i 固定时相当于求 \(\frac{x_i}{y_i}*x_j + y_j\) 的最小值。
然后就可以转成维护凸包了。
但是我们还需要排除同一连通块中的点,而众所周知凸包很难实现删除。
可以考虑 cdq 分治(不清楚是不是,不过很像)。将同一连通块的点视作同一颜色,对颜色进行分治。
具体来说,对于区间 [l, r] 中的颜色,我们只考虑 [l, mid] 对 [mid + 1, r] 的贡献与 [mid + 1, r] 对 [l, mid] 的贡献。
分治时分开维护询问与点,通过先递归再归并排序实现横坐标与询问的有序。然后就可以 O(nlogn) 搞定一次查询最小邻边了。
套上生成树的复杂度为 O(nlog^2n)。但由于 boruvka 一般跑不满所以常数小,可以通过。
@accepted code@
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
typedef pair<ll, int> pli;
const int MAXN = 100000;
const ll INF = (1LL<<60);
struct point{
int x, y, c;
point(int _x=0, int _y=0, int _c=0):x(_x), y(_y), c(_c) {}
}pnt[MAXN + 5], qry[MAXN + 5], tmp[MAXN + 5];
double f(point p) {
return - 1.0 * p.x / p.y;
}
bool cmp1(point a, point b) {
return a.c == b.c ? (a.x == b.x ? a.y < b.y : a.x < b.x) : a.c < b.c;
}
bool cmp2(point a, point b) {
return a.c == b.c ? f(a) < f(b) : a.c < b.c;
}
double slope(point a, point b) {
if( a.x == b.x ) {
if( b.y >= a.y ) return INF;
else return -INF;
}
else return 1.0 * (b.y - a.y) / (b.x - a.x);
}
int que[MAXN + 5]; pli lnk[MAXN + 5];
void solve(int l, int r, int L, int R) {
if( L == R ) return ;
int M = (L + R) >> 1, m;
for(int i=l;i<=r;i++)
if( pnt[i].c <= M )
m = i;
solve(l, m, L, M), solve(m + 1, r, M + 1, R);
int s = 1, t = 0;
for(int i=l;i<=m;i++) {
while( s < t && slope(pnt[que[t-1]], pnt[que[t]]) >= slope(pnt[que[t]], pnt[i]) )
t--;
que[++t] = i;
}
for(int i=m+1;i<=r;i++) {
while( s < t && slope(pnt[que[s]], pnt[que[s+1]]) <= f(qry[i]) )
s++;
int j = que[s];
lnk[qry[i].c] = min(lnk[qry[i].c], mp(1LL*pnt[j].x*qry[i].x + 1LL*pnt[j].y*qry[i].y, pnt[j].c));
}
s = 1, t = 0;
for(int i=m+1;i<=r;i++) {
while( s < t && slope(pnt[que[t-1]], pnt[que[t]]) >= slope(pnt[que[t]], pnt[i]) )
t--;
que[++t] = i;
}
for(int i=l;i<=m;i++) {
while( s < t && slope(pnt[que[s]], pnt[que[s+1]]) <= f(qry[i]) )
s++;
int j = que[s];
lnk[qry[i].c] = min(lnk[qry[i].c], mp(1LL*pnt[j].x*qry[i].x + 1LL*pnt[j].y*qry[i].y, pnt[j].c));
}
int p = l, q = m + 1, k = l;
while( p <= m && q <= r ) {
if( pnt[p].x < pnt[q].x )
tmp[k++] = pnt[p++];
else tmp[k++] = pnt[q++];
}
while( p <= m ) tmp[k++] = pnt[p++];
while( q <= r ) tmp[k++] = pnt[q++];
for(int i=l;i<=r;i++) pnt[i] = tmp[i];
p = l, q = m + 1, k = l;
while( p <= m && q <= r ) {
if( f(qry[p]) < f(qry[q]) )
tmp[k++] = qry[p++];
else tmp[k++] = qry[q++];
}
while( p <= m ) tmp[k++] = qry[p++];
while( q <= r ) tmp[k++] = qry[q++];
for(int i=l;i<=r;i++) qry[i] = tmp[i];
}
int id[MAXN + 5], fa[MAXN + 5], clr[MAXN + 5];
int find(int x) {
return fa[x] = (fa[x] == x ? x : find(fa[x]));
}
bool unite(int x, int y) {
int fx = find(x), fy = find(y);
if( fx != fy ) {
fa[fx] = fy;
return true;
}
else return false;
}
int x[MAXN + 5], y[MAXN + 5], n;
void solve() {
scanf("%d", &n);
for(int i=1;i<=n;i++)
scanf("%d%d", &x[i], &y[i]), fa[i] = i;
ll ans = 0;
while( true ) {
int cnt = 0;
for(int i=1;i<=n;i++)
if( fa[i] == i )
id[clr[i] = (++cnt)] = i;
if( cnt == 1 ) break;
for(int i=1;i<=n;i++)
clr[i] = clr[find(i)];
for(int i=1;i<=n;i++)
pnt[i] = qry[i] = point(x[i], y[i], clr[i]);
sort(pnt + 1, pnt + n + 1, cmp1);
sort(qry + 1, qry + n + 1, cmp2);
for(int i=1;i<=cnt;i++)
lnk[i] = mp(INF, -1);
solve(1, n, 1, cnt);
for(int i=1;i<=cnt;i++)
if( unite(id[i], id[lnk[i].se]) )
ans += lnk[i].fi;
}
printf("%lld\n", ans);
}
int main() {
int T; scanf("%d", &T);
while( T-- ) solve();
}
@details@
其实。。。我一直卡在把询问和横坐标分开排序这一点上。。。
一直在想怎么才能避免在凸包上二分。。。
@hdu - 6329@ Problem K. Transport Construction的更多相关文章
- HDU 6342.Problem K. Expression in Memories-模拟-巴科斯范式填充 (2018 Multi-University Training Contest 4 1011)
6342.Problem K. Expression in Memories 这个题就是把?变成其他的使得多项式成立并且没有前导零 官方题解: 没意思,好想咸鱼,直接贴一篇别人的博客,写的很好,比我的 ...
- 2018 Multi-University Training Contest 4 Problem K. Expression in Memories 【模拟】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 200 ...
- Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP
Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...
- Codeforces 1089K - King Kog's Reception - [线段树][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem K]
题目链接:https://codeforces.com/contest/1089/problem/K time limit per test: 2 seconds memory limit per t ...
- Gym 101981K - Kangaroo Puzzle - [玄学][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem K]
题目链接:http://codeforces.com/gym/101981/problem/K Your friend has made a computer video game called “K ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题
Problem K. UTF-8 Decoder 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c702 ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem K. KMC Attacks 交互题 暴力
Problem K. KMC Attacks 题目连接: http://codeforces.com/gym/100714 Description Warrant VI is a remote pla ...
- XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem K. Piecemaking
题目:Problem K. PiecemakingInput file: standard inputOutput file: standard outputTime limit: 1 secondM ...
- HDU 6343.Problem L. Graph Theory Homework-数学 (2018 Multi-University Training Contest 4 1012)
6343.Problem L. Graph Theory Homework 官方题解: 一篇写的很好的博客: HDU 6343 - Problem L. Graph Theory Homework - ...
随机推荐
- 一句话介绍python线程、进程和协程
一.进程: Python的os模块封装了常见的系统调用,其中就包括fork.而fork是linux常用的产生子进程的方法,简言之是一个调用,两个返回. 在python中,以下的两个模块用于进程的使用. ...
- 微信小程序示例
http://www.cnblogs.com/ihardcoder/p/6097941.html http://www.wxapp-union.com/ http://blog.csdn.net/li ...
- Python执行时间的计算方法小结
Python执行时间的计算方法小结 首先说一下我遇到的坑,生产上遇到的问题,我调度Python脚本执行并监控这个进程,python脚本运行时间远远大于python脚本中自己统计的程序执行时间. 监控p ...
- 笔记本最小安装centos7 连接WiFi的方法
1.首先下载iw工具. yum -y install iw 2.获取无线网卡的名称 执行iw dev,假设获得名称为 wlp3s0(示例) 3.激活无线网络接口 执行ip link set wlp3s ...
- Cesium 1.50重量级新功能测评
概要 既Cesium 1.49中3dtile加载性能大幅提升以后,Cesium 1.50再次迎来几个重量级新功能: 1 地球裁切,这下相当于可以截取一部分地形影像数据,当作一个平面场景来用了! 2 射 ...
- SpringCloud-微服务的注册与发现Eureka
一.SpringCloud简介 Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...
- JAVA邀请码生成器
code import java.util.Random; /** * 邀请码生成器,算法原理:<br/> * 1) 获取id: 1127738 <br/> * 2) 使用自定 ...
- linux一些配置的记录
ssh 登录设置主机的别名 这样不用写那么多东西了 在/etc/ssh/ 下的ssh_config 和 sshd_config 两个文件 前一个是配置客户端的配置文件 另一个是服务器端的配置文件 主要 ...
- 2019-9-2-win10-uwp-存放网络图片到本地
title author date CreateTime categories win10 uwp 存放网络图片到本地 lindexi 2019-09-02 12:57:38 +0800 2018-2 ...
- Django快速创建新项目
Python免费视频含配套文件QQ124111294 https://pan.baidu.com/s/1bL5ml4 python.exe manage.py startapp app01 pytho ...