import java.util.LinkedList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String nextLine = scanner.nextLine(); String[] split = nextLine.split(" "); LinkedList<int[]> list = new LinkedList<>(); int length = Integer.valueOf(split[0]) + Integer.valueOf(split[1]); for (int i = 0; i < length; i++) { String[] get = scanner.nextLine().split(" "); int[] g = new int[2]; g[0] = Integer.parseInt(get[0]); g[1] = Integer.parseInt(get[1]); if (list.isEmpty()) { list.add(g); } else { int index = 0; while (index < list.size() && g[0] > list.get(index)[0]) { index++; } list.add(index, g); } } for (int i = 0; i < list.size(); i++) { if(i == list.size() - 1){ System.out.print("" + list.get(i)[0] + " " + list.get(i)[1]); }else{ System.out.println("" + list.get(i)[0] + " " + list.get(i)[1]); } } } } |
Double click to view unformatted code.