View Code of Problem 5

#include <iostream>
#include<algorithm>
using namespace std;
struct node
{

    int dight,a,b;
} s[100001],t;

int main()
{
    int w,n,m,i,j;
    cin>>w;
    while(w--)
    {

        cin>>n>>m;
        for(i=1; i<=n; i++)
        {
            cin>>s[i].a>>s[i].b;
              s[i].dight=i;
        }
        for(i=1;i<=n-1;i++)
            for(j=1;j<=n-i;j++)
        {

            if(s[j].a>s[j+1].a)
            {

                t=s[j];s[j]=s[j+1];s[j+1]=t;
            }
        }
          for(i=1;i<=n-1;i++)
            for(j=1;j<=n-i;j++)
            {

                if((s[j].a==s[j+1].a)&&(s[j].b>s[j+1].b))
                {
                    t=s[j];s[j]=s[j+1];s[j+1]=t;
                }
            }
              for(i=1;i<=n-1;i++)
            for(j=1;j<=n-i;j++)
            {
                 if((s[j].a==s[j+1].a)&&(s[j].b==s[j+1].b)&&(s[j].dight<s[j+1].dight))
                   {
                         t=s[j];s[j]=s[j+1];s[j+1]=t;

                   }
            }

            for(i=n;i>=n-m+1;i--)
            {
              if(i==n) cout<<s[i].dight;
              else cout<<" "<<s[i].dight;
            }
            cout<<endl;


    }
    return 0;
}

Double click to view unformatted code.


Back to problem 5