K points to origin

Given a bounch of points, ask to find K closest point to origin.

This question can be changed. For example, here is origin, but there might be another point.

Here is just a basic algorithm.

//  main.cpp

//  k points to origin

//  Created by Xiaohe Huang on 10/19/15.

//  Copyright © 2015 Xiaohe Huang. All rights reserved.

//

#include <iostream>

#include<algorithm>

#include<queue>

using namespace std;

struct Cpoint

{

double x;

double y;

};

struct cmp

{

public:

bool operator()(Cpoint n1,Cpoint n2)

{

return (n1.x)*(n1.x)+(n1.y)*(n1.y)<(n2.x)*(n2.x)+(n2.y)*(n2.y);

}

};

int main(int argc, const char * argv[]) {

// insert code here...

Cpoint points[5];

points[0]={9,9};

points[1]={8,8};

points[2]={7,7};

points[3]={6,6};

points[4]={5,5};

priority_queue<Cpoint,vector<Cpoint>,cmp> pq;

for(int i;i<5;i++)

{

if(i<3)

pq.push(points[i]);

else

{

if((points[i].x)*(points[i].x)+(points[i].y)+(points[i].y)<(pq.top().x+pq.top().x)+(pq.top().y+pq.top().y))

{

pq.pop();

pq.push(points[i]);

}

}

}

return 0;

}

时间: 2025-01-01 21:32:04

K points to origin的相关文章

[Swift Weekly Contest 118]LeetCode973. 最接近原点的 K 个点 | K Closest Points to Origin

We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The answer is guaranteed to be unique (exce

[Solution] 973. K Closest Points to Origin

Difficulty: Easy Problem We have a list of points on the plane. Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order. The answer is guaran

LeetCode 973. K Closest Points to Origin

原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return

973. K Closest Points to Origin - Medium

We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The answer is guaranteed to be unique (exce

LC 973. K Closest Points to Origin

We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The answer is guaranteed to be unique (exce

FB面经 Prepare: K closest point to the origin

Give n points on 2-D plane, find the K closest points to origin 1 package fbPractise; 2 3 import java.util.*; 4 5 class Coordinate { 6 int x; 7 int y; 8 public Coordinate(int x, int y) { 9 this.x = x; 10 this.y = y; 11 } 12 } 13 14 public class Kclos

K Closest Points Lintcode

Given some points and a point origin in two dimensional space, find k points out of the some points which are nearest to origin.Return these points sorted by distance, if they are same with distance, sorted by x-axis, otherwise sorted by y-axis. Exam

K个最近的点

前段时间在网上看到一些准备找工作的人会在LintCode上刷题,然后我今天上去看了一下,也打算开始做题,然后把每天做的题目和以后的优化记录下来. 2017年8月6日 21:17:27 第一题: 描述:给定一些 points 和一个 origin,从 points 中找到 k 个离 origin 最近的点.按照距离由小到大返回.如果两个点有相同距离,则按照x值来排序:若x值也相同,就再按照y值排序. 样例:给出 points = [[4,6],[4,7],[4,4],[2,5],[1,1]], o

Programming Assignment 3: Collinear Points

The problem. Given a set of N distinct points in the plane, draw every (maximal) line segment that connects a subset of 4 or more of the points. Point data type. Create an immutable data type Point that represents a point in the plane by implementing