zoukankan      html  css  js  c++  java
  • codeforces 660D D. Number of Parallelograms(计算几何)

    题目链接:

    D. Number of Parallelograms

    time limit per test
    4 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given n points on a plane. All the points are distinct and no three of them lie on the same line. Find the number of parallelograms with the vertices at the given points.

    Input

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

    Each of the next n lines contains two integers (xi, yi) (0 ≤ xi, yi ≤ 109) — the coordinates of the i-th point.

    Output

    Print the only integer c — the number of parallelograms with the vertices at the given points.

    Example
    input
    4
    0 1
    1 0
    1 1
    2 0
    output
    1


    题意:

    给了这么些点,问能形成多少个平行四边形;

    思路:

    把所有的线段找出来,按长度排序,平行四边形对边长度相等且互相平行,然后判断一下就好了,我的由于每个都算了4遍,所以最后/4;
    比赛还没完我就写题解了,说不定还要被hack,好方,哈哈哈哈;

    AC代码:

    /*
    2014300227    660D - 4    GNU C++11    Accepted    468 ms    80348 KB
    */
    #include <bits/stdc++.h>
    using namespace std;
    const int N=1e6+20;
    typedef long long ll;
    const double PI=acos(-1.0);
    int n,cnt=1,vis[4*N];
    ll x[2005],y[2005];
    struct Line
    {
        int fi,se;
        ll le;
    };
    Line line[4*N];
    int cmp(Line a,Line b)
    {
        return a.le<b.le;
    }
    int findpos(ll num)
    {
        int l=1,r=cnt-1,mid;
        while(l<=r)
        {
            mid=(l+r)>>1;
            if(line[mid].le<num)l=mid+1;
            else r=mid-1;
        }
        return l;
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d%I64d",&x[i],&y[i]);
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                line[cnt].fi=i;
                line[cnt].se=j;
                line[cnt++].le=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
            }
        }
    
        sort(line+1,line+cnt,cmp);
        vis[1]=1;
        for(int i=2;i<cnt;i++)
        {
            if(line[i].le==line[i-1].le)
            {
                vis[i]=vis[i-1];
            }
            else vis[i]=i;
        }
        int ans=0;
        for(int i=1;i<cnt;i++)
        {
            int pos=vis[i];
            for(int j=pos;j<cnt;j++)
            {
                if(line[j].le>line[i].le)break;
                if(line[j].fi==line[i].se||line[j].se==line[i].fi||line[j].fi==line[i].fi||line[j].se==line[i].se)continue;
                int cx,cy,dx,dy;
                cx=line[i].fi;
                cy=line[i].se;
                dx=line[j].fi;
                dy=line[j].se;
                if((x[cx]-x[cy])*(y[dx]-y[dy])==(y[cx]-y[cy])*(x[dx]-x[dy]))
                ans++;
            }
        }
        cout<<ans/4<<"
    ";
    
        return 0;
    }
  • 相关阅读:
    springboot 实现 aop
    使用@import导入实现了ImportBeanDefinitionRegistrar接口的类,不能被注册为bean
    Spring Boot 动态数据源(多数据源自动切换)
    springboot2动态数据源的绑定
    三分钟学会@Autowired@Qualifier@Primary注解
    java复制文件的4种方式
    五款最好的免费同步软件
    springboot2多数据源完整示例
    WebSocket 结合 Nginx 实现域名及 WSS 协议访问
    Spring的注解@Qualifier注解
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5370491.html
Copyright © 2011-2022 走看看