题解 [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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容

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