Codeforces Round #445 D. Symmetric Projections

D. Symmetric Projections
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a set of n points on the plane. A line containing the origin is called good, if projection of the given set to this line forms a symmetric multiset of points. Find the total number of good lines.

Multiset is a set where equal elements are allowed.

Multiset is called symmetric, if there is a point P on the plane such that the multiset is centrally symmetric in respect of point P.

Input
The first line contains a single integer n (1 ≤ n ≤ 2000) — the number of points in the set.

Each of the next n lines contains two integers xi and yi ( - 106  ≤  xi,  yi  ≤  106) — the coordinates of the points. It is guaranteed that no two points coincide.

Output
If there are infinitely many good lines, print -1.

Otherwise, print single integer — the number of good lines.

Examples
input
3
1 2
2 1
3 3
output
3
input
2
4 3
1 2
output
-1

题意:给出n个点,过原点做一天直线,如果这个点在这条直线的映射是中心对称的,那么就成这条线是好的,如果好的线是无限的,那么输出-1,否则输出好线的数量。
每一好线都会经过这n个点的重心,{sum(x[i])/n,sum(y[i])/n);证明:因为这n个点的相互对称的点肯定在好的线上,所以这n个点重心必定在好的线上。然后只需要枚举第一个点跟后面的点对称,然后判断时候所有的点都有对称的点就好了,有一个很巧妙的判断方法,对于每一个点,定义一个虚点,虚点就是这个点关于重心中心对称的点,虚点和实点重合的时候就拿掉,因为两个关于重心中心对称的点默认对称就好了。枚举第一个实点与其他的点对称,如果第一个实点的映射关于某个点对称,那么这个直线必定会在通过第一个实点的时候通过第二个点的虚点,所以,只需要枚举第一个实点和所有的虚点连线就可得到所有的斜率。
然后对于每个斜率判断所有的点是否中心对称,判断是这样的:因为斜率确定,那么可以通过保存这条直线与y轴的交点来得知每一条直这里有一个好有的方法,就是先记录经过每个实点的的直线的交点,然后记录经过每个虚点的直线的交点,如果两个集合一样那么就是可行的,否则就是不可行的,因为如果每个实点都有它对称的实点,那么这实点与另一个点对应的虚点得到的与y轴交点是一样的,否则就说明又不对称的、
还有就是关于斜率的保存,我最开始把分母和分子用set,存起来,结果发现对于两个数有一个是0的时候回出现重复,所以如果用gcd的话要特判0.不过有一个更简单的方法,就是建set加一个cmp。

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair <ll, ll> pii;

const int MAXN = 2005;

int N;
int x[MAXN], y[MAXN];
map <pii, int> cnt;
vector <pii> a, b;

struct cmp {
    bool operator()(const pii &lhs, const pii &rhs) const {
        return lhs.first * rhs.second < rhs.first * lhs.second;
    }
};

set <pii, cmp> good;

void load() {
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
        scanf("%d%d", x + i, y + i);
}

vector <ll> get(vector <pii> v, ll rx, ll ry) {
    vector <ll> res;
    for (auto it : v)
        res.push_back(it.first * rx + it.second * ry);
    sort(res.begin(), res.end());
    return res;
}

void check(ll rx, ll ry) {
    if (get(a, rx, ry) == get(b, rx, ry)) {
        if (rx < 0) {
            rx = -rx;
            ry = -ry;
        }
        good.insert({rx, ry});
    }
}

int solve() {
    int sx = 0, sy = 0;
    for (int i = 0; i < N; i++) {
        sx += x[i];
        sy += y[i];
        x[i] *= N;
        y[i] *= N;
    }

    for (int i = 0; i < N; i++) {
        cnt[{x[i], y[i]}]++;
        cnt[{2ll * sx - x[i], 2ll * sy - y[i]}]--;
    }

    for (auto it : cnt)
        for (int i = 0; i < abs(it.second); i++)
            if (it.second > 0)
                a.push_back(it.first);
            else
                b.push_back(it.first);

    assert(a.size() == b.size());

    if (a.empty())
        return -1;

    for (auto it : b)
        check(it.second - a[0].second, a[0].first - it.first);

    return good.size();
}

int main() {
    load();
    printf("%d\n", solve());
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值