洛谷P2906 [USACO08OPEN]牛的街区Cow Neighborhoods

曼哈顿距离转切比雪夫距离

 1 #include <bits/stdc++.h>
 2 #define LL long long
 3 #define GG int
 4 #define For(i, j, k) for(register int i=j; i<=k; i++)
 5 #define Dow(i, j, k) for(register int i=j; i>=k; i--)
 6 using namespace std;
 7 GG read() {
 8     GG x = 0, f = 1;
 9     char ch = getchar();
10     while(ch<‘0‘||ch>‘9‘) { if(ch==‘-‘) f = -1; ch = getchar(); }
11     while(ch>=‘0‘&&ch<=‘9‘) { x = x*10+ch-48; ch = getchar(); }
12     return x * f;
13 }
14 void write(GG x) {
15     if(x<0) putchar(‘-‘), x = -x;
16     if(x>9) write(x/10);
17     putchar(x%10+48);
18 }
19 void writeln(GG x) { write(x); putchar(‘\n‘); }
20
21 const int N = 100011;
22 struct node{
23     LL x,y;
24     int id;
25 }a[N];
26 int n,C, Mx, ans;
27 int fa[N], tot[N];
28 multiset<node> s;
29 multiset<node> ::iterator it;
30
31 bool cmp_x(node a, node b) { return a.x < b.x; }
32 bool operator <(node a,node b) {
33     return a.y < b.y;
34 }
35
36 int find(int x) {
37     if(fa[x] == x) return x;
38     return fa[x] = find(fa[x]);
39 }
40
41 void Union(int x, int y) {
42     x = find(x); y = find(y);
43     if(x==y) return;
44     if(tot[x] < tot[y]) swap(x, y);
45     fa[y] = x; tot[x] += tot[y];
46     --ans;
47 }
48
49 void work() {
50     sort(a+1, a+n+1, cmp_x);
51     int h = 1;
52     s.insert(a[1]);
53     s.insert( (node){0ll, 1e10, 0 } );
54     s.insert( (node){0ll, -1e10, 0 } );
55     For(i, 2, n) {
56         while(a[i].x-a[h].x > C) s.erase( s.find(a[h]) ), ++h;
57         it = s.lower_bound(a[i]);
58         node p = *it;
59         if(p.y-a[i].y <= C) Union(a[i].id, p.id);
60         --it; p = *it;
61         if(a[i].y-p.y <= C) Union(a[i].id, p.id);
62         s.insert(a[i]);
63     }
64 }
65
66 int main() {
67     n = read(); C = read();
68     For(i, 1, n) {
69         int x=read(), y=read();
70         a[i].x = x+y; a[i].y=x-y;
71         a[i].id = i;
72     }
73     For(i, 1, n) fa[i] = i, tot[i]=1;
74     ans = n;
75     work();
76     For(i, 1, n) Mx = max(Mx, tot[i]);
77     printf("%d %d\n", ans, Mx);
78     /*
79     For(i, 1, n) fa[i] = find(fa[i]);
80     sort(fa+1, fa+n+1);
81     fa[0] = fa[1]+1;
82     int num = 0, len = 0, Mx = 0;
83     For(i, 1, n)
84         if(fa[i] != fa[i-1]) {
85             if(len > Mx) Mx = len;
86             len = 1;
87             ++num;
88         }
89         else ++len;
90     if(len>Mx) Mx = len;
91     printf("%d %d\n", num, Mx);
92     */
93 }

原文地址:https://www.cnblogs.com/third2333/p/8464760.html

时间: 2024-11-09 22:41:18

洛谷P2906 [USACO08OPEN]牛的街区Cow Neighborhoods的相关文章

洛谷 P2909 [USACO08OPEN]牛的车Cow Cars

P2909 [USACO08OPEN]牛的车Cow Cars 题目描述 N (1 <= N <= 50,000) cows conveniently numbered 1..N are driving in separate cars along a highway in Cowtopia. Cow i can drive in any of M different high lanes (1 <= M <= N) and can travel at a maximum speed

[USACO08OPEN]牛的街区Cow Neighborhoods

题目描述: luogu 题解: 技巧题. 曼哈顿距离:$|x1-x2|+|y1-y2|$ 切比雪夫距离:$\max(|x1-x2|,|y1-y2|)$ 曼哈顿距离转切比雪夫距离:$(x,y)->(x+y,x-y)$ 所以……排完序拿stl::set模拟就好了. 代码: #include<set> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typ

洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic

P2853 [USACO06DEC]牛的野餐Cow Picnic dfs 1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,k,p[10000],can[10000]; 4 int w[1000+15][1000+15]; 5 bool vis[10000]; 6 7 void dfs(int pre) 8 { 9 for(int j=1;j<=n;j++) 10 { 11 if(w[pre][j]&&!

洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic

P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N (1 ≤ N ≤ 1,000) pastures, conveniently numbered 1...N. The pastures are connected by M (1 ≤ M ≤ 10,000) one-way path

洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths

题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has set up a series of tolls that the cows will pay when they traverse the cowpaths throughout the farm. The cows move from any of the N (1 <= N <= 250)

洛谷 2966 [USACO09DEC]牛收费路径Cow Toll Paths

https://www.luogu.org/problem/show?pid=2966 题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has set up a series of tolls that the cows will pay when they traverse the cowpaths throughout the farm. The c

洛谷P2853 [USACO06DEC]牛的野餐Cow Picnic

题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N (1 ≤ N ≤ 1,000) pastures, conveniently numbered 1...N. The pastures are connected by M (1 ≤ M ≤ 10,000) one-way paths (no path connects a pasture to

洛谷P3045 [USACO12FEB]牛券Cow Coupons

P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 86分求救 题目描述 Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget

洛谷P2879 [USACO07JAN]区间统计Tallest Cow

To 洛谷.2879 区间统计 题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the height H (1 ≤ H ≤ 1,000,000) of the tallest cow along with th