zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    mouse_event,keybd_event
    构造自己的StringGrid
    BMP2JPG,JPG2BMP
    $J的用法
    一个简单的系统托盘程序
    构造自己的双向链表
    C++的iostream标准库介绍
    D3DXVec4Dot 函数
    3D游戏从头编之D3D(3)
    D3DXVec4Cross 函数
  • 原文地址:https://www.cnblogs.com/gidear/p/11773632.html
Copyright © 2011-2022 走看看