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 ;
}

hdu 5020(斜率的表示+STL)的更多相关文章

  1. B - Lawrence HDU - 2829 斜率dp dp转移方程不好写

    B - Lawrence HDU - 2829 这个题目我觉得很难,难在这个dp方程不会写. 看了网上的题解,看了很久才理解这个dp转移方程 dp[i][j] 表示前面1~j 位并且以 j 结尾分成了 ...

  2. hdu 5020 求三点共线的组合数(容器记录斜率出现次数)

    题意:       给你n个点,问你3点共线的组合数有多少,就是有多少种组合是满足3点共线的. 思路:      一开始抱着试1试的态度,暴力了一个O(n^3),结果一如既往的超时了,然后又在刚刚超时 ...

  3. hdu 3507 斜率dp

    不好理解,先多做几个再看 此题是很基础的斜率DP的入门题. 题意很清楚,就是输出序列a[n],每连续输出的费用是连续输出的数字和的平方加上常数M 让我们求这个费用的最小值. 设dp[i]表示输出前i个 ...

  4. hdu 5020 求3点共线的组合数

    http://acm.hdu.edu.cn/showproblem.php?pid=5020 求3点共线的组合数 极角排序然后组合数相加 #include <cstdio> #includ ...

  5. hdu 6040 Hints of sd0061(stl: nth_element(arr,arr+k,arr+n))

    Hints of sd0061 Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  6. D - Pearls HDU - 1300 斜率dp+二分

    D - Pearls HDU - 1300 这个题目也是一个比较裸的斜率dp,依照之前可以推一下这个公式,这个很好推 这个注意题目已经按照价格升序排列序,所以还是前缀和还是单调的. sum[i] 表示 ...

  7. HDU - 1022 Train Problem I STL 压栈

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. hdu 3507 斜率优化

    我的第一道斜率优化. 就这道题而言,写出原始的方程: dp[i] = min{ dp[j] + (sum[i]-sum[j])2  + M | j in [0,i) } O(n^2)的复杂度肯定超时, ...

  9. HDU 2646 栈的应用 STL

    Expression Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. 12.1.VUE学习之-循环li,if判断示例讲解class中应用表达式

    功能: 当点击按键时,改变当前循环数组里的status里的值, 判断staus里的当前的值来,切换显示 删除 和 恢复 的按钮 判断staus里的当前的值来改变span标签里的字体颜色样式 <! ...

  2. vue之神奇的动态按钮

    今天我们将利用vue的条件指令来完成一个简易的动态变色功能按钮 首先我们还是要对vue进行配置,在上篇随笔中有相关下载教学. 然后我们要在html页面上搭建三个简易的按钮,颜色分别为紫,绿和蓝(颜色随 ...

  3. pycharm配置Git托管

    利用Pycharm和github管理代码转载https://www.cnblogs.com/feixuelove1009/p/5955332.html git教程--廖雪峰git教程  转载https ...

  4. 时间转换,django的时间设置,re模块简单校验密码和手机号

    时间转换和密码,手机的re模块简单校验 import re,time def check_userinfo(request): pwd = request.POST.get("pwd&quo ...

  5. C++实例 MySTLString

    #include <iostream> #include <cstring> #include <string> using namespace std; clas ...

  6. linux lvm扩容

    1.分区,  查看磁盘使用:fdisk -l 对磁盘分区:fdisk /dev/sdb 2.创建pv pvcreate /dev/sdb1 查看pv: pvdisplay 3.查看vg  vgdisp ...

  7. 算法训练 Eurodiffusion

    Eurodiffusion /***********并未完全AC***********/ #include<iostream> #include<algorithm> #inc ...

  8. ACM模板

    #include <iostream> //万能头文件#include<bits/stdc++.h> 方便时用 #include <algorithm> #incl ...

  9. Linux文件类型 扩展名的作用

    链接类型文件 查找显示管道文件 普通文件类型 file 查看文件的类型 data文件类型 创建块字和符设备 mknod 1,.tar .tar.gz .tgz .zip tar.bz 表示压缩文件,创 ...

  10. mysql-update时where条件无索引锁全表

          1 5.3日数据处理需求 UPDATE md_meter set warranty_end_date = DATE_ADD(warranty_begin_date,INTERVAL 10 ...