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

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

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







当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值