Revenge of Collinearity

Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 612    Accepted Submission(s): 207

Problem Description
In
geometry, collinearity is a property of a set of points, specifically,
the property of lying on a single line. A set of points with this
property is said to be collinear (often misspelled as colinear).
---Wikipedia

Today,
Collinearity takes revenge on you. Given a set of N points in
two-dimensional coordinate system, you have to find how many set of
<Pi, Pj, Pk> from these N points are collinear. Note that <Pi, Pj, Pk> cannot contains same point, and <Pi, Pj, Pk> and <Pi, Pk, Pj> are considered as the same set, i.e. the order in the set doesn’t matter.

 
Input
The first line contains a single integer T, indicating the number of test cases.

Each test case begins with an integer N, following N lines, each line contains two integers Xi and Yi, describing a point.

[Technical Specification]
1. 1 <= T <= 33
2. 3 <= N <= 1 000
3. -1 000 000 000 <= Xi, Yi <= 1 000 000 000, and no two points are identical.
4. The ratio of test cases with N > 100 is less than 25%.

 
Output
For each query, output the number of three points set which are collinear.
 
Sample Input
2
3
1 1
2 2
3 3
4
0 0
1 0
0 1
1 1
 
Sample Output
1
0
 
Source
 
题意:平面上有n个点,统计有多少个三个点在一条直线上(注意:(1,2,3)和(1,3,2)相同)。
题解:这题O(n^3)会炸。。所以想办法优化,然后就想到斜率。但是斜率不存在怎么办??
1.然后看了别人的代码,求出 y1 - y2 与 x1 - x2 的最大公约数d,斜率可以直接用((y1-y2)/d,(x1-x2)/d)进行唯一的表示。
2。对所有点进行排序,先按x坐标,然后再按y坐标排序。接着对于第 i 个点,我们找到 p (p>=2)个点与其共线,那么总共有p*(p-2)种可能的组合方法。
3.用pair 和 map 将条件 1,2联系起来。map进行计数,pair用来存斜率。
4.好巧妙的一个题。
pair是可以直接根据里面的元素进行判断是否相等的.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<int,pair<int,int> > piii;
int main()
{
pii a(,);
pii b(,);
printf("%d\n",a==b);
piii c(,make_pair(,));
piii d(,make_pair(,));
printf("%d\n",c==d);
return ;
}
/*1 1*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int N = ;
struct Point{
int x,y;
}p[N];
int cmp(Point a,Point b){
if(a.x==b.x) return a.y<b.y;
return a.x<b.y;
}
int gcd(int a,int b){
return b==?a:gcd(b,a%b);
}
map<pair<int,int>,int> mp;
map<pair<int,int>,int>::iterator it;
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
int n;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&p[i].x,&p[i].y);
}
int ans = ;
for(int i=;i<=n;i++){
mp.clear();
for(int j=i+;j<=n;j++){
int x = p[j].x - p[i].x;
int y = p[j].y - p[i].y;
int d = gcd(x,y);
x/=d,y/=d;
mp[make_pair(x,y)]++;
}
for(it = mp.begin();it!=mp.end();it++){
if(it->second>=){
int t = it->second;
ans+=t*(t-)/;
}
}
}
printf("%d\n",ans);
}
return ;
}

最新文章

  1. jQuery 学习之路(4):事件
  2. Graph | Eulerian path
  3. .NET Core Runtime IDentifier (RID) catalog
  4. jquery定义表格宽度
  5. jxse2.6连接外网rdv一直连接不上,而相同的代码用jxse2.7却能连上
  6. java strtus2 DynamicMethodInvocation配置入门 &quot; ! &quot;访问action里面的方法
  7. Top 100 English Verbs
  8. 使用myeclipse新建和删除web项目时一定要小心
  9. Thrift总结(一)介绍
  10. sublime text 3 package Install 安装失败解决方法
  11. 支付-stripe
  12. Python正则的贪婪和非贪婪示例
  13. sentinel 控制台接入
  14. mysql 乱码解决方案
  15. mysql hive sql 进阶
  16. PHP双向队列,双端队列代码
  17. NamedParameterJdbcTemplate常用方法总结
  18. css圆角和阴影兼容问题(ie7,ie8)
  19. 38 一次 redis 连接泄露的原因 以及 ShardedJedisPool
  20. compare `lvs/haproxy/nginx`

热门文章

  1. kali下将Python2.x切换至Python3.x
  2. 04vim的使用
  3. mongodb安装,库操作,集合操作(表),文档操作(记录)
  4. astyle 使用说明 —— 集成到开发平台中
  5. day 16 JS DOM 继续
  6. Django admin模块使用search时报错:django.core.exceptions.FieldError: Related Field got invalid lookup: contains
  7. ASP.NET下调用ffmpeg与mencoder实现视频转换截屏
  8. Windows Server 2012 R2:细节信息汇总
  9. 剖析微软Hyper-V的最佳部署方式
  10. Python框架之Django学习笔记(十六)