View Code of Problem 5

#include <iostream>
#include <algorithm>
using namespace std;
typedef struct{
	int index=0;
	int a=0;
	int b=0; 
}fruit; 
bool cmp(fruit f1,fruit f2){
	if(f1.a==f2.a){
		if(f1.b!=f2.b){
			return f1.b>f2.b;
		}
		else{
			return f1.index<f2.index;
		}
	}
	else
		return f1.a>f2.a;
		
}
int main() {
	int N;
	cin>>N;
	for(int in=0;in<N;in++)
	{
		int n,m;
		cin>>n>>m;
		fruit f[n];
		for(int i=0;i<n;i++)
		{
			f[i].index=i;
			cin>>f[i].a>>f[i].b;
		}
		sort(f,f+n,cmp);
	
		for(int i=0;i<m;++i){
			if(i!=0){
				cout<<" "<<f[i].index+1;
			}
			else{
				cout<<f[i].index+1;
			}
		}
		if(in!=N-1)
		cout<<endl;

	}
	
}

Double click to view unformatted code.


Back to problem 5