CSA Round #84 The Sprawl
Analysis
曼哈顿距离($L1$ metric)最小生成树。
Implementation
#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的更多相关文章
- CSA Round 84 Growing Trees
题目 题目大意 给定一棵有 $n$ 个节点的树,边的权值每天变化.对于第 $i$ 条边,在第 $0$ 天,其权值为 $c_i$,每天权值变化 $a_i$(即,在第 $k$ 天,其权值为 $c_i + ...
- CSA Round 84 Mahattan Center
题目 题目大意 给定平面上的 $n$ 个点和常数 $k$,求 $x$ 轴上的点 $p$ 到 $n$ 个点中距其最近的 $k$ 个点的距离之和的最小值.两点之间的距离定义为曼哈顿距离. 数据范围 $1\ ...
- CSA Round #54 $\ $Voting
CSA Round #54 \(\ \)Voting 题目大意: 原题网址:戳我戳我! 一次歌唱比赛中,一位歌手刚刚结束表演,评委正在打分. 一共有 \(n\) 位评委,他们每人可以打 \(1\) 分 ...
- Codeforces Beta Round 84 (Div. 2 Only)
layout: post title: Codeforces Beta Round 84 (Div. 2 Only) author: "luowentaoaa" catalog: ...
- 题解-CSA Round#18 Randomly Permuted Costs
Problem CSA Round 18 题意概要:给定一个有重边有自环 \(n\) 点 \(m\) 边的有向无环图(DAG),每条边有其权值,每当你走到一个点 \(x\) 时,所有从 \(x\) 连 ...
- Educational Codeforces Round 84 (Div. 2)
Educational Codeforces Round 84 (Div. 2) 读题读题读题+脑筋急转弯 = =. A. Sum of Odd Integers 奇奇为奇,奇偶为偶,所以n,k奇偶性 ...
- csa Round #66 (Div. 2 only)
csa66 Risk Rolls Time limit: 1000 msMemory limit: 256 MB Alena and Boris are playing Risk today. W ...
- Bestcoder Round #84
A题 Aaronson http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=718&pid=1001 感觉一 ...
- BestCoder Round #84 Bellovin
Bellovin 题意: 给个中文链接:戳戳戳 题解: 这个题其实就是让你求每一位的最长公共子序列,之后输出就好了,求这个有2个算法,一个是n方,另一个nlogn,所以显然是nlogn的算法,其实这就 ...
随机推荐
- Problem K: 搜索基础之棋盘问题
Problem K: 搜索基础之棋盘问题 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 92 Solved: 53[Submit][Status][W ...
- WPF实现ListView大小图标和分组
XAML: <ListView Grid.Row="3" Name="t_lvw_time" HorizontalAlignment="Stre ...
- Java设计模式学习——设计原则
第一章 设计原则 1.开闭原则 一个软件实体,像类,模块,函数应该对扩展开放,对修改关闭 在设计的时候,要时刻考虑,让这个类尽量的好,写好了就不要去修改.如果有新的需求来,在增加一个类就完事了,原来的 ...
- mysql 绿色版 安装
1.下载mysql的绿色版压缩包.(自行百度) 2.自行规划目录解压 3.安装 注意:我遇到的两种版本的安装方法(暂时按照解压后的目录中是否含有data目录来区分) 3.1 无data目录(我用到的的 ...
- 安装配置mysql图文步骤以及配置mysql的环境变量的步骤
MySQL下载地址:http://dev.mysql.com/downloads/installer/ 我的数据库是5.5.21这个版本的.其实可以一直点击next,直到出现第14张图,从这里开始要注 ...
- asp.net 中 UpdataPanel 的使用注意点
1. 在UpdataPanel 前必须加上asp:ScriptManager的控件,保证页面能够正常显示
- 九、Shell 流程控制
Shell 流程控制 和Java.PHP等语言不一样,sh的流程控制不可为空,如(以下为PHP流程控制写法): <?php if (isset($_GET["q"])) { ...
- tcl之list操作
- JZOJ 3385. 【NOIP2013模拟】黑魔法师之门
3385. [NOIP2013模拟]黑魔法师之门 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Detailed Limi ...
- day 59 MySQL之锁、事务、优化、OLAP、OLTP
MySQL之锁.事务.优化.OLAP.OLTP 本节目录 一 锁的分类及特性 二 表级锁定(MyISAM举例) 三 行级锁定 四 查看死锁.解除锁 五 事务 六 慢日志.执行计划.sql优化 七 ...