P3033 [USACO11NOV]Cow Steeplechase G

P3033 [USACO11NOV]Cow Steeplechase G,第1张

P3033 [USACO11NOV]Cow Steeplechase G

提交1.80k

通过638

时间限制1.00s

内存限制125.00MB

提交答案加入题单

复制题目

题目提供者FarmerJohn2

难度提高+/省选-

历史分数100

 提交记录  查看题解

标签

USACO2011

 查看算法标签

进入讨论版

相关讨论

 查看讨论

推荐题目

 查看推荐

 洛谷推荐关闭

 展开

题目描述

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.

 【AC代码】
#include
using namespace std;
typedef long long ll;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
inline int read()
{
	char ch=getchar();
	int n=0,m=1;
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')m=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
	return n*m;
}
void write(int n)
{
	if(n>9)write(n/10);
	putchar(n%10+'0');
}
int n,x1[N],x2[N],yy1[N],y2[N],a[N],ans,vis[N],head[N],to[N],ne[N],id,d[N];
void add(int x,int y)
{
	to[++id]=y,ne[id]=head[x],head[x]=id;
}
bool dfs(int u)
{
	for(int i=head[u];i;i=ne[i])
	{
		
		int v=to[i];
		if(vis[v])continue;
		vis[v]=1;
		if(!d[v]||dfs(d[v]))
		{
			d[v]=u;
			return true;
		}
	}
	return false;
}
int main(int argc,char **argv)
{
	n=read();
	for(int i=1;i<=n;i++)
	{
		x1[i]=read(),yy1[i]=read(),x2[i]=read(),y2[i]=read();
		if(x1[i]>x2[i])swap(x1[i],x2[i]);
		if(yy1[i]>y2[i])swap(yy1[i],y2[i]);
		a[i]=(x1[i]==x2[i])?1:2;
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			if(a[i]==1&&a[j]==2)
			{
				if(x1[j]<=x1[i]&&x1[i]<=x2[j]&&yy1[j]>=yy1[i]&&y2[j]<=y2[i])add(i,j);
			}
			if(a[i]==2&&a[j]==1)
			{
				if(x1[j]>=x1[i]&&x2[j]<=x2[i]&&yy1[i]>=yy1[j]&&y2[i]<=y2[j])add(j,i);
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(a[i]==2)continue;
		memset(vis,0,sizeof vis);
		if(dfs(i))ans++;
	}
	ans=n-ans,write(ans);
	return 0;
}

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/674616.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-19
下一篇 2022-04-19

发表评论

登录后才能评论

评论列表(0条)

保存