Guest User

BAT2

a guest
May 11th, 2018
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. import static java.lang.Integer.*;
  6.  
  7. class BAT2 {
  8.     static int a[] = new int[105];
  9.     static int dp[][][] = new int[105][105][105];
  10.     static final int UNDEFINED = -1;
  11.     public static void main(String[] args) throws IOException {
  12.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  13.         int n, t;
  14.         String s[];
  15.         t = parseInt(br.readLine());
  16.         while (t-- > 0) {
  17.             n = parseInt(br.readLine());
  18.             a[n] = MAX_VALUE;
  19.             a[n + 1] = MIN_VALUE;
  20.             fillDp(n + 2);
  21.             s = br.readLine().split("\\s");
  22.             for (int i = 0; i < n; i++) {
  23.                 a[i] = parseInt(s[i]);
  24.             }
  25.             System.out.println(solve(n));
  26.         }
  27.     }
  28.  
  29.     private static int solve(int n) {
  30.         return maxColoredElements(n - 1, n, n + 1);
  31.     }
  32.  
  33.     private static int maxColoredElements(int i, int li, int ld) {
  34.         if (i == -1) {
  35.             return 0;
  36.         }
  37.         if (dp[i][li][ld] != UNDEFINED) {
  38.             return dp[i][li][ld];
  39.         }
  40.         int temp = maxColoredElements(i - 1, li, ld);
  41.         if (a[i] < a[li]) {
  42.             temp = max(temp, 1 + maxColoredElements(i - 1, i, ld));
  43.         }
  44.         if (a[i] > a[ld]) {
  45.             temp = max(temp, 1 + maxColoredElements(i - 1, li, i));
  46.         }
  47.         return dp[i][li][ld] = temp;
  48.     }
  49.  
  50.     private static void fillDp(int n) {
  51.         for (int i = 0; i < n; i++) {
  52.             for (int j = 0; j < n; j++) {
  53.                 for (int k = 0; k < n; k++) {
  54.                     dp[i][j][k] = UNDEFINED;
  55.                 }
  56.             }
  57.         }
  58.     }
  59. }
Add Comment
Please, Sign In to add comment