题目

Analysis

曼哈顿距离($L1$ metric)最小生成树。

Implementation

下面的代码参考了 gispzjz 在比赛中的提交

#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x <<": " << (x) << endl
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#ifdef LOCAL
#define see(x) cout << #x << ": " << (x) << endl
#endif
#ifndef LOCAL
#define see(x)
#endif #define rep(n) for(int _ = 0; _ != (n); ++_)
//#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rng(i, n) for(int i = 0; i != (n); ++i)
#define rng(i, a, b) for(int i = (a); i < (b); ++i)
#define rno(i, b) for(int i = 0; i<(b); ++i)
#define rnc(i, a, b) for(int i = (a); i<=(b); ++i)
#define RNG(i, a) for(auto &i: (a))
#define dwn(i, r, l) for(int i = (r); i>=(l); i--) namespace std {
template<class T>
T begin(std::pair<T, T> p)
{
return p.first;
}
template<class T>
T end(std::pair<T, T> p)
{
return p.second;
}
} #if __cplusplus < 201402L
template<class Iterator>
std::reverse_iterator<Iterator> make_reverse_iterator(Iterator it)
{
return std::reverse_iterator<Iterator>(it);
}
#endif template<class Range>
std::pair<std::reverse_iterator<decltype(begin(std::declval<Range>()))>, std::reverse_iterator<decltype(begin(std::declval<Range>()))>> make_reverse_range(Range &&r)
{
return std::make_pair(make_reverse_iterator(::begin(r)), make_reverse_iterator(::end(r)));
} #define RRNG(x, cont) for (auto &x: make_reverse_range(cont)) template<class T> int sign(const T &a) { return a == 0 ? 0 : a > 0 ? 1 : -1; }
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
template<class T> void Min(T &a, const T &b){ a = min(a, b); }
template<class T> void Max(T &a, const T &b){ a = max(a, b); } template<typename T> void println(const T &t) { cout << t << '\n'; }
template<typename T, typename ...Args> void println(const T &t, const Args &...rest) { cout << t << ' '; println(rest...); } template<typename T> void print(const T &t) { cout << t << ' '; } template<typename T, typename ...Args> void print(const T &t, const Args &...rest) { cout << t; print(rest...); } // this overload is chosen when there's only one argument
template<class T> void scan(T &t) { cin >> t; }
template<class T, class ...Args> void scan(T &a, Args &...rest) { cin >> a; scan(rest...); } using ll = long long;
using ull = unsigned long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pip = pair<int, pii>;
using szt = size_t;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using pli = pair<ll,int>;
using wg = vector<vpii>; //weighted graph int cas;
const double pi = acos(-1);
ll mod = 1e9 + 7;
//要求:0<=a<mod, 0<=b<=mod
template<class T>
inline void add_mod(T &a, const T &b) {
a += b;
if (a >= mod) a -= mod;
}
template<class T>
void sub_mod(T &a, const T &b){
a -= b;
if (a < 0) a += mod;
}
auto bo=[](int x){
bitset<5> a(x);
cout << a << endl;
}; //返回值:a中比k小的元素有多少个?
template<class V, class Cont>
int get_rank(const V &k, const Cont &a){
return std::lower_bound(all(a), k) - a.begin();
} mat operator*(const mat &a, const mat &b) {
mat c(a.size(), vec(b[0].size()));
for (size_t i = 0; i < a.size(); i++) {
for (size_t j = 0; j < a[0].size(); j++) {
if (a[i][j]) { // optimization for sparse matrix
for (size_t k = 0; k < b[0].size(); k++) {
add_mod(c[i][k], a[i][j] * b[j][k] % mod);
}
}
}
}
return c;
} vec operator*(const mat &a, const vec &b) {
vec c(a.size());
for (size_t i = 0; i < a.size(); i++) {
for (size_t j = 0; j < a[0].size(); j++) {
add_mod(c[i], a[i][j] * b[j] % mod);
}
}
return c;
} mat pow(mat a, ull n) {
mat res(a.size(), vec(a[0].size()));
for (size_t i = 0; i < a.size(); i++) {
res[i][i] = 1;
}
while (n) {
if (n & 1) {
res = res * a;
}
a = a * a;
n >>= 1;
}
return res;
} // Codeforces does not support __int128
//std::ostream& operator<<(std::ostream& os, __int128 T) {
// if (T<0) os<<"-";
// if (T>=10 ) os<<T/10;
// if (T<=-10) os<<(-(T/10));
// return os<<( (int) (T%10) >0 ? (int) (T%10) : -(int) (T%10) ) ;
//}
//
//__int128 LPOW(__int128 x, ll n) {
// __int128 res = 1;
// for (; n; n /= 2, x *= x, x %= mod) {
// if (n & 1) {
// res *= x;
// res %= mod;
// }
// }
// return res;
//} ll POW(ll x, ll n){
ll res = 1;
for (; n; n /= 2, x *= x, x %= mod) {
if (n & 1) {
res *= x;
res %= mod;
}
}
return res;
} ll INV(ll x) {
return POW(x, mod - 2);
} ll inv(ll x){
// see(x);
return x == 1? 1: (mod - mod/x * inv(mod%x) % mod);
} // 2D rotation
void rotate(double &x, double &y, double theta) {
double tx = cos(theta) * x - sin(theta) * y;
double ty = sin(theta) * x + cos(theta) * y;
x = tx, y = ty;
} struct dsu{
vector<int> par;
explicit dsu(int n){ // 0-indexed
par.resize(n);
rng(i, 0, n){
par[i] = i;
}
}
bool same(int x, int y){
return root(x) == root(y);
}
int root(int x){
return par[x] == x ? x : par[x] = root(par[x]);
}
void unite(int x, int y){
x = root(x);
y = root(y);
par[x] = y;
}
}; struct bit {
vector<int> a;
vector<int> id; // id[i]:键区间(i-lowbit(i), i]中,x+y取最小值的点的编号
bit(int n, int v = 0, bool manhattan = false) {
a.resize(n + 1);
for (int i = 1; i <= n; ++i) a[i] = v;
if(manhattan){
id.resize(n+1);
rng(i, 1, n+1) id[i] = -1;
}
} ll sum(int x) {
ll res = 0;
while (x) {
res += a[x];
x -= x & -x;
}
return res;
} ll sum(int l, int r) {
if (l > r) return 0;
return sum(r) - sum(l - 1);
} void add(int x, ll v) {
while (x < a.size()) {
a[x] += v;
x += x & -x;
}
} void min(int x, int v) {
while (x < a.size()) {
a[x] = std::min(a[x], v);
x += x & -x;
}
} int manhattan_min(int x){ // 返回x+y最小的点的编号
int ans = -1;
int res = INT_MAX;
while(x){
if(a[x] < res){
res = a[x];
ans = id[x];
}
x -= x & - x;
}
return ans;
} void manhattan_min(int x, int v, int i){
while(x < a.size()){
if(a[x] > v){
a[x] = v;
id[x] = i;
}
x += x & - x;
}
} void max(int x, int v) {
while (x < a.size()) {
a[x] = std::max(a[x], v);
x += x & -x;
}
} int min(int x) {
int res = INT_MAX;
while (x) {
res = std::min(res, a[x]);
x -= x & -x;
}
return res;
} int max(int x) {
int res = INT_MIN;
while (x) {
res = std::max(res, a[x]);
x -= x & -x;
}
return res;
}
}; namespace util{
int len(ll x){return snprintf(nullptr, 0, "%lld", x);}
vi get_d(ll x){
vi res;
while(x) {
res.pb(x%10);
x /= 10;
}
reverse(all(res));
return res;
}
template <class T> T parity(const T &a){
return a & 1;
}
template <class T>
void out (const vector<T> &a){
std::copy(a.begin(), a.end(), std::ostream_iterator<T>(std::cout, ", "));
cout << endl;
};
} using namespace util; #include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> using order_statistic_tree = __gnu_pbds::tree<
int,
__gnu_pbds::null_type,
greater<int>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>; const ll LINF = LLONG_MAX/10;
const int INF = INT_MAX/10;
const int M = 5005; const int N = 1e5+5; int a[N]; struct point{
int x, y, id;
bool operator<(const point &rhs)const{
return x < rhs.x || (x == rhs.x && y < rhs.y);
}
int dis(const point rhs) {
return abs(x - rhs.x) + abs(y - rhs.y);
};
}; struct edge{
int u, v, l;
bool operator<(const edge &rhs) {
return l < rhs.l;
};
}; ll kruskal(vector<edge> &e, int n) { // 0-indexed
dsu a(n);
vi size(n);
fill(all(size), 1); sort(all(e)); ll sum = 0;
int cnt = 0;
RNG(x, e) {
int u = a.root(x.u), v = a.root(x.v);
if(u != v){
sum += 1LL * x.l/2 * size[u] * size[v];
a.par[u] = v;
size[v] += size[u];
++cnt;
if(cnt == n - 1) break;
}
}
return sum;
}; void R1(vector<point>& a, vector<edge> &e){
// 离散化
map<int, int> ls; RNG(p, a) {
ls[p.x - p.y];
} int cnt = 0; RNG(x, ls) {
x.second = ++cnt;
} bit b(cnt, INT_MAX, true); sort(all(a)); dwn(i, a.size() - 1, 0) {
// 为了好写,规定区域 R_1(s) 为 x >= x_s, x - y <= x_s - y_s
int pos = ls[a[i].x- a[i].y];
int j = b.manhattan_min(pos);
if (j != -1) {
e.pb({a[i].id, a[j].id, a[i].dis(a[j])});
}
b.manhattan_min(pos, a[i].x + a[i].y, i);
}
} ll manhattan(vector<point> &a){
vector<edge> e;
rng(i, 0, 4){
if(i == 1 || i == 3){ // 交换x,y坐标
RNG(j, a){
swap(j.x, j.y);
}
}
else if(i == 2){
RNG(j, a){
j.x = - j.x;
}
}
R1(a, e);
} return kruskal(e, a.size());
} int main() {
// Single Cut of Failure taught me
cout << std::fixed;
cout << setprecision(10);
ios::sync_with_stdio(false);
cin.tie(nullptr); #ifdef LOCAL
freopen("main.in", "r", stdin);
// freopen("main.out", "w", stdout);
#endif int n;
scan(n);
vector<point> p(n); rng(i, 0, n){
scan(p[i].x, p[i].y);
p[i].id = i;
} println(manhattan(p)); #ifdef LOCAL
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}

备忘

$R_1(s)\colon x \ge x_s, x - y \le x_s - y_s$,

$R_2(s)\colon y \ge y_s, x - y \ge x_s - y_s$,

$R_3(s) \colon y \le y_s, x + y \ge x_s + y_s$,

$R_4(s) \colon x \ge x_s, x+y \le x_s + y_s $ 。

$R_2$ 变换到 $R_1$: $(x,y) \to (y, x)$

$R_3$ 变换的 $R_1$: $(x, y) \to (-y, x)$

$R_4$ 变换到 $R_1$:$(x, y) \to (x, -y)$

$R_1, R_2$ 的离散化部分可共用, $R_3, R_4$ 的离散化部分可共用,但上面给出的实现并未这样做,这是一个可以优化的点。

扩展

欧几里得距离最小生成树也可用类似的划分平面的方法解决。

仍然可以按 $45^\circ$ 度划分平面。$\forall a, b \in R_1(s)$,在 $\triangle sab$ 中,有 $\angle{s} < \max(\angle{a}, \angle{b})$,于是由正弦定理可知 $|ab| < \max(|sa|,|sb|)$ 。

CSA Round #84 The Sprawl的更多相关文章

  1. CSA Round 84 Growing Trees

    题目 题目大意 给定一棵有 $n$ 个节点的树,边的权值每天变化.对于第 $i$ 条边,在第 $0$ 天,其权值为 $c_i$,每天权值变化 $a_i$(即,在第 $k$ 天,其权值为 $c_i + ...

  2. CSA Round 84 Mahattan Center

    题目 题目大意 给定平面上的 $n$ 个点和常数 $k$,求 $x$ 轴上的点 $p$ 到 $n$ 个点中距其最近的 $k$ 个点的距离之和的最小值.两点之间的距离定义为曼哈顿距离. 数据范围 $1\ ...

  3. CSA Round #54 $\ $Voting

    CSA Round #54 \(\ \)Voting 题目大意: 原题网址:戳我戳我! 一次歌唱比赛中,一位歌手刚刚结束表演,评委正在打分. 一共有 \(n\) 位评委,他们每人可以打 \(1\) 分 ...

  4. Codeforces Beta Round 84 (Div. 2 Only)

    layout: post title: Codeforces Beta Round 84 (Div. 2 Only) author: "luowentaoaa" catalog: ...

  5. 题解-CSA Round#18 Randomly Permuted Costs

    Problem CSA Round 18 题意概要:给定一个有重边有自环 \(n\) 点 \(m\) 边的有向无环图(DAG),每条边有其权值,每当你走到一个点 \(x\) 时,所有从 \(x\) 连 ...

  6. Educational Codeforces Round 84 (Div. 2)

    Educational Codeforces Round 84 (Div. 2) 读题读题读题+脑筋急转弯 = =. A. Sum of Odd Integers 奇奇为奇,奇偶为偶,所以n,k奇偶性 ...

  7. csa Round #66 (Div. 2 only)

    csa66 Risk Rolls Time limit: 1000 msMemory limit: 256 MB   Alena and Boris are playing Risk today. W ...

  8. Bestcoder Round #84

    A题 Aaronson http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=718&pid=1001 感觉一 ...

  9. BestCoder Round #84 Bellovin

    Bellovin 题意: 给个中文链接:戳戳戳 题解: 这个题其实就是让你求每一位的最长公共子序列,之后输出就好了,求这个有2个算法,一个是n方,另一个nlogn,所以显然是nlogn的算法,其实这就 ...

随机推荐

  1. Linux基础精华(转)

    Linux基础精华 (继续跟新中...) 常用命令: Linux shell 环境 让你提升命令行效 率的 Bash 快捷键 [完整版] 设置你自己的liux alias Linux的Find使用 L ...

  2. IntelliJ IDEA中激活JRebel插件

    1. 下载激活软件:https://github.com/ilanyu/ReverseProxy/releases/tag/v1.0 我下载的是 2. 双击文件运行 3. 点击change licen ...

  3. C#创建和使用ActiveX组件

    开发基于.Net平台上的程序员是很难从本质上把Visual C#和ActiveX组件联起来,虽然在使用Visual C#开发应用程序时,有时为了快速开发或者由于.Net FrameWork SDK的不 ...

  4. Java基础面试题:super.getClass().getName() 执行结果是什么?

    package com.swift; import java.util.Date; public class Getclass_Test extends Date { public static vo ...

  5. JavaScript深拷贝与浅拷贝的理解

    个人是这么理解深拷贝和浅拷贝的:就是假设B复制了A,当修改A时,看B是否会发生变化,如果B也跟着变了,说明这是浅拷贝,拿人手短,如果B没变,那就是深拷贝,自食其力. 一起看看我举的浅拷贝栗子: let ...

  6. Java中的finally

    基础用法: int f1() { try{ return 1; }finally { System.out.println("finall执行"); } } @Test publi ...

  7. CMDB(资产管理系统) day1

    运维自动化最重要的就是标准化一切 自动化运维则支持以下功能: 1.OS的选择统一化,同一个项目使用同样的OS系统部署其所需要的各类软件.2.软件安装标准化,例如JAVA虚拟机,php,nginx,my ...

  8. Python_day01_作业笔记

    内容大纲: 1. python的出生与应用以及历史, python2x: 源码冗余,源码重复,源码不规范. python3x: 源码清晰优美简单.   2. python的种类. Cpython: 官 ...

  9. debug模式开启会做哪些事(源码分析)

    以往开发中不管是django框架下开发还是其它框架下开发, 只知道在开发阶段要开启debug模式, 却一直没有深究它会我们做哪些事, 今天使用tornado时偶然看到源码中写的很清楚,故写下来加深印象 ...

  10. stark组件前戏(2)之单例模式

    单,一个. 例,实例.对象. 通过利用Python模块导入的特性:在Python中,如果已经导入过的文件再被重新导入时候,python不会重新解释一遍,而是选择从内容中直接将原来导入的值拿来用.   ...