HDU 5020 Revenge of Collinearity(枚举)

Revenge of Collinearity

Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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

BestCoder Round #10

题目大意:给出n个二维平面的点,问有多少对三点共线。

解题思路:既然是三点共线,不妨设为A,B,C三点,如果我们纯粹暴力枚举A,B,C三点,为了ABC三点之间不相互重复,我们可以先对点排个序,按照x优先非递减,y次之非递减。但是纯粹暴力必然TLE,如果减少枚举的点数,那么就可以减少时间复杂度,假如我们只枚举点A,那么对于其他点中的每一个都会与A点形成一条直线,如果某一条直线出现的不止一次,那么就意味着至少有另外两对与A点在同一直线上,那么就相应的存在三点共线。假设有k个点与A点共线(注意:这里共线是说这k+1个点都在同一直线上)(另外,还要注意这k个点是不重复的,何为不重复呢?举例说明:有(1,0),(2,0),(3,0),(4,0),(5,0)五个点,当A=(1,0)时,k=4;当A=(2,0)时,k=3而不是k=4;当A=(3,0)时,k=2,……)。因为枚举的是A点,其中A点必包括,那么就有k*(k-1)/2对三点共线,枚举完所有的A点,总和就是答案。

对于共线数目k的求解………………………………

代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
typedef pair<ll,ll> p;
map<p,ll> m;
struct point
{
    ll x,y;
}a[1005];
ll gcd(ll a,ll b)
{
    return b?gcd(b,a%b):a;
}
bool cmp(point a,point b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}
int main()
{
    ll i,j,k,n,t;
    scanf("%I64d",&t);
    while(t--)
    {
        scanf("%I64d",&n);
        for(i=0;i<n;i++)
            scanf("%I64d%I64d",&a[i].x,&a[i].y);
        sort(a,a+n,cmp);
        ll ans=0;
        for(i=0;i<n;i++)
        {
            m.clear();
            for(j=i+1;j<n;j++)
            {
                ll dx=a[j].x-a[i].x;
                ll dy=a[j].y-a[i].y;
                ll d=gcd(dx,dy);
                dx=dx/d;
                dy=dy/d;
                m[make_pair(dx,dy)]++;
            }
            map<p,ll>::iterator it;
            for(it=m.begin();it!=m.end();it++)
            {
                k=it->second;
                if(k>=2)
                    ans=ans+k*(k-1)/2;
            }
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-05 20:34:43

HDU 5020 Revenge of Collinearity(枚举)的相关文章

hdu 4430 Yukari&#39;s Birthday 枚举+二分

注意会超long long 开i次根号方法,te=(ll)pow(n,1.0/i); Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3262    Accepted Submission(s): 695 Problem Description Today is Yukari's n-th birt

HDU 4994 Revenge of Nim (博弈)

题目链接:HDU 4994 Revenge of Nim 题意:两个取石头,分别在N堆里去.只有第一堆取完才能取第二堆,以此类推,最后一个取完的为赢家. 思路:从头开始扫,直到第一个不为1为止,判断现在的主动权在谁手里,谁就是赢家.(这里读者可以自己写几组数据试试.) AC代码: #include<stdio.h> #include<string.h> int main() { int yaoga; int t,i,n; int a[1010]; while(scanf("

hdu 5087 Revenge of LIS II(LIS)

题目连接:hdu 5087 Revenge of LIS II 题目大意:给定一个序列,求第2长的LIS长度. 解题思路:用o(n^2)的算法求LIS,每个位置维护两个值,最大和最小即可.注意的是dp[0]中的最大第二大不能都复制成0. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1005; int N, A[maxn],

hdu 5088 Revenge of Nim II(高斯消元)

题目链接:hdu 5088 Revenge of Nim II 题目大意:Nim游戏的变形,因为游戏很不公平,所以现在转变规则,后手可以选取若干堆石子剔除,剩下堆的石子用 来进行游戏,问说后手可能胜利吗. 解题思路:其实即为取出非0堆石子,使得Nim和为0.因为是Nim和(亦或),所以以每个位建立方程,列出40个方 程,进行亦或形式的高斯消元,因为全0肯定为一解,所以方程肯定有解,那么存在多解的情况即为存在自有变元. #include <cstdio> #include <cstring

hdu 2489 Minimal Ratio Tree 枚举+最小生成树

点的总数很小,直接枚举就好. #include <stdio.h> #include <string.h> #define N 20 #define inf 1000000 int mk[N],n,k,ans[N]; double low[N],val[N]; double map[N][N],MIN; double prim() { int i,j; double sum=0; double tot=0; for(i=1;i<=n;i++) low[i]=inf; int

hdu 4309 Seikimatsu Occult Tonneru 枚举+最大流

http://blog.csdn.net/julyana_lin/article/details/8070949 题意: n个点,每个点有初始的值 ,三种 通道,1.隧道:可以用来躲避,有固定的容量,也可以用来传递.2.普通的道路,可以无限的通过.3.桥(最多有12座):不花费的话能通过一人,修之后可以无限通过.问最少花费最大可以隐藏人数. 解: 网络流 + 枚举 官方题解: 先不考虑可以修复的桥的性质,则可以将模型简化为n个点的人通过有通过人数上限的有向边,到达一些有人数上限的特殊的边(隧道)

HDU 4996 Revenge of LIS(DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4996 题意:求1到n的全排列中,有多少个排列的最长上升子列长度为K? 思路:对于当前的最长上升子列,我们记录最后一个值得最小值即可.因此我们用2^n的状态表示当前最长上升子列中使用了哪些数字,且字典序最小.前n-1个数字之后,我们枚举最后一个位置的数字为[1,n]中每个数字,设为k,那么我们只要将前面[1,n-1]组成的数列中所有大于等于k的数字加一即可. int n,k; i64 f[22][22

HDU - 4994 Revenge of Nim

Problem Description Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from

HDU 4334 Trouble(hash + 枚举)

HDU 4334 题意: 给五个数的集合,问能否从每个集合中取一个数,使五个数之和为0. 思路: 集合大小是200,直接枚举的复杂度是200^5,一定会超时. 直接枚举的上限是3层,我们可以将枚举剩下两个集合各任取一个元素可能组成的元素和,并将其作hash处理,使我们能很快判断枚举出来的三个集合元素和在剩下的两个集合里是否有相应元素匹配. code: /* * @author Novicer * language : C++/C */ #include<iostream> #include&l