按照半徑排序,然後累加人數直到超過百萬
#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\n",sqrt(iter->first)); }
本題採用c++的map比較方便,map自動按照半徑排序,但在將半徑作為鍵時,先不要開方,不然存入map中會損失精度