Magic Line

题目链接(传送门) 来源:牛客网

题目描述

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

ZYB got  N\ N 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)T \ (1 \leq T \leq 10000)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)N \ (2 \leq N \leq 1000)N (2≤N≤1000), the number of points. The following $N$ lines each contains two integers xi,yi (∣xi,yi∣≤1000)x_i, y_i  \ (|x_i, y_i| \leq 1000)xi​,yi​ (∣xi​,yi​∣≤1000), denoting the x-coordinate and the y-coordinate of the  i\ i i-th point.

It is guaranteed that the sum of  N\ N N over all cases does not exceed 2×1052 \times 10^52×105.

输出描述:

For each case, print four integers x1,y1,x2,y2x_1, y_1, x_2, y_2x1​,y1​,x2​,y2​ in a line, representing a line passing through (x1,y1)(x_1, y_1)(x1​,y1​) and (x2,y2)(x_2, y_2)(x2​,y2​). Obviously the output must satisfy (x1,y1)≠(x2,y2)(x_1,y_1) \ne (x_2,y_2)(x1​,y1​)​=(x2​,y2​).

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

输入

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

输出

-1 999000000 1 -999000001

题目描述:

给出很多坐标点,要求找出一条线使其二等分所有点(线上不能有输入的坐标点),输出线上任意两点坐标。

思路:

a.将所有点排序,找到 n/2 的p点的坐标,再按下图操作。

b.当找到p(x,y)后,由于点的坐标范围是[-1000~1000],所以先找到点A(x+1,1e7)和点B(x-1,y1)可以根据斜率求出         点B坐标,这样可以满足p点在线上且线上有且仅有一点p。

c.过点AB的这条线是恰好过p点(所有点的中点),所以B的纵坐标-1则恰好不过这个点,也就满足了线上没有点的要求。

思路很简单,但点排序的时候遇到问题:

1、如果按照蓝线方向排序:

bool cmp(node a,node b)
{
if(a.x==b.x){
return a.y>b.y;
}return a.x<b.x;
}

2、按照与蓝线垂直方向排序:

bool cmp(node a,node b)
{
if(a.x==b.x){
return a.y<b.y;
}return a.x<b.x;
}

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAX=2e5;
struct node{
LL x;
LL y;
}point[MAX+5];
bool cmp(node a,node b)
{
if(a.x==b.x){
return a.y>b.y;
}return a.x<b.x;
}
int main()
{
LL t,n;
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
for(LL i=0;i<n;i++){
scanf("%lld%lld",&point[i].x,&point[i].y);
}
sort(point,point+n,cmp);
LL ans=n/2-1;
printf("%lld 10000000 %lld %lld\n",point[ans].x+1,point[ans].x-1,2*point[ans].y-10000000-1);
}
return 0;
}

Magic Line【坐标点排序方法】的更多相关文章

  1. javascript总结24:Array常用的队列操作和排序方法

    1 数组-引用类型 JavaScript中的内置对象 复习数组的使用 两种创建数组的方式 Array对象的属性 length 获取数组的长度(元素个数) 2 常用方法 : 检测数组 instanceo ...

  2. Magic Line

    Magic Line 玄学过题系列,随机选在所有点左下方的点,然后对其他点斜率排序,取斜率在中间两个点之间 比赛时,左下方点不够随机==,导致没卡过去 #include<bits/stdc++. ...

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

    原题链接:H  Magic Line 题意简述: 给定n个点,要求画一条直线将n个点分成均有n / 2个点的两部分,不能有点在线上: 解题思路: 首先,先将所有的点进行以x为第一关键字,y为第二关键字 ...

  4. 2019牛客多校第三场H Magic Line 思维

    Magic Line 题意 给出n(偶)个整点 整点范围1000,找出一条直线,把n个点分成均等的两部分 分析 因为都是整数,并且范围比较小,所以直接按x排序找到在中间那一部分,并且把中间那一部分的点 ...

  5. JavaScript高级程序设计--对象,数组(栈方法,队列方法,重排序方法,迭代方法)

    1.使用对象字面量定义对象 var person={}; 使用这种方式创建对象时,实际上不会调用Object构造函数. 开发人员更喜欢对象字面量的语法.   2.有时候需要传递大量可选参数的情形时,一 ...

  6. php语言实现的7种基本的排序方法

    今天总结了一下常用的7种排序方法,并用php语言实现. 直接插入排序 /* * 直接插入排序,插入排序的思想是:当前插入位置之前的元素有序, * 若插入当前位置的元素比有序元素最后一个元素大,则什么也 ...

  7. C语言中常见的排序方法

    在C语言中,常见的排序方法有冒泡法,排序法,插入法等等.所谓的冒泡法,就是对一组数字进行从大到小或者从小到大的一种排序方法.主要就是相邻的数值相互交换.从第一个数值开始,如果这相邻的两个数值排序与我们 ...

  8. Atitit.现实生活中最好使用的排序方法-----ati排序法总结

    Atitit.现实生活中最好使用的排序方法-----ati排序法总结 1. 现在的问题 1 2. 排序的类别::插入排序//交换排序//选择排序(每次最小/大排在相应的位置  )//归并排序//基数排 ...

  9. 转:Java实现几种常见排序方法

    日常操作中常见的排序方法有:冒泡排序.快速排序.选择排序.插入排序.希尔排序,甚至还有基数排序.鸡尾酒排序.桶排序.鸽巢排序.归并排序等. 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一 ...

随机推荐

  1. 实验三 Java基本程序设计(2)

                                             实验三 Java基本程序设计(2)                                           ...

  2. POJ1984

    题目链接:https://vjudge.net/problem/POJ-1984 解题思路:并查集+离线操作. 用dx[ ]和dy[ ]两个数组存储某点相对于该点所在集合的源头的方位,因此不难推知dx ...

  3. .Net基础之5——复杂数据类型

    (1)复习 1.变量 int  double   string   char   bool    decimal 变量的使用规则:先声明再赋值最后使用 2.Camo      Pascal 3.运算符 ...

  4. 汉字统计(hdu2030)

    输入格式:一个整型,再循环带有空格的字符串 思考:用scanf_s()函数输入整型,然后一个大循环,再用gets_s()函数输入带空格的字符串. 注意:scanf_s()函数多加了%c,&d, ...

  5. 初识Mysql 外键

    1.创建学生表(主表) CREATE TABLE `stu` ( `stunum` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT N ...

  6. json和数组

    接触数组: 1.数组的定义方法    var arr = [1,2,3,4,5];    var arr = new array();此处括号内可以填写数组的元素,或者直接填写元素的个数.2.数组中各 ...

  7. android自动化

    1.环境安装 JDK 1.8 Appium Android_SDK python https://www.cnblogs.com/xiaohanzi/p/10676720.html https://b ...

  8. 面向对象案例 - 学生信息管理系统V1.0

    学生管理系统项目[所有知识点整合] 1. 学生管理系统项目 尝试完成以下功能 实体类: 学生类: id, 姓名,年龄,性别,成绩 需要使用数组保存学生信息 Student[] allStu 需要完成的 ...

  9. Java基础语法--分支结构

    if-else 结构 if(条件表达式){ 执行代码块; } if(条件表达式){ 执行代码块; }else { 执行代码块; } if(条件表达式){ 执行代码块; }else if (条件表达式) ...

  10. 跨域解决方案 - 跨域资源共享cors

    目录 1. cors 介绍 2. 原理 3. cors 解决跨域 4. 自定义HTTP 头部字段解决跨域 5. 代码演示 5. 参考链接 1. cors 介绍 cors 说的是一个机制,其实相当于一个 ...