2019牛客暑期多校训练营(第三场)H题_Magic Line

在这里插入图片描述
链接:https://ac.nowcoder.com/acm/contest/883/H
来源:牛客网

题目大意:

在众偶数点中,用一根线把他们分开成两部分,点分布在线的两侧。

官方解法:

在这里插入图片描述
思路:

不过还有一种方法进行代码上的优化,跟官解思想差不多,就是不考虑x是否重复。
就是在划直线的时候注意要避免对称,所以两边y加减有略微不同。
(也就是如果加减一样则h[n / 2]在直线上)

//cmp为sort自定义排序提供了排序(规则)方法。
代码实现:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 1000000007;
struct node
{
    int x, y;
};
int cmp(node a, node b)
{
    if (a.x!=b.x)
    {
        return a.x < b.x;//x从小到大排
    }
    else
    {
        return a.y < b.y;//x相等,y从小到大排
    }
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        node h[1005];
        int n;
        cin >> n;
        for (int i = 0; i < n; i++)
        {
            cin >> h[i].x >> h[i].y;
        }
        sort(h, h + n, cmp);
        cout << h[n / 2].x - 1 << ' ' << h[n / 2].y + 900005 << ' ' << h[n / 2].x + 1 << ' ' << h[n / 2].y - 900006 << endl;
        //这里是关键;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/gidear/p/11773632.html