Advertisement
Malinovsky239

Untitled

Jan 15th, 2012
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4.  
  5. #define N 5005
  6.  
  7. using namespace std;
  8.  
  9. int n, m, a[N], b[N], dp[N];
  10.  
  11. int main() {
  12.     freopen("interview.in", "r", stdin);
  13.     freopen("interview.out", "w", stdout);
  14.  
  15.     cin >> n >> m;
  16.     for (int i = 0; i < n; i++)
  17.         cin >> a[i];
  18.  
  19.     for (int j = 0; j < m; j++)
  20.         cin >> b[j];
  21.  
  22.     for (int i = 0; i < n; i++) {
  23.         for (int pos1 = 0, pos2 = 0, mx = 1; pos1 < m; pos1++) {
  24.             while (b[pos1] != a[i] && pos1 < m) pos1++;
  25.             if (pos1 == m) continue;
  26.  
  27.             for (; pos2 < pos1; pos2++)
  28.                 if (b[pos2] < b[pos1])
  29.                     mx = max(mx, dp[pos2] + 1);              
  30.  
  31.             dp[pos1] = max(dp[pos1], mx);
  32.         }
  33.     }
  34.  
  35.     int ans = 0;
  36.     for (int i = 0; i < m; i++)
  37.         ans = max(ans, dp[i]);
  38.     cout << ans << endl;
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement