View Code of Problem 5

#include <stdio.h>
#define maxn 100000

int f[maxn];//a
int h[maxn];//b
int x[maxn];//xuhao


int main()
{
    int t;
    scanf("%d",&t);
    
    while(t>0)
    {
        t--;
        
        int n,m;
        
        scanf("%d%d",&n,&m);
        
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&f[i],&h[i]);//a b
            x[i]=i;
        }

        for (int j=1;j<n;j++)
        {
            for(int k=j+1;k<=n;k++)
            {
                int t;
                if(f[j]<f[k])
                {
                    t=f[j];
                    f[j]=f[k];
                    f[k]=t; //swap
                    
                    t=h[j];
                    h[j]=h[k];
                    h[k]=t;
                    
                    t=x[j];
                    x[j]=x[k];
                    x[k]=t;

                }
                
                if (f[j]==f[k]&&h[j]<h[k])
                {
                    t=f[j];
                    f[j]=f[k];
                    f[k]=t;
                    
                    t=h[j];
                    h[j]=h[k];
                    h[k]=t;
                    
                    t=x[j];
                    x[j]=x[k];
                    x[k]=t;
                    
                }
                
                    
            }
        }
        
        
        
        for(int s=1;s<=m;s++)
        {
            printf("%d ",x[s]);
        }
        printf("\n");
    }
    return 0;
}


Double click to view unformatted code.


Back to problem 5