View Code of Problem 5

#include <bits/stdc++.h>
using namespace std;
struct  Lanzi  //结构体篮子,篮子里面有apple pear 序号id
{
	int apple;
	int pear;
	int id;
};
bool cmp(Lanzi x,Lanzi y)
{
	if(x.apple!=y.apple)
        return x.apple>y.apple;//苹果不同,按苹果大的在前面排序
    else if(x.pear!=y.pear)
        return x.pear>y.pear;//苹果相同,梨子不同,按苹果大的在前面排序
    else
        return x.id<y.id;//苹果梨子都相同,按序号小的在前面排序
 
}
int main()
{
	int t, n, m;
	int i;
	Lanzi lz[100000];
	cin >> t;
	while (t--)
	{
		cin >> n >> m;
		for (i = 0; i < n; i++)
		{
			cin >> lz[i].apple >> lz[i].pear;
			lz[i].id = i + 1;
		}
		/*for (i = 0; i < n; i++)
		{
			cout << lz[i].apple << " " << lz[i].pear << " " << lz[i].id << endl;
		}*/
        sort(lz,lz+n,cmp);//sort默认小的在前面排序,cmp可改变排序规则
        if(m==1)
        {
            cout<<lz[0].id<<endl;
            continue;
        }
        else
        {
            for(i=0;i<m-1;i++)
            {
                cout<<lz[i].id<<" ";
            }
            cout<<lz[m-1].id<<endl;
        }
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 5