View Code of Problem 5

import java.util.Scanner;

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

}

Double click to view unformatted code.


Back to problem 5