View Code of Problem 5

#include<bits/stdc++.h>
using namespace std;
struct fruit{
	int id;
	int apple;
	int pear;
}f[100001];
int cmp(fruit a,fruit b){
     if(a.apple==b.apple){
		 if(a.pear==b.pear){
			a.id<b.id;
		}
		else{
			return a.pear>b.pear;
		}
	}
	else{
		return a.apple>b.apple;
	}
}
int main(){
	int t;
	cin>>t;
	while(t--){
		int n,m;
		cin>>n>>m;
		for(int i=1;i<=n;i++){
			cin>>f[i].apple;
			cin>>f[i].pear;
			f[i].id=i;
		}
		sort(f+1,f+n+1,cmp);
		for(int i=1;i<=m;i++){
			if(i!=m){
				cout<<f[i].id<<" ";
			}
			else cout<<f[i].id;
		} 
		cout<<endl;
	}
	
	return 0;
}

Double click to view unformatted code.


Back to problem 5