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. 如何使用pyinstaller打包32位的exe

    说明:原来安装的python为64位,故安装的pyinstaller和打包后的exe都为64位.而64位的exe文件在32位的win7操作系统下是无法执行的,显示不兼容.网上查询发现,简单(可能不方便 ...

  2. bootmem_free_node

    该函数设置: 1.pgdata节点的成员 2.pgdata->zone的成员 3.初始化zone->free_area 4.初始化zone所包含的所有页对应的页框描述符page结构体 /* ...

  3. HDU - 5017 Ellipsoid(模拟退火)

    题意 给一个三维椭球面,求球面上距离原点最近的点.输出这个距离. 题解 模拟退火. 把\(z = f(x, y)\)函数写出来,这样通过随机抖动\(x\)和\(y\)坐标就能求出\(z\). 代码 / ...

  4. [USACO]奶牛赛跑(逆序对)

    Description 约翰有 N 头奶牛,他为这些奶牛准备了一个周长为 C 的环形跑牛场.所有奶牛从起点同时起跑,奶牛在比赛中总是以匀速前进的,第 i 头牛的速度为 Vi.只要有一头奶牛跑完 L 圈 ...

  5. qt4.8.5 qtwebkit 静态编译 版本

    2013年就编译好了,qtwebkit是最不好编译的了,尤其是静态编译,这儿分享给大家 估计总有人会用得到... 静态库下载地址:http://yunpan.cn/cyyNqrApbVDwq  提取码 ...

  6. System.AccessViolationException”类型的第一次机会异常在 System.Data.dll 中发生 其他信息: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

    管理员cmd中运行  netsh winsock reset

  7. cf979d Kuro and GCD and XOR and SUM

    set做法 正解是trie-- 主要是要学会 \(a\ \mathrm{xor}\ b \leq a+b\) 这种操作 #include <iostream> #include <c ...

  8. Chrome autocomplete="off"无效

    如果不希望输入框自动填充,可以设置 input 或 textarea 标签的属性 autocomplete="off". 但是有时 Chrome 会忽视 autocomplete ...

  9. Halcon18 Linux For Armv7a 下载

    Halcon18 Linux For Armv7a 下载地址:http://www.211xun.com/download_page_16.html HALCON 18 是一套机器视觉图像处理库,由一 ...

  10. Format aborted in 格式化namenode 失败的原因

    [user6@das0 hadoop-0.20.203.0]$ bin/hadoop namenode -format 12/02/20 14:05:17 INFO namenode.NameNode ...