(Relax ST1.13)POJ 2780 Linearity(给出若干个点,求最多有多少个点共线,不能使用n^3算法)
2013-11-29 16:35
489 查看
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; const int maxn = 1010; const int inf = INT_MIN; struct Point{ int x; int y; bool operator<(const Point& b)const{//排序规则这里怎么定义都无所谓,但是一定要定义。。。 if(x == b.x){ return y < b.y; } return x < b.x; } }p[maxn]; double cal(const Point& a,const Point& b){ if(b.x == a.x){ return inf; } return (b.y - a.y)*1.0/(b.x - a.x); } int main(){ int n; while(scanf("%d",&n)!=EOF){ int maxm = -10; double xielv[maxn*2]; int i; for(i = 0 ; i < n ; ++i){ scanf("%d%d",&p[i].x,&p[i].y); } sort(p,p+n); int j; for(i = 0 ; i < n-1 ; ++i){ int index = 0; for(j = i+1 ; j < n ; ++j){ xielv[index++] = cal(p[i],p[j]); } sort(xielv,xielv+index);//对斜率进行排序.... /** * 将斜率数组排序以后,这时候可能就会出现这么一种情况, * 前后可能有好几个斜率是一样的... * 以下就是数这样的斜率线每段有多少个... */ for(j = 0 ; j < index ; ++j){ int count = 0; while(fabs(xielv[j] - xielv[j+1]) == 0){ count++; j++; } maxm = (maxm>count)?maxm:count; } } // printf("%d\n",maxm+2); cout<<maxm+2<<endl; } return 0; }
相关文章推荐
- (Relax ST1.12)POJ 2606 Rabbit hunt(给出若干个点,求最多能有多少个点共线)
- poj 2780 Linearity 最多共线点经典问题
- (Relax DFS1.2)POJ 2386 Lake Counting(使用DFS来计算有多少坨东西是连通的)
- (Relax ST1.6)POJ 1218 THE DRUNK JAILER(求1-n之间有多少个平方数)
- Hdu 4052 Adding New Machine(给你W*H大小的矩形,其中有N个地区不能使用(给出了这个地区的两个顶点的坐标即(x1,y1)和(x2,y2)),问能下多少个1*M的矩形)
- (Relax 数论1.13)POJ 2909 Goldbach's Conjecture(欧拉筛法的应用:给出一个偶数,要求在这个偶数的范围内,有几对素数和是等于这个偶数的,注意素数对的不重复性! )
- (Relax ST1.16)POJ 3589 Number-guessing Game(求连个字符串中相同的字符有多少个和同位置&&同字符的字符个数有多少个)
- 使用C++设计一个类,该类最多能被实例化3次且不能被继承
- 使用python测测你的系统最多能创建多少个线程
- (Relax ST1.17)POJ 3618 Exploration(求在t时间内能去n个景点中的几个)
- (Relax DFS1.3)POJ 1321 棋盘问题(使用DFS来解决特定终点问题)
- poj 1631 最多能有多少条不交叉的线 最大非降子序列 (LIS)
- (Relax 水题1.2)POJ 1032 Parliament(将n分解成若干个互不相等的整数的和,并且是这些整数的乘积最大)
- 【翻译自mos文章】在11.2中,使用DBMS_SERVICE.CREATE_SERVICE 能最多创建多少个service?
- 题目:给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符。
- poj 1118 Lining Up || poj 2780 Linearity || poj 2606 Rabbit hunt
- (Relax DP1.5)POJ 1458 Common Subsequence(使用dp来求解最长公共子串)
- MongoDB 中最多可以使用多少个集合
- poj 1185 炮兵阵地 在n*m的矩阵中放置炮使其不能互相攻击的最多放置炮的个数
- 用天平(只能比较,不能称重)从一堆小球中找出其中唯一一个较轻的,使用x 次天平, 最多可以从y 个小球中找出较轻的那个,求y 与x 的关系式。