K Closest Point To Origin

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include <queue> using namespace std; typedef pair<int , pair<int , int >> ppi; int main() { int k; cin>>k; int m; cin>>m; int n; cin>>n; int A[m][n]; for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ cin>>A[i][j]; } } priority_queue<pair<int,pair<int,int>>> maxh; for(int i=0; i<m; i++){ maxh.push({A[i][0]*A[i][0] + A[i][1]*A[i][1] ,{ A[i][0] , A[i][1] } }); if(maxh.size()>k){ maxh.pop(); } } while(k>0){ pair<int,int> p = maxh.top().second; cout<<p.first<<","<<p.second; maxh.pop(); k--; cout<<endl; } }
Editor Settings
Theme
Key bindings
Full width
Lines