Codeforces 552D Vanya and Triangles【暴力枚举+思维】

本文介绍了一种高效算法,用于计算平面上N个不同点间形成的非零面积三角形的数量。通过优化思路,避免了传统O(n^3)的时间复杂度,采用O(n^2)的方法来枚举点对并统计可能构成的三角形。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Vanya and Triangles
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Vanya got bored and he painted n distinct points on the plane. After that he connected all the points pairwise and saw that as a result many triangles were formed with vertices in the painted points. He asks you to count the number of the formed triangles with the non-zero area.

Input

The first line contains integer n (1 ≤ n ≤ 2000) — the number of the points painted on the plane.

Next n lines contain two integers each xi, yi ( - 100 ≤ xi, yi ≤ 100) — the coordinates of the i-th point. It is guaranteed that no two given points coincide.

Output

In the first line print an integer — the number of triangles with the non-zero area among the painted points.

Examples
Input
4
0 0
1 1
2 0
2 2
Output
3
Input
3
0 0
1 1
2 0
Output
1
Input
1
1 1
Output
0
Note

Note to the first sample test. There are 3 triangles formed: (0, 0) - (1, 1) - (2, 0); (0, 0) - (2, 2) - (2, 0); (1, 1) - (2, 2) - (2, 0).

Note to the second sample test. There is 1 triangle formed: (0, 0) - (1, 1) - (2, 0).

Note to the third sample test. A single point doesn't form a single triangle.


题目大意:

给你N个点,让你找到面积不为0的三角形的个数。


思路:


显然直接枚举三个点出来判断是否为三角形时间复杂度为O(n^3),即使是CF的评测机,4s也肯定接受不了8e9的操作数量。

那么我们考虑思维优化。

观察到点的范围处于【-100,100】之间,那么我们可以先枚举出来两个点,已知一共有n个点,那么与这两个点处于同一直线上的点去掉,其他点都可以与这两个点构成一个三角形。

那么我们O(n^2)去枚举两个点,很显然,通过两点法:(x-x1)/(x2-x1)=(y-y1)/(y2-y1)能够很容易通过枚举一个x来求得一个y.(而且输入都是整数,所以要求求y的时候 ,一定要能够整除才行);同时,对于这个点(x,y)进行去除。那么不和这两个点处于同一直线的其他点的个数就得以统计了。

ans=sum/3(一个三角形肯定会重复三次被统计).

注意几点:

①枚举出来的两点处于一条直线上。

②枚举出来的x所求的y可能出现数组越界的情况。

③处理好数组大小,以及负数点的表达。


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int a[2200][2];
int vis[300][300];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int mid=150;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            vis[mid+x][mid+y]++;
            a[i][0]=x;a[i][1]=y;
        }
        __int64 output=0;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                int sum=n;
                int x1=a[i][0],y1=a[i][1],x2=a[j][0],y2=a[j][1];
                if(x1==x2||y1==y2)
                {
                    if(x1==x2)
                    {
                        int x=x1;
                        for(int y=-100;y<=100;y++)
                        {
                            sum-=vis[x+mid][y+mid];
                        }
                    }
                    if(y1==y2)
                    {
                        int y=y1;
                        for(int x=-100;x<=100;x++)
                        {
                            sum-=vis[x+mid][y+mid];
                        }
                    }
                }
                else
                {
                    for(int x=-100;x<=100;x++)
                    {
                        int fenzi=(x-x1)*(y2-y1);
                        int fenmu=(x2-x1);
                        if(fenmu==0)continue;
                        if(fenzi%fenmu==0)
                        {
                            int y=fenzi/fenmu+y1;
                            if(y>=-100&&y<=100)
                            sum-=vis[x+mid][y+mid];
                        }
                    }
                }
                output+=sum;
            }
        }
        printf("%I64d\n",output/3);
    }
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值