2018东北四省赛 Spin A Web 曼哈顿距离最小生成树

2023-10-04 02:30

本文主要是介绍2018东北四省赛 Spin A Web 曼哈顿距离最小生成树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

莫队的论文,讲的很清晰

问题描述:给定平面N个点,两边相连的代价为曼哈顿距离,求这些点的最小生成树

按一般想法,prime复杂度O(n^2),Kruskal复杂度O(n^2 logn),N很大时,这复杂度要爆炸了

但是最小生成树具有一个性质——环切性质,即如果在一个图中存在一个环,把环中权最大的边删去,那么现在最小生成树的权和

删之前相同,所以很多边都是没用的,可以删去

在平面内,分割成八个区域

可以证明,中心的原点只需和每个区域的一个点相连即可(证明当然是要看莫队的)

所以构造的图中至多有8n条边,复杂度为O(nlogn)

以区域1为例,设原点为O(x0,y0),另一点P(x1,y1),则 x1>x0 且 y1-x1 > y0-y1,满足x1+y1最小的点即是最近的点

实现使先将x排序,再将y-x排序,然后用树状数组维护x+y的最小值

然后八个区域转换后可以到区域1,因为是双向边,所以四次转换就可以

第一次直接计算,第二次按y=x翻转,即x与y坐标互换,第三次按x=0翻转,即x坐标为负数,第四次在按y=x翻转,就可以计算了

7231: Spin A Web

时间限制: 1 Sec  内存限制: 128 MB
提交: 127  解决: 24
[提交] [状态] [讨论版] [命题人:admin]

题目描述

We have a canvas divided into grid with H rows and W columns. The square at the ith row from the top and the jth  column from the left is represented as (i, j). (i, j) square has two value xi,j  and yi,j .
Now we want to merge the squares to a connected web with minimal cost. Two squares can be connected if they are in the same row or column, and the cost of connecting (i0, j0) and (i1, j1) is
|xi0,j0 − xi1,j1 | + |yi0,j0 − yi1,j1 |.

 

输入

Input is given from Standard Input in the following format:
H W
x1,1 x1,2  . . . x1,W
.
.
xH,1 xH,2  . . . xH,W
y1,1 y1,2  . . . y1,W
.
.
yH,1 yH,2  . . . yH,W
Constraints
1 ≤ H × W ≤ 100000
−108 ≤ xi,j, yi,j ≤ 108(1 ≤ i ≤ H, 1 ≤ j ≤ W )
All of them are integers.

 

输出

Print one line denotes the minimal cost to merge the square to be a connected web.

 

样例输入

1 3
1 3 2
1 2 3

 

样例输出

5

这个题只能同行或者同列相连,所以对每行每列求一次

代码:

#include <bits/stdc++.h>using namespace std;
typedef long long ll;
const int maxn = 1e5 + 100;
const int inf = 0x3f3f3f3f;inline int read() {char c;int sum = 0;int f = 1;c = getchar();while (c < '0' || c > '9') {if (c == '-')f = -1;c = getchar();}while (c >= '0' && c <= '9') {sum = sum * 10 + c - '0';c = getchar();}return sum * f;
}struct node {int x, y, pos;friend bool operator<(node a, node b) {if (a.x != b.x)return a.x < b.x;return a.y < b.y;}
} s[maxn];int p[maxn];struct node2 {int u, v, val;friend bool operator<(node2 x, node2 y) {return x.val < y.val;}
} edg[maxn * 8];int tot = 0;
int a[maxn], b[maxn];bool cmp(int a, int b) {if (s[a].x != s[b].x) return s[a].x < s[b].x;return s[a].y < s[b].y;
}bool cmp2(node2 x, node2 y) {return x.val < y.val;
}int value[maxn], poss[maxn];
int fa[maxn];void add(int x, int val, int pos) {for (int i = x; i > 0; i -= (i & (-i))) {if (value[i] > val) {value[i] = val;poss[i] = pos;}}
}int ask(int x, int n) {int ma = inf;int ans = -1;for (int i = x; i <= n; i += (i & (-i))) {if (value[i] < ma) {ma = value[i];ans = poss[i];}}return ans;
}int findd(int x) {return fa[x] == x ? x : fa[x] = findd(fa[x]);
}void Union(int x, int y) {int fx = findd(x);int fy = findd(y);if (fx != fy) {fa[fx] = fy;}
}void mandis(int n) {sort(p, p + n, cmp);for (int i = 0; i < n; i++) {a[i] = b[i] = s[p[i]].y - s[p[i]].x;}sort(b, b + n);int k = unique(b, b + n) - b;for (int i = 1; i <= n; i++) {value[i] = inf;poss[i] = -1;}for (int i = n - 1; i >= 0; i--) {int pp = lower_bound(b, b + k, a[i]) - b + 1;int ans = ask(pp, n);if (ans != -1) {edg[tot].u = s[p[i]].pos;edg[tot].v = s[p[ans]].pos;edg[tot++].val = abs(s[p[i]].x - s[p[ans]].x) + abs(s[p[i]].y - s[p[ans]].y);}add(pp, s[p[i]].x + s[p[i]].y, i);}
}void change(int n) {for (int i = 1; i <= 4; i++) {for (int j = 0; j < n; j++) {if (i == 2 || i == 4)swap(s[p[j]].x, s[p[j]].y);else if (i == 3)s[p[j]].x = -1 * s[p[j]].x;}mandis(n);}
}int main() {int h, w;h = read(), w = read();int id = 1;for (int i = 0; i < h; i++) {for (int j = 0; j < w; j++) {s[i * w + j].x = read();s[i * w + j].pos = id++;}}for (int i = 0; i < h; i++) {for (int j = 0; j < w; j++) {s[i * w + j].y = read();}}int cnt;for (int i = 0; i < h; i++) {cnt = 0;for (int j = 0; j < w; j++)p[cnt++] = i * w + j;change(cnt);}for (int j = 0; j < w; j++) {cnt = 0;for (int i = 0; i < h; i++) {p[cnt++] = i * w + j;}change(cnt);}for (int i = 1; i <= h * w; i++)fa[i] = i;sort(edg, edg + tot, cmp2);ll ans = 0;for (int i = 0; i < tot; i++) {if (findd(edg[i].u) != findd(edg[i].v)) {Union(edg[i].u, edg[i].v);ans += edg[i].val;}}printf("%lld\n", ans);return 0;
}

 

这篇关于2018东北四省赛 Spin A Web 曼哈顿距离最小生成树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1130

相关文章

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

Web服务器-Nginx-高并发问题

《Web服务器-Nginx-高并发问题》Nginx通过事件驱动、I/O多路复用和异步非阻塞技术高效处理高并发,结合动静分离和限流策略,提升性能与稳定性... 目录前言一、架构1. 原生多进程架构2. 事件驱动模型3. IO多路复用4. 异步非阻塞 I/O5. Nginx高并发配置实战二、动静分离1. 职责2

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

C#使用Spire.XLS快速生成多表格Excel文件

《C#使用Spire.XLS快速生成多表格Excel文件》在日常开发中,我们经常需要将业务数据导出为结构清晰的Excel文件,本文将手把手教你使用Spire.XLS这个强大的.NET组件,只需几行C#... 目录一、Spire.XLS核心优势清单1.1 性能碾压:从3秒到0.5秒的质变1.2 批量操作的优雅

Python使用python-pptx自动化操作和生成PPT

《Python使用python-pptx自动化操作和生成PPT》这篇文章主要为大家详细介绍了如何使用python-pptx库实现PPT自动化,并提供实用的代码示例和应用场景,感兴趣的小伙伴可以跟随小编... 目录使用python-pptx操作PPT文档安装python-pptx基础概念创建新的PPT文档查看

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结

Python实现数据可视化图表生成(适合新手入门)

《Python实现数据可视化图表生成(适合新手入门)》在数据科学和数据分析的新时代,高效、直观的数据可视化工具显得尤为重要,下面:本文主要介绍Python实现数据可视化图表生成的相关资料,文中通过... 目录前言为什么需要数据可视化准备工作基本图表绘制折线图柱状图散点图使用Seaborn创建高级图表箱线图热

Python中经纬度距离计算的实现方式

《Python中经纬度距离计算的实现方式》文章介绍Python中计算经纬度距离的方法及中国加密坐标系转换工具,主要方法包括geopy(Vincenty/Karney)、Haversine、pyproj... 目录一、基本方法1. 使用geopy库(推荐)2. 手动实现 Haversine 公式3. 使用py

SQLServer中生成雪花ID(Snowflake ID)的实现方法

《SQLServer中生成雪花ID(SnowflakeID)的实现方法》:本文主要介绍在SQLServer中生成雪花ID(SnowflakeID)的实现方法,文中通过示例代码介绍的非常详细,... 目录前言认识雪花ID雪花ID的核心特点雪花ID的结构(64位)雪花ID的优势雪花ID的局限性雪花ID的应用场景