View Code of Problem 5

#include<iostream>
#include<algorithm>
using namespace std;
struct fruit
{
    int apple;
    int pear;
    int num;
};
bool cmp(struct fruit a,struct fruit b)
{
    if(a.apple>b.apple)
        return true;
    else if(a.apple==b.apple)
    {
        if(a.pear>b.pear)
            return true;
        else if(a.pear==b.pear)
        {
            return a.num<b.num;
        }
        else
            return false;
    }
    else
        return false;
}
int main()
{
    int t,n,m;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        struct fruit basket[n];
        for(int i=0; i<n; i++)
        {
            cin>>basket[i].apple>>basket[i].pear;
            basket[i].num=i;
        }

        sort(basket,basket+n,cmp);

        for(int i=0; i<m; i++)
        {
            if(i==0)cout<<basket[i].num+1;
            else cout<<" "<<basket[i].num+1;
        }
        cout<<endl;
    }
    return 0;
}

Double click to view unformatted code.


Back to problem 5