[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;
typedef long long ll;
const int N = 100050;
template<typename T>
inline void read(T&x)
{
    T f = 1,c = 0;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){c=c*10+ch-‘0‘;ch=getchar();}
    x = f*c;
}
int n,c,ff[N],siz[N];
int findff(int x){return x==ff[x]?x:ff[x]=findff(ff[x]);}
void merge(int x,int y)
{
    x = findff(x),y = findff(y);
    if(x!=y)ff[x] = y,siz[y]+=siz[x];
}
struct Point
{
    ll x,y;int id;
    Point(){}
    Point(ll x,ll y):x(x),y(y){}
    bool operator < (const Point&a)const{return x!=a.x?x<a.x:y<a.y;}
}p[N];
bool cmp(Point a,Point b){return a.y!=b.y?a.y<b.y:a.x<b.x;}
set<Point>tr;
int main()
{
    read(n),read(c);ll x,y;
    for(int i=1;i<=n;i++)
    {
        read(x),read(y);
        p[i] = Point(x+y,x-y);
    }
    sort(p+1,p+1+n,cmp);
    for(int i=1;i<=n;i++)
        siz[i]=1,ff[i]=i,p[i].id=i;
    set<Point>::iterator it;Point now;
    for(int i=1,j=1;i<=n;i++)
    {
        while(p[i].y-p[j].y>c)tr.erase(p[j]),j++;
        it = tr.lower_bound(p[i]);
        if(it!=tr.end())
        {
            now=(*it);
            if(now.x-p[i].x<=c)merge(now.id,i);
        }
        if(it!=tr.begin())
        {
            it--;now=(*it);
            if(p[i].x-now.x<=c)merge(now.id,i);
        }
        tr.insert(p[i]);
    }
    int ans1 = 0,ans2 = -1;
    for(int i=1;i<=n;i++)
        if(findff(i)==i)ans1++,ans2=max(ans2,siz[i]);
    printf("%d %d\n",ans1,ans2);
    return 0;
}

原文地址:https://www.cnblogs.com/LiGuanlin1124/p/11094565.html

时间: 2024-11-02 17:34:36

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

洛谷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

洛谷 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 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 of S_i (1 <= S_i <= 1,00

bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set

Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个"群".每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤[1..10^9]:Xi,Yi∈整数.当满足下列两个条件之一,两只奶牛i和j是属于同一个群的: 1.两只奶牛的曼哈顿距离不超过C(1≤C≤10^9),即lXi - xil+IYi - Yil≤C. 2.两只奶牛有共同的邻居.即,存在一只奶牛k,使i与k,j与k均同属一个群.

bzoj1604[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居*

bzoj1604[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 题意: n只牛,牛结成群当且仅当两只牛曼哈顿距离≤c或存在第三头牛使两头牛与它的曼哈顿距离都≤c,求最大的群和群数.n≤100000 题解: 好神啊.先把曼哈顿距离转成切比雪夫距离,(x,y)转为(x+y,x-y).然后按x坐标排序,用两个指针维护使x坐标差值≤c,同时将新插入的y坐标放入set,每次在set里查找出与当前y差值不超过c的最大y,将这两个点合并成一个集合,用并查集维护. 代码: 1 #

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

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 mo

编程算法 - 最好牛线(Best Cow Line) 代码(C)

最好牛线(Best Cow Line) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定长度为N的字符串S, 要构造一个长度为N的字符串T. 反复进行如下任意操作. 从S的头部删除一个字符, 放入T的尾部; 从S的尾部删除一个字符, 放入T的尾部; 目标是要构造字典序尽可能小的字符串T. 使用贪心算法, 不断选取S首尾最小的字符, 放入T, 如果相等, 则再次向内查找, 找到内部最小的. 代码: /* * main.cpp * * Cr

洛谷 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