View Code of Problem 5

#include<bits/stdc++.h>
using namespace std;
struct node{
	int no;
	int apple;
	int pear;
};
//排序:先选苹果多的苹果数量相同选梨多的 两者相同选序号小的
bool cmp(node a, node b) {
	if(a.apple!=b.apple) return a.apple>b.apple;
	if(a.pear!=b.pear) return a.pear>b.pear;
	return a.no<b.no; 
}
int main() {
	int t,n,m;
	cin>>t;
	while(t--) {
		cin>>n>>m;
		struct node ns[n];
		for(int i=0; i<n; i++) {
			ns[i].no=i+1;
			cin>>ns[i].apple>>ns[i].pear;
		} 
		sort(ns,ns+n,cmp);
		for(int i=0; i<m; i++) cout<<ns[i].no<<" ";
		cout<<endl;
	}
}

Double click to view unformatted code.


Back to problem 5