poj2780Linearity【共线】
2015-10-10 21:32
260 查看
Language: Default Linearity
Alice often examines star maps where stars are represented by points in a plane and there is a Cartesian coordinate for each star. Let the Linearity of a star map be the maximum number of stars in a straight line. For example, look at the star map shown on the figure above, the Linearity of this map is 3, because the star 1, star 2, and star 5 are within the same straight line, and there is no straight line that passes 4 stars. You are to write a program to find the Linearity of a star map. Input Input will contain multiple test cases. Each describes a star map. For each test case, the first line of the input contains the number of stars N (2 <= N <= 1000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0 <= X, Y <= 1000). There can be only one star at one point of the plane. Output Output the Linearity of the map in a single line. Sample Input 5 0 0 2 0 0 2 1 1 2 2 Sample Output 3 题意:求一条直线与平面最多的点数相交; 本来还想用我写lightoj1292的代码来个1A却TLE了然后各种优化还是TLE无奈看了别人的思路固定一点求它到其他个点的斜率然后计算斜率相等的最大数目即可 #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #define inf 1e20 #define eps 1e-10 using namespace std; struct point{ double x,y; }A[1010]; int seg(double n){ if(fabs(n)<eps)return 0; else if(n<0)return -1; return 1; } int MAX(int a,int b){ return a>b?a:b; } double K[1010]; bool cmp(double a,double b){ return a<b; } double Slop(point a,point b){ if(seg(a.x-b.x)==0)return inf; return (a.y-b.y)/(a.x-b.x); } int main() { int n,i,j,k,ans,temp,m; while(scanf("%d",&n)!=EOF){ for(i=0;i<n;++i){ scanf("%lf%lf",&A[i].x,&A[i].y); } for(ans=0,m=0,i=0;i<n;++i,m=0){ for(j=i+1;j<n;++j){ K[m++]=Slop(A[i],A[j]); } sort(K,K+m,cmp);temp=1; for(int v=1;v<m;++v){ if(seg(K[v]-K[v-1])==0)temp++; else{ ans=MAX(ans,temp); temp=1; } } ans=MAX(ans,temp); } printf("%d\n",ans+1); } return 0; } |
相关文章推荐
- 黑马程序员——JavaSE之IO流二
- 二叉树学习笔记之二叉查找树(BSTree)
- 定制一下最近一段时间的学习计划
- 类的静态成员函数和静态数据成员
- POJ1930,无限循环小数变分数
- json解析,异步下载(listview仅滑动时加载)Demo总结
- json解析,异步下载(listview仅滑动时加载)Demo总结
- Request简单总结
- 分布式文件系统
- 【---重要---】 iOS:界面适配
- TCP/IP协议原理与应用笔记15:网络连接设备
- DLX Dancing Links X Algorithm 舞蹈链 学习总结
- Python base64模块
- C++primer学习:关联容器(3)
- 活用各种数据结构——RMQ/树状数组/分桶法和平方分割
- CSS优先级
- 时间点的类Date和日历表示的类GregorianCalendar
- 百度贴吧自动图片下载爬虫
- 安卓长按后的释放响应(长按模仿iphone6s的3DTouch)
- 简单的控件自适应