找礼物(find)

 找礼物(find)

题目描述

新 年到了,你的好友和你(共K个人)的周围满是礼物,你让你的好友先拿,但是每个人只能拿当前离自己最近的礼物[当然如果有并列的多个礼物离你的距离相等 (精确到小数点后四位,所有运算均为去尾),这些礼物就都属于这个人]。现在你们所在的位置是原点(0,0),每个礼物的位置用坐标表示。现在告诉你每个 礼物的坐标,还有每个礼物是谁送的。要你找出你的礼物离你多远,你能拿到多少礼物,这些礼物是谁送的。如果你拿不到礼物,请输出“555…”。

输入

第1行:N和K分别表示礼物的个数和人数(K≤N≤100000);
第2到N+1行:每行先是赠送礼品人的姓名,然后是礼物的坐标(x,y)(坐标绝对值小于106)。数据间用空格分开。

输出

第1行:D和U表示礼物距你多远(只要去尾后的整数)和你能拿到多少礼物。
第2到U+1行:每行一个人名,表示送礼的人(按照输入的顺序输出)。

样例输入

5 2
Jason 1 1
Herry 4 4
Patty 3 4
Tom 2 10
Petter 5 10

样例输出

5 1
Patty

提示

long long大法好

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;

typedef long long int ll;
struct point {
    string name;
    ll d;
    int num;
};

bool cmp(const point a, const point b) {
    if(a.d == b.d) {
        return a.num < b.num;
    }
    return a.d < b.d;
}

int main() {
    ios::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    point s[100002];
    ll x, y;
    for(int i = 0; i < n; i++) {
        cin >> s[i].name>> x >> y;
        s[i].num = i;
        s[i].d = ll(sqrt(x*x + y*y + 0.0)*10000);
    }
    sort(s, s+n, cmp);
    queue<int>que;
    int cnt = 1;
    ll lem  = s[0].d;
    if(k == 1){
        que.push(0);
    }
    for(int i = 1; i < n; i++) {
        if(s[i].d != lem) {
            cnt++;
            lem = s[i].d;
        }
        if(cnt == k) {
            que.push(i);
        }
    }
    if(que.size() > 0) {
        int x = que.front();
        cout<< s[x].d/10000 <<" "<<que.size() << endl;
         while(!que.empty()){
            int x = que.front();
            cout << s[x].name << endl;
            que.pop();
        }
    }else {
       cout<<"555..."<<endl;
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/cshg/p/5722646.html