View Code of Problem 5

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		for(int i = 0; i < t; i ++) {
			int n = sc.nextInt();
			int m = sc.nextInt();
			List<Fruit> list = new ArrayList<Fruit>();
			for(int j = 1; j <= n; j ++) {
				int apple = sc.nextInt();
				int bear = sc.nextInt();
				Fruit f = new Fruit(apple, bear, j);
				list.add(f);
			}
			Collections.sort(list);
			for(int j = list.size() - 1; j > n - m - 1; j --) {
				//System.out.println(list.get(j));
				Fruit fruit = list.get(j);
				if(j != n - m) {
					System.out.print(fruit.getSort() + " ");
				} else {
					System.out.println(fruit.getSort());
				}
			}
		}
	}

}
class Fruit implements Comparable<Fruit>{
	int apple;
	int bear;
	int sort;
	
	public Fruit() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Fruit(int apple, int bear, int sort) {
		super();
		this.apple = apple;
		this.bear = bear;
		this.sort = sort;
	}
	
	public int getApple() {
		return apple;
	}
	public void setApple(int apple) {
		this.apple = apple;
	}
	public int getBear() {
		return bear;
	}
	public void setBear(int bear) {
		this.bear = bear;
	}
	public int getSort() {
		return sort;
	}
	public void setSort(int sort) {
		this.sort = sort;
	}
	@Override
	public String toString() {
		return "Fruit [apple=" + apple + ", bear=" + bear + ", sort=" + sort + "]";
	}
	@Override
	public int compareTo(Fruit o) {
		// TODO Auto-generated method stub
		if(this.getApple() > o.getApple()) {
			return 1;
		} else if(this.getApple() == o.getApple()) {
			if(this.getBear() > o.getBear()) {
				return 1;
			} else if(this.getBear() == o.getBear()) {
				if(this.getSort() > o.getSort()) {
					return -1;
				} else {
					return 1;
				}
			} else {
				return -1;
			}
		} else {
			return -1;
		}
	}
	
}

Double click to view unformatted code.


Back to problem 5