Codeforces Round #242 (Div. 2) B. Megacity

按照半径排序,然后累加人数直到超过百万

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

int main(){
    int n,s;
    cin >> n >>s;
    map<double,int> a;
    for(int i = 0 ; i < n ; ++ i){
        double x,y;
        int people;
        cin>> x >>y>>people;
        double r = x*x+y*y;
        if(a.find(r)!=a.end()) a[r]+=people;
        else a.insert(make_pair(r,people));
    }
    map<double,int>::iterator iter = a.begin();
    for(; iter!=a.end(); iter++){
        s+=iter->second;
        if(s >= 1000000) break;
    }
    if(iter==a.end())cout<<-1<<endl;
    else printf("%0.6f
",sqrt(iter->first));
}

本题采用c++的map比较方便,map自动按照半径排序,但在将半径作为键时,先不要开方,不然存入map中会损失精度

原文地址:https://www.cnblogs.com/xiongqiangcs/p/3689348.html