题解 [USACO11NOV]牛的障碍Cow Steeplechase

Chtholly

传送门 Luogu P3033 [USACO11NOV]牛的障碍Cow Steeplechase

题目描述

Farmer John has a brilliant idea for the next great spectator sport: Cow Steeplechase! As everyone knows, regular steeplechase involves a group of horses that race around a course filled with obstacles they must jump over. FJ figures the same contest should work with highly-trained cows, as long as the obstacles are made short enough.

In order to design his course, FJ makes a diagram of all the N (1 <= N <= 250) possible obstacles he could potentially build. Each one is represented by a line segment in the 2D plane that is parallel to the horizontal or vertical axis. Obstacle i has distinct endpoints (X1_i, Y1_i) and (X2_i, Y2_i) (1 <= X1_i, Y1_i, X2_i, Y2_i <= 1,000,000,000). An example is as follows:



FJ would like to build as many of these obstacles as possible, subject to the constraint that no two of them intersect. Starting with the diagram above, FJ can build 7 obstacles:



Two segments are said to intersect if they share any point in common, even an endpoint of one or both of the segments. FJ is certain that no two horizontal segments in the original input diagram will intersect, and that similarly no two vertical segments in the input diagram will intersect.

Please help FJ determine the maximum number of obstacles he can build.

给出N平行于坐标轴的线段,要你选出尽量多的线段使得这些线段两两没有交点(顶点也算),横的与横的,竖的与竖的线段之间保证没有交点,输出最多能选出多少条线段。

输入输出格式

输入格式

  • Line 1: A single integer: N.

  • Lines 2..N+1: Line i+1 contains four space-separated integers representing an obstacle: X1_i, Y1_i, X2_i, and Y2_i.
    输出格式

  • Line 1: The maximum number of non-crossing segments FJ can choose.

输入输出样例

输入样例#1

3
4 5 10 5
6 2 6 12
8 3 8 5

输出样例#1

2

说明

There are three potential obstacles. The first is a horizontal segment connecting (4, 5) to (10, 5); the second and third are vertical segments connecting (6, 2) to (6, 12) and (8, 3) to (8, 5).

The optimal solution is to choose both vertical segments.

解题思路

根据题意,我们得出如果有两条线段之间有交点,那么这两条线段就只能选择其中一条,所以我们把有交点的线段之间连边,跑一遍匈牙利,找出最大匹配数,然后用n一减就好了
下面是全洛谷跑的最快内存最小最蒟蒻的代码

C++代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define rr register
#define forvec(T,a,it) for(vector<T>::iterator it=a.begin();it!=a.end();++it)
using namespace std;

const int maxn=255;
int n,sum=0;
struct node{
    int pos,far,near;
}heng[maxn],shu[maxn];
int c_heng,c_shu;
int link[maxn],vis[maxn];

inline int read(){
    int chtholly=0,willem=1;char c=getchar();
    while(c<'0' || c>'9'){if(c=='-')willem=-1;c=getchar();}
    while(c<='9' && c>='0'){chtholly=chtholly*10+(int)(c-'0');c=getchar();}
    return chtholly*willem;
}

inline bool you_jiao_dian(int i,int j){
    if(heng[i].far>=shu[j].pos && heng[i].near<=shu[j].pos && shu[j].far>=heng[i].pos && shu[j].near<=heng[i].pos)
        return 1;
    return 0;
}

inline bool dfs(int x,vector<int>*a){
    forvec(int,a[x],it){
        int v=*it;
        if(!vis[v]){
            vis[v]=1;
            if(link[v]==-1 || dfs(link[v],a)){
                link[v]=x;return 1;
            }
        }
    }
    return 0;
}

inline void max_match(vector<int>*a){
    memset(link,0xff,sizeof(link));
    for(rr int i=1;i<=c_heng;++i){
        memset(vis,0,sizeof(vis));
        if(dfs(i,a))sum++;
    }
}

int main(){
    n=read();
    vector<int>a[n+1];
    for(rr int i=1;i<=n;++i){
        int x1=read(),y1=read(),x2=read(),y2=read();
        if(x1==x2){
            shu[++c_shu].pos=x1;
            shu[c_shu].far=max(y1,y2);
            shu[c_shu].near=min(y1,y2);
        } else if(y1==y2){
            heng[++c_heng].pos=y1;
            heng[c_heng].far=max(x1,x2);
            heng[c_heng].near=min(x1,x2);
        }
    }
    for(rr int i=1;i<=c_heng;++i)
        for(rr int j=1;j<=c_shu;++j)
            if(you_jiao_dian(i,j))
                a[i].push_back(j);
    max_match(a);
    printf("%d\n",n-sum);
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,430评论 0 23
  • 以前曾经去故宫参观过一次,但也只是走马观花,匆匆而过。未完全开放的故宫对我来说,依然是神秘未知的,那些珍贵文物对肤...
    果昊妈妈阅读 3,294评论 0 0
  • 年轻时,我家里穷,孩子多,我是最年长的,所以读到高二就辍学了。参加工作以后几十年如一日兢兢业业地当工人。结婚以后更...
    大花小素阅读 2,895评论 0 0
  • 【编者按】该系列所有文章都全部来源于真实生活,大多是作者本人的亲身经历,也有一些以第一人称叙述的朋友发生的故事。曾...
    佛多锡墨理氏阅读 1,755评论 0 0
  • 文.贺之 五点钟的米粥 开出的粥花真的很漂亮 肩背上的爱人 虽疲惫却芬芳依旧 东半球的太阳 炽热而执着 西半球的月...
    刘罡Liam阅读 1,755评论 0 0