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. BATJ解决千万级别数据之MySQL 的 SQL 优化大总结

    引用 在数据库运维过程中,优化 SQL 是 DBA 团队的日常任务.例行 SQL 优化,不仅可以提高程序性能,还能减低线上故障的概率. 目前常用的 SQL 优化方式包括但不限于:业务层优化.SQL 逻 ...

  2. Poj2109 (2) k^n = p.

    二分法,由p最大值最小值的中间值开始猜k,通过比较pow(k,n)与p的大小来进一步精确k,直到找到. #include<stdio.h> #include<math.h> # ...

  3. SpringBoot2.x快速入门指南(一)

    SpringBoot2.x快速入门指南(一) 准备工作 IDE: IntelliJ IDEA 2020.3 Java环境 jdk1.8 在官网快速创建SpringBoot项目 下面开始进入正题: 进入 ...

  4. Git-Jenkins-代码的上线

    第一章:自动化上线代码基本介绍 1.软件开发生命周期 老板的创意---产品经理---立项---开发团队---测试团队---运维上线 产品经理---加需求---开发团队---测试----更新代码,上线 ...

  5. 解决docker创建的elasticsearch-head容器不能连接elasticsearch等问题

    在使用docker创建elasticsearch-head容器去连接elasticsearch的时候,容易出两个问题 1.不能连接elasticsearch 修改elasticsearch.yml文件 ...

  6. 大O符号初学者指南

    原文地址:https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/ 计算机科学中,大O表示法被用来描述一个算法的性能或复杂度. ...

  7. springboot 启动报错"No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available"

    1.问题 springboot启动报错 "D:\Program Files\Java\jdk-11\bin\java.exe" -XX:TieredStopAtLevel=1 -n ...

  8. 线程的同步机制:同步代码块&同步方法

    解决存在的线程安全问题:打印车票时出现重票,错票 使用同步代码块的解决方案 TestWindow2 package com.aff.thread; /* 使用实现Runnable接口的方式,售票 存在 ...

  9. 是时候扔掉cmder, 换上Windows Terminal

    作为一个Windows的长期用户,一直没有给款好用的终端,知道遇到了 cmder,它拯救一个习惯用Windows敲shell命令的人. 不用跟我安利macOS真香!公司上班一直用macOS,一方面确实 ...

  10. 前端基础知识之html和css全解

    前端回顾 目录 前端回顾 基础知识 HTTP协议 认识HTML HTML组成 HTML标签 div和span标签 特殊的属性 常用标签 认识css 选择器 属性 前端就是展示给用户并且与用户进行交互的 ...