P3033 [USACO11NOV]Cow Steeplechase G
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<bits/stdc++.h>
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;
}
P3033 [USACO11NOV]Cow Steeplechase G相关推荐
- P3033 [USACO11NOV]牛的障碍Cow Steeplechase
P3033 [USACO11NOV]牛的障碍Cow Steeplechase 题目描述 详见:P3033 [USACO11NOV]牛的障碍Cow Steeplechase solution 裸题. 对 ...
- P2870 [USACO07DEC]Best Cow Line G
P2870 [USACO07DEC]Best Cow Line G 题意: 给你一个字符串,每次从首或尾取一个字符组成字符串,问所有能够组成的字符串中字典序最小的一个. 题解: 现在要组成字典序最小的 ...
- 专题突破之反悔贪心——建筑抢修,Cow Coupons G, Voting (Hard Version),Cardboard Box
文章目录 [JSOI2007]建筑抢修 [USACO12FEB]Cow Coupons G CF1251E2 Voting (Hard Version) CF436E Cardboard Box [J ...
- 牛的障碍Cow Steeplechase
题目描述 Farmer John has a brilliant idea for the next great spectator sport: Cow Steeplechase! As every ...
- [USACO11NOV]牛的障碍Cow Steeplechase(匈牙利算法)
洛谷传送门 题目描述: 给出N平行于坐标轴的线段,要你选出尽量多的线段使得这些线段两两没有交点(顶点也算),横的与横的,竖的与竖的线段之间保证没有交点,输出最多能选出多少条线段. 因为横的与横的,竖的 ...
- P2906 [USACO08OPEN]Cow Neighborhoods G 切比雪夫距离 + 并查集 + set
传送门 考虑将曼哈顿距离转换成切比雪夫距离,这样问题就变成了max(∣x1−x2∣,∣y1−y2∣)≤dmax(|x_1-x_2|,|y_1-y_2|)\le dmax(∣x1−x2∣,∣y1− ...
- P3067 [USACO12OPEN]Balanced Cow Subsets G 折半搜索
传送门 文章目录 目录 题意: 思路: 目录 题意: 给你nnn个数,从中任意选出一组数,使这些数能分成和相等的两组,问有多少种选数方案. 2≤n≤20,1≤ai≤1e92\le n\le 20,1\ ...
- P5200 [USACO19JAN]Sleepy Cow Sorting G
题目描述 Farmer John正在尝试将他的 NN 头奶牛(1\le N\le 10^51≤N≤105),方便起见编号为 1\ldots N1-N,在她们前往牧草地吃早餐之前排好顺序. 当前,这些奶 ...
- NOIP复健计划——动态规划
树形DP [POI2011] DYN-Dynamite 二分 K K K check(mid): 能否选出 m m m个点,使得 ∀ i 为关键点, M i n j i s s e l e c t e ...
最新文章
- 使用rsync同步linux服务器上的文件到windows上
- 成功解决AttributeError: module ‘enum‘ has no attribute ‘IntFlag‘?
- HB-X打不开的解决办法
- 数据库实验三 SQL查询数据
- linux安装python3.7的步骤_Linux 安装python3.7.3
- java编程规范每行代码窄字符,wiki/0xFE_编程规范.md at master · islibra/wiki · GitHub
- threadlocal内存泄露_ThreadLocal用法详解和原理
- 提醒!赶快弃掉这个区块链平台!
- Docker入门安装教程
- 详解谷歌官方教程 Android插件ADT 9.0.0
- 从0开始学习 GitHub 系列之「07.GitHub 常见的几种操作」
- 使用oracle执行txt语句,oracle常用SQL语句.txt
- TLSF内存分配器记录
- 虚拟服务器不识别网银盾,建行u盾插电脑没反应网页不自动跳出 3步教你快速解决...
- 基于junit4的关于个人所得税计算的等价类与边界值_微服务的未来——从单体服务角度看微服务与云计算DevOps结合的演进...
- linux双系统无u盘安装教程视频教程,U盘安装Windows和Ubuntu 15.04双系统图解教程
- 幼麟棋牌登录流程分析
- MatplotlibTutorial——Matplotlib的基本使用【Jupiter Notebook代码】
- ch341a_USB转串口/并口驱动
- 【NGINX】nginx+uwsgi+django+python部署总结