Advertisement
Guest User

Untitled

a guest
Jan 15th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4.  
  5.  
  6. const int N = 111;
  7. int n, count[N], g[N][N], left[N], right[N], max_value, ans, f[N];
  8.  
  9.  
  10. void input() {
  11.     std::cin >> n;
  12.     for (int i = 1; i <= n; ++i) {
  13.         int x;
  14.         std::cin >> x;
  15.         right[x] = i;
  16.         if (!left[x]) {
  17.             left[x] = i;
  18.         }
  19.         ++count[x];
  20.         max_value = std::max(max_value, x);
  21.     }
  22. }
  23.  
  24.  
  25. void build_graph() {
  26.     for (int i = 1; i <= max_value; ++i) {
  27.         for (int j = 1; j <= max_value; ++j) {
  28.             if (count[i] && count[j] && right[i] < left[j]) {
  29.                 g[i][j] = 1;
  30.             }
  31.         }
  32.     }
  33. }
  34.  
  35.  
  36. int calc(int v) {
  37.     if (f[v]) {
  38.         return f[v];
  39.     }
  40.     f[v] = count[v];
  41.     for (int next = 1; next <= max_value; ++next) {
  42.         if (g[v][next]) {
  43.             f[v] = std::max(f[v], count[v] + calc(next));
  44.         }
  45.     }
  46.     return f[v];
  47. }
  48.  
  49.  
  50. int main() {
  51.     freopen("input.txt", "r", stdin);
  52.     freopen("output.txt", "w", stdout);
  53.  
  54.     input();
  55.     build_graph();
  56.     for (int i = 1; i <= max_value; ++i) {
  57.         ans = std::max(ans, calc(i));
  58.     }
  59.     std::cout << ans << std::endl;
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement