Quick actions

cmd+k|ctrl+k

Navigation

Languages

K Closest Point To Origin

Snippet info

Language

Cpp

Visibility

public

Author

amangoyal1727

Created

2021-01-11T22:28:33Z

Updated

2021-01-11T22:28:33Z

#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;
    }
       
}
INFO