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

题目描述:

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.

输入描述:

There are multiple cases. The first line of the input contains a single integer T (1≤T≤10000), 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 N N lines each contains two integers xi,yi (|xi,yi|≤1000), denoting the x-coordinate and the y-coordinate of the i-th point. It is guaranteed that the sum of N over all cases does not exceed 2×105.

输出描述:

For each case, print four integers x1,y1,x2,y2 in a line, representing a line passing through (x1,y1) and (x2,y2). Obviously the output must satisfy (x1,y1) ≠ (x2,y2). The absolute value of each coordinate must not exceed 109. It is guaranteed that at least one solution exists. If there are multiple solutions, print any of them.

解:

题目就是求一个直线,把给定的点分成两部分,每部分的点的个数相等,输出的是直线上整点的坐标。一开始以为是一个计算几何的题,后来才发现根本不用那么麻烦,只需要把点都读进来,按照坐标排一下序,找到最中间的两个点,如果这两个点的横坐标相同,那么就一个点的横坐标往左移一下,一个往右移一下,之后纵坐标直接固定一个值,另一个点的纵坐标就利用对称来求;如果横坐标不同,那直接输出就行了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;
 
const double eps = 1e-8;
const int maxn = 1e3+5;
 
int n;
 
struct poi
{
    int x, y;
}p[maxn];
 
bool cmp(poi a, poi b)
{
    if(a.x == b.x)
        return a.y < b.y;
    else
        return a.x < b.x;
}
 
int main ()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d", &n);
        bool f1 = 1, f2 = 1;
        for(int i = 1; i <= n; ++i)
            scanf("%d%d", &p[i].x, &p[i].y);
        sort(p+1, p+n+1, cmp);
        if(p[n/2].x == p[n/2+1].x)
            printf("%d 1000000 %d %d\n", p[n/2].x-1, p[n/2+1].x+1, p[n/2].y+p[n/2+1].y-1000000);
        else
            printf("%d 1000000  %d -1000000\n", p[n/2].x, p[n/2+1].x);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值