• H-Magic Line_2019 牛客暑期多校训练营(第三场)


    题目连接:

    https://ac.nowcoder.com/acm/contest/883/H

    Description

    There are always some problems that seem simple but is difficult to solve.

    ZYB got N distinct points on a two-dimensional plane. He wants to draw a magic line so that the points will be divided into two parts, and the number of points in each part is the same. There is also a restriction: this line can not pass through any of the points.

    Help him draw this magic line.

    Input

    There are multiple cases. The first line of the input contains a single integer (T(1<=T<=1000)), indicating the number of cases.

    For each case, the first line of the input contains a single even integer (N (2 <= N <= 1000)), the number of points. The following (N) lines each contains two integers xi,yi |(xi,yi)| <= 1000, denoting the x-coordinate and the y-coordinate of the -th point.

    It is guaranteed that the sum of N over all cases does not exceed 2*10^5.

    Output

    For each case, print four integers (x_1, y_1, x_2, y_2) in a line, representing a line passing through ((x_1, y_1)) and$ (x_2, y_2)$. Obviously the output must satisfy .

    The absolute value of each coordinate must not exceed (10^9). It is guaranteed that at least one solution exists. If there are multiple solutions, print any of them.

    Sample Input

    1
    4
    0 1
    -1 0
    1 0
    0 -1

    Sample Output

    -1 999000000 1 -999000001

    Hint

    题意

    二维平面上有n个整数坐标的点,求出一条直线将平面上的点分为数量相等的两部分,且线上不能有点,输出线上两个点确定该直线

    题解:

    先在左下角无穷远处取一质数坐标点(x,y) 对该点和n个点进行极角排序,设排序后中点坐标为(a,b)则这两点连线会将点分为数量相等的两部分,接着取左下角关于中点的对称点(a+a-x, b+b-y),再将该点左移动一格变成(2a-x-1, 2b-y)
    则(x,y) (2a-x-1, 2b-y)两点确定的直线就可以分割点为两部分,且线上不会有点

    代码

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int MAX=100005;
    const int INF=999999;
    typedef long long ll;
    
    int n,top;
    struct Node
    {
           ll x,y;
    }p[MAX],S[MAX];
    ll Cross(Node a,Node b,Node c)
    {
           return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
    }
      
    ll dis(Node a,Node b)
    {
        return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    }
     bool cmp(Node a,Node b)
    {  
       ll flag = Cross(p[1],a,b);
       if(flag != 0) return flag > 0;
       return dis(p[1],a) < dis(p[1],b); 
    }
      
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            int n;
            scanf("%d", &n);
            p[1].x = -400000009; p[1].y = -2e3;
     
            for(int i=1;i<=n;i++)
                scanf("%lld%lld",&p[i+1].x,&p[i+1].y);
            n++;
            sort(p+2, p+1+n, cmp);
             
            int pos = n/2 + 1;
            ll a = p[pos].x - p[1].x + p[pos].x-1;
            ll b = p[pos].y - p[1].y + p[pos].y;
     
            printf("%lld %lld %lld %lld
    ", p[1].x, p[1].y, a, b);
        }
    }
    
  • 相关阅读:
    10. Regular Expression Matching
    9. Palindrome Number
    6. ZigZag Conversion
    5. Longest Palindromic Substring
    4. Median of Two Sorted Arrays
    3. Longest Substring Without Repeating Characters
    2. Add Two Numbers
    链式表的按序号查找
    可持久化线段树——区间更新hdu4348
    主席树——树链上第k大spoj COT
  • 原文地址:https://www.cnblogs.com/bpdwn-cnblogs/p/11247943.html
Copyright © 2020-2023  润新知