AT_abc418_e 题解(常数略大)

· · 题解

题目大意

给定一个平面直角坐标系里的 N 个两两不重合,三点不共线的点,问能构成多少个梯形(包含平行四边形和长方形、正方形)。

思路

我们发现梯形有一组对边平行,然后平行的直线斜率相等,所以考虑把两点间连线的斜率都算出来,然后对于相同的斜率选择两条底边。

什么是斜率?斜率 k=\frac{x_1-x_2}{y_1-y_2},用来表示直线在平面直角坐标系中的倾斜程度。

但是,在这种情况下,我们重复计算了平行四边形。

考虑算出平行四边形的个数然后减去一遍。不过对于每组平行对边,逐一判断是否是平行四边形的时间复杂度太高了,我们要换一种方法。下面是平行四边形的判定方法:

  1. 两组对边均互相平行。
  2. 两组对边分别相等。
  3. 一组对边平行且相等。
  4. 两组对角分别相等。
  5. 对角线互相平分。

我们发现,上面的方法中,第三条是最容易的。我们只需要再维护一个量 l,表示长度即可。长度可以用勾股定理计算,方便起见,我们也可以只算出长度的平方(因为平方相等,长度非负,长度一定相等)。

使用 map 容器维护,常数略大请自行处理。

代码(常数略大)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

int n;

struct dot
{
    int x, y;
} ;

dot p[2010];

struct node
{
    int xa, ya, xb, yb;
    double k;  
    long long len; 
} ;

node l[4000010];

map<double, int> mp1; 
map<pair<double, long long>, int> mp2;

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> p[i].x >> p[i].y;
    int sum = 0;
    for (int i = 1; i <= n; i++)
        for (int j = i + 1; j <= n; j++)
        {
            l[++sum] = (node){p[i].x, p[i].y, p[j].x, p[j].y, 0, 0};
            if (p[i].y == p[j].y) l[sum].k = 1e9;
            else l[sum].k = 1.0 * (p[i].x - p[j].x) / (p[i].y - p[j].y);
            l[sum].len = 1LL * (p[i].x - p[j].x) * (p[i].x - p[j].x);
            l[sum].len += 1LL * (p[i].y - p[j].y) * (p[i].y - p[j].y);
        }
    for (int i = 1; i <= sum; i++)
    {
        mp1[l[i].k]++;
        mp2[make_pair(l[i].k, l[i].len)]++;
    }
    long long ans = 0;
    for (map<double, int>::iterator it = mp1.begin(); it != mp1.end(); it++)
    {
        int v = (*it).second;
        ans += 1LL * v * (v - 1) / 2;
    }
    long long c = 0;
    for (map<pair<double, long long>, int>::iterator it = mp2.begin(); it != mp2.end(); it++)
    {
        int v = (*it).second;
        c += 1LL * v * (v - 1) / 2;
    }
    ans -= c / 2;
    cout << ans << endl;
    return 0;
}

总结

一道数学题,考察平面直角坐标系及平行四边形、梯形方面的知识。难度符合 \color{green}\texttt{475pts}。