package chelper; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; import io.InputReader; import io.OutputWriter; import misc.SimpleSavingChelperSolution; public class B extends SimpleSavingChelperSolution { public void solve(int testNumber, InputReader in, OutputWriter out) { wrapSolve(testNumber, in, out); } @Override public void solve(int testNumber) { int n = in.nextInt(); // int[] a = in.nextIntArray(n); List a = new ArrayList<>(); for (int i = 0; i < n; i++) { int t = in.nextInt(); a.add(new int[]{t, i}); } a.sort((o1, o2) -> Integer.compare(o1[0], o2[0])); Collections.reverse(a); int max = -1; int c = 0; TreeMap map = new TreeMap<>(); int[] b = new int[n]; List> lists = new ArrayList<>(); for (int[] i : a) { int element = i[0]; int index = i[1]; Integer next = map.ceilingKey(index); if (next == null) { map.put(index, c); lists.add(new ArrayList<>()); lists.get(c).add(element); c++; continue; } int t = map.get(next); lists.get(t).add(element); map.put(index, t); map.remove(next); } for (List list : lists) { Collections.sort(list); for (Integer integer : list) { out.print(integer + " "); } out.println(); } } }