hqt

LIS Performance CP3

hqt
Jun 15th, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. package com.classicdp;
  2.  
  3. import java.util.Collections;
  4. import java.util.Stack;
  5. import java.util.Vector;
  6.  
  7. public class LISPerformance {
  8.      static void reconstruct_print(int end, int[] a, int[] p) {
  9.             int x = end;
  10.             Stack<Integer> s = new Stack<Integer>();
  11.            
  12.             for (; p[x] >= 0; x = p[x]) {
  13.                 s.push(a[x]);
  14.             }
  15.            
  16.             System.out.printf("[%d", a[x]);
  17.             for (; !s.isEmpty(); s.pop()) System.out.printf(", %d", s.peek());
  18.             System.out.printf("]\n");
  19.           }
  20.  
  21.           public static void main(String[] args) {
  22.             final int MAX_N = 100000;
  23.  
  24.             int[] A = new int[] {-7, 10, 9, 2, 3, 8, 8, 1, 2, 5, 6, 4};
  25.             int n = A.length;
  26.            
  27.             // id of element of L array
  28.             int[] L_id = new int[MAX_N];
  29.            
  30.             // ?????
  31.             int[] P = new int[MAX_N];
  32.            
  33.             Vector<Integer> L = new Vector<Integer>();
  34.  
  35.             int lis = 0;        // length of L. because we use static array
  36.             int lis_end = 0;
  37.             for (int i = 0; i < n; ++i) {
  38.               int pos = Collections.binarySearch(L, A[i]);
  39.               if (pos < 0) pos = -(pos + 1); // some adjustments are needed
  40.               if (pos >= L.size()) L.add(A[i]);
  41.               else                 L.set(pos, A[i]);
  42.               L_id[pos] = i;
  43.               P[i] = pos > 0 ? L_id[pos - 1] : -1;
  44.               if (pos + 1 > lis) {
  45.                 lis = pos + 1;
  46.                 lis_end = i;
  47.               }
  48.  
  49.               System.out.printf("Considering element A[%d] = %d\n", i, A[i]);
  50.               System.out.printf("LIS ending at A[%d] is of length %d: ", i, pos + 1);
  51.               reconstruct_print(i, A, P);
  52.               System.out.println("L is now: " + L);
  53.               //System.out.print("ID:"); print(L_id, i);
  54.               //System.out.print("P :"); print(P, i);
  55.               System.out.printf("\n");
  56.             }
  57.            
  58.             System.out.println("Assert: " + (L.size() == lis));
  59.  
  60.             System.out.printf("Final LIS is of length %d: ", lis);
  61.             reconstruct_print(lis_end, A, P);
  62.           }
  63.          
  64.           private static void print(int[] K, int n) {
  65.               System.out.print("[");
  66.               for (int i = 0; i < n; i++) {
  67.                   System.out.print(K[i] + ", ");
  68.               }
  69.               System.out.println("]");
  70.           }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment