Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 1.58 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. class Main {
  5.         public static void main(String args[]) {
  6.                 // encapsulate System.in for better input parsing
  7.                 Scanner in = new Scanner(System.in);
  8.                 // for each testcase
  9.                 int tCases = in.nextInt();
  10.                 for (int i = 0; i < tCases; i++) {
  11.                         int count = in.nextInt();
  12.                         ArrayList<Integer> numbers = new ArrayList<Integer>();
  13.                         for (int j = 0; j < count; j++) {
  14.                                 numbers.add(in.nextInt());
  15.                         }
  16.                         Table table = new Table(numbers);
  17.                         System.out.println(table.calcMax());
  18.                 }
  19.         }
  20. }
  21.  
  22. class Item {
  23.         public Item(int i, int j) {
  24.                 count = i;
  25.                 value = j;
  26.         }
  27.  
  28.         int count;
  29.         int value;
  30. }
  31.  
  32. class Table {
  33.         int count;
  34.         ArrayList<Integer> numbers;
  35.         ArrayList<Item> maxima;
  36.  
  37.         public Table(ArrayList<Integer> numbers) {
  38.                 count = numbers.size();
  39.                 this.numbers = numbers;
  40.                 dpTable2 = new int[count];
  41.                 maxima = new ArrayList<Item>();
  42.         }
  43.  
  44.         int[] dpTable2;
  45.  
  46.         public int calcMax() {
  47.                 maxima.add(new Item(1,numbers.get(count-1)));
  48.                 for (int i = count - 2; i >= 0; i--) {
  49.                         calc(i);
  50.                 }
  51.  
  52.        
  53.  
  54.                 return maxima.get(0).count;
  55.         }
  56.  
  57.         private void calc(int pos) {
  58.                
  59.                 int max = 0;
  60.                
  61.                 for (int i = 0; i < maxima.size(); i++) {
  62.                         if (maxima.get(i).value > numbers.get(pos)) {
  63.                                 max = maxima.get(i).count;
  64.  
  65.  
  66.                                 for (int j = i; j >= 0; j--) {
  67.                                         if (maxima.get(j).count > max) {
  68.                                                 maxima.add(j, new Item(max + 1,numbers.get(pos)));
  69.                                                 return;
  70.                                         }
  71.                                 }
  72.  
  73.                                 maxima.add(0, new Item(max + 1, numbers.get(pos)));
  74.                                 return;
  75.                         }
  76.                 }
  77.                 maxima.add(new Item(1, numbers.get(pos)));
  78.                 return;
  79.  
  80.         }
  81. }