View Code of Problem 5


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc  = new Scanner(System.in);
        int t = sc.nextInt();
        while (t--!=0){
            int n = sc.nextInt();
            int m = sc.nextInt();
            int a[] = new int[n];
            int b[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = sc.nextInt();
                b[i] = sc.nextInt();
            }
            for (int i = 0; i < m; i++) {
                int max = a[0];
                int index = 0;
                for (int j = 1; j < n; j++) {
                    if(a[j] > max){
                        max = a[j];
                        index = j;
                    }
                    else if(a[j]==max){
                        if(b[j] > b[index]){
                            max = a[j];
                            index = j;
                        }
                    }
                }
                if(i!=m-1)
                System.out.print(index + 1 +" ");
                else
                    System.out.println(index + 1);
                a[index] = -1;
            }
        }
    }
}

Double click to view unformatted code.


Back to problem 5