JZOJ 3493. 【NOIP2013模拟联考13】三角形

Description

平面上有n个点,求出用这些点可以构成的三角形数。
 

Input

第一行一个整数n。

接下来n行,每行两个整数,表示点的坐标。

Output

输出仅一个整数,表示所求答案。
 

Sample Input

5
0 0
1 1
1 -1
-1 -1
-1 1

Sample Output

8
 

Data Constraint

对于50%的数据,n<=300。

对于100%的数据,n<=3000,坐标的绝对值不超过10^4,保证没有重合的点。
 
做法:确定第一个点,枚举直线,判断不能形成三角形的情况,斜率不存在时要特判。
 
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 3007
using namespace std;
int n, h[N], z[N], tot, cnt, c[N * 2];
long long ans;
bool b[N];
double K[N];

int main()
{ 
//    freopen("triangle.in", "r", stdin);
//    freopen("triangle.out", "w", stdout);
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d%d", &h[i], &z[i]);
    ans = (n * (n - 1)) / 2;
    ans *= n - 2;
    ans /= 3;
    for (int i = 1; i <= n - 1; i++)
    {
        cnt = 0;
        tot = 0;
        for (int j = i + 1; j <= n; j++)
            if (h[i] == h[j])    tot++;
            else    K[++cnt] = (double)(z[i] - z[j]) / (double)(h[i] - h[j]);
        sort(K + 1, K + cnt + 1);
        ans -= (tot * (tot - 1)) / 2;
        tot = 1;
        for (int j = 2; j <= cnt; j++)
        {
            if (K[j] == K[j - 1]) tot++;
            else tot = 1;
            ans -= tot - 1;
        }
    }
    printf("%lld", ans);
    fclose(stdin);
    fclose(stdout);
}
View Code

猜你喜欢

转载自www.cnblogs.com/traveller-ly/p/9434567.html