9192. Three Lines
限制条件
时间限制: 1 秒, 内存限制: 256 兆
题目描述
Farmer John wants to monitor his N cows (1 <= N <= 50,000) using a new surveillance system he has purchased. The ith cow is located at position (x_i, y_i) with integer coordinates (in the range 0...1,000,000,000); no two cows occupy the same position. FJ's surveillance system contains three special cameras, each of which is capable of observing all the cows along either a vertical or horizontal line. Please determine if it is possible for FJ to set up these three cameras so that he can monitor all N cows. That is, please determine if the N locations of the cows can all be simultaneously "covered" by some set of three lines, each of which is oriented either horizontally or vertically.
输入格式
Line 1: The integer N.
Lines 2..1+N: Line i+1 contains the space-separated integer x_i and y_i giving the location of cow i.
输出格式
Please output 1 if it is possible to monitor all N cows with three cameras, or 0 if not.
样例输入
6 1 7 0 0 1 2 2 0 1 4 3 4
样例输出
1
提示
In the sample, there are 6 cows, at positions (1,7), (0,0), (1,2), (2,0), (1,4), and (3,4). The lines y=0, x=1, and y=4 are each either horizontal or vertical, and collectively they contain all N of the cow locations.
题目来源
2013年每周一赛第十一场
这道题讲的是一个矩阵删除任意的三行、三列之后所有的点都被删除了,任何行数、列数小于
3的矩阵都不用考虑直接输出1(为1,4情况),接下来考虑的是2,3,我的方法是删除数字最多的两行或两列看列数或行数是否《=1,如果成立则输出1,否则为0
这道题我感受到自己的编程能力还很弱,首先编程过程中变量太多,自己有时候会感觉的很烦,编程过程中复制粘贴会忘记改,导致了很多错误
变量忘记初始化,最关键的的是有时候想问题太复杂了,导致自己不能完成程序。
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
const int maxn = 50010;
using namespace std;
struct point
{
int x,y;
}map1[maxn],map2[maxn];
int n;
struct point visit1[maxn],visit2[maxn];
bool cmpx(struct point a,struct point b)//设置比较函数
{
return a.x>b.x? true : false;
}
bool cmpy(struct point a,struct point b)
{
return a.y>b.y ?true : false;
}
int count( struct point map[])
{
int t1 = 0, t2 = 0,i;
sort(map,map+n,cmpx);
for(i=1; i <n; i++)
{
if(map[i].x != map[i-1].x)
t1++;
}
sort(map,map+n,cmpy);
for(i=1; i <n; i++)
{
if(map[i].y!= map[i-1].y)
t2++;
}
if(t1<=1||t2<=1)
return 1;
else
return 0;
}
int main()
{
int i,k,l,c,flage;
while(scanf("%d",&n)!=EOF)
{
memset(map1,-1,sizeof(map1));
memset(map2,-1,sizeof(map1));
memset(visit1,0,sizeof(visit1));
memset(visit2,0,sizeof(visit2));
flage=0;
for(i = 0; i < n; i++)
{
scanf("%d%d", &map1[i].x , &map1[i].y );
map2[i] = map1[i];
}
sort(map1, map1+n, cmpx);//对点的x,y进行排序
sort(map2, map2+n, cmpy);
l=0;
k=0;
int len1=0;
int len2=0;
for(i=0;i<n;i++)//寻找同行和同列的元素的个数用visit1,visit2记录
{
c=map1[l].x;
visit1[k].x = 0;
while(c==map1[l].x)
{
visit1[k].x++;
visit1[k].y=i;
l++;
}
i=l-1;
k++;
}
len1=k;
sort(visit1,visit1+k,cmpx);
k=0;
l=0;
for(i=0;i<n;i++)
{
c=map2[l].y;
visit2[k].x = 0;
while(c==map2[l].y)
{
visit2[k].x++;//z这里的赋值错误找了很久
visit2[k].y=i;
l++;
}
i=l-1;
k++;
}
len2=k;
sort(visit2,visit2+k,cmpx);
int t1 = 0, t2 = 0;
if(len1<=3||len2<=3)//对行或者列《=3的直接输出
printf("1\n");
else
{
int max1,max2;
max1=map1[visit1[0].y].x;
max2=map1[visit1[1].y].x;
for(i=0;i<n;i++)//先对第二种情况分析
{
if(map1[i].x==max1||map1[i].x==max2)
{
map1[i].x=-1;
map1[i].y=-1;
}
}
if(count(map1))
{
flage=1;
}
else
{
max1=map2[visit2[0].y].y;
max2=map2[visit2[1].y].y;
for(i=0;i<n;i++)//对第三种分析
{
if(map2[i].y==max1||map2[i].y==max2)
{
map2[i].x=-1;
map2[i].y=-1;
}
}
if(count(map2))
{
flage=1;
}
}
if(flage==1)
printf("1\n");
else
printf("0\n");
}
}
return 0;
}