HDU5762 Teacher Bo 多校联赛第三场1011
来源:互联网 发布:守望先锋手机数据查询 编辑:程序博客网 时间:2024/05/18 20:08
Teacher Bo
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1532 Accepted Submission(s): 593
Problem Description
Teacher BoBo is a geography teacher in the school.One day in his class,he marked N points in the map,the i -th point is at (Xi,Yi) .He wonders,whether there is a tetrad (A,B,C,D)(A<B,C<D,A≠CorB≠D) such that the manhattan distance between A and B is equal to the manhattan distance between C and D.
If there exists such tetrad,print "YES",else print "NO".
If there exists such tetrad,print "YES",else print "NO".
Input
First line, an integer T . There are T test cases.(T≤50)
In each test case,the first line contains two intergers, N, M, means the number of points and the range of the coordinates.(N,M≤105) .
Next N lines, thei -th line shows the coordinate of the i -th point.(Xi,Yi)(0≤Xi,Yi≤M) .
In each test case,the first line contains two intergers, N, M, means the number of points and the range of the coordinates.
Next N lines, the
Output
Sample Input
23 101 12 23 34 108 82 33 34 4
Sample Output
YESNO
题解
曼哈顿距离 l=|x1-x2|+|y1-y2|
考虑一种暴力,每次枚举两两点对之间的曼哈顿距离,并开一个桶记录每种距离是否出现过,如果某次枚举出现了以前出现的距离就输 YES ,否则就输 NO .
注意到曼哈顿距离只有 O(M) 种,根据鸽笼原理,上面的算法在 O(M) 步之内一定会停止.所以是可以过得.
一组数据的时间复杂度 O(min{N2,M}) .
方法一
#include<stdio.h>#include<algorithm>#include<iostream>#include<string.h>using namespace std;int dp[2000005];struct node{ int x,y;}a[100005];int cmp(node a,node b){ if(a.x!=b.x) return a.x<b.x; else return a.y<b.y;}int main(){ int t; scanf("%d",&t); while(t--) { int n,m,i,j; memset(dp,0,sizeof(dp)); scanf("%d%d",&n,&m); for(int i=0;i<n;i++) { scanf("%d%d",&a[i].x,&a[i].y); } sort(a,a+n,cmp); for(i=1;i<n;i++) { if(a[i].x==a[i-1].x&&a[i].y==a[i-1].y) { for(j=i+1;j<n;j++) { a[j]=a[j-1]; } n--; i--; } } int flag=0; for(i=0;i<n;i++) { if(flag==1) break; for(j=i+1;j<n;j++) { int ss=abs(a[i].x-a[j].x)+abs(a[i].y-a[j].y); dp[ss]++; if(dp[ss]>=2) { flag=1; break; } } } if(flag==1) printf("YES\n"); else printf("NO\n"); }}第二种方法
#include<bits/stdc++.h>using namespace std;int dp[2000005];struct node{ int x,y;} a[100005];int cmp(node a,node b){ if(a.x!=b.x) return a.x<b.x; else return a.y<b.y;}int main(){ int t; scanf("%d",&t); while(t--) { int n,m,i,j; scanf("%d%d",&n,&m); set<int>u; for(int i=0; i<n; i++) { scanf("%d%d",&a[i].x,&a[i].y); } sort(a,a+n,cmp); for(i=1; i<n; i++) { if(a[i].x==a[i-1].x&&a[i].y==a[i-1].y) { for(j=i+1; j<n; j++) { a[j]=a[j-1]; } n--; i--; } } int flag=0; for(i=0; i<n; i++) { if(flag==1) break; for(j=i+1; j<n; j++) { int ss=abs(a[i].x-a[j].x)+abs(a[i].y-a[j].y); if(u.find(ss)!=u.end()) { flag=1; break; } u.insert(ss); } } if(flag==1) printf("YES\n"); else printf("NO\n"); }}
0 0
- HDU5762 Teacher Bo 多校联赛第三场1011
- HDU5762 多校联合3 Teacher Bo
- HDU5762 Teacher Bo
- hdu5762 Teacher Bo
- HDU5753 Permutation Bo 多校联赛第三场
- hdu5762 Teacher Bo (暴力)
- 【大胆乱搞】HDU5762 Teacher Bo
- HDU5762[Teacher Bo]--暴力枚举
- 2016 多校联训 第三场Teacher Bo hdoj5762
- HDU5762 Teacher Bo 带处理的暴力
- [杂题 简单复杂度分析]HDU5762. Teacher Bo
- 【复杂度分析】HDU5762[Teacher Bo]题解
- hdu5762(2016多校第三场,思维题)
- 多校联赛 第三场
- 多校联赛第三场
- 2016第三场多校联盟训练1011 Teacher Bo(曼哈顿距离)
- HDU 5762 Teacher Bo 2016多校赛第三场 鸽巢原理
- 2016多校联合第三场 HDU5758 Explorer Bo
- Delphi 属性多参数使用
- 由多线程内存溢出产生的实战分析
- 多线程编程——互斥对象
- 错误:Undefined symbols for architecture armv7
- 分布式锁redis实现
- HDU5762 Teacher Bo 多校联赛第三场1011
- Hibernate中通过HQL/JPQL查询的方式实现动态数据获取
- hdu5755Gambler Bo
- 基本的sql语句
- HTTP tunneling工具-端口转发
- ERROR C2514:XXX类没有构造函数
- Sublime text 3最新版破解方法
- HDU 2199 Can you solve this equation?(二分法求近似解)
- 打开文件和写文件的操作