Advertisement
STANAANDREY

subsir de len max cls

Dec 21st, 2020
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #define NMAX 1003
  4. int n, v[NMAX], best[NMAX], p[NMAX], maxLen, maxPos;
  5.  
  6. void write(int pos) {
  7.     if (pos) {
  8.         write(p[pos]);
  9.         cout << v[pos] << ' ';
  10.     }
  11. }
  12.  
  13. int main() {
  14.     cin >> n;
  15.     for (int i = 1; i <= n; i++) {
  16.         cin >> v[i];
  17.         best[i] = 1;
  18.     }
  19.     for (int i = 1; i <= n; i++) {
  20.         for (int j = i - 1; j; j--) {
  21.             if (v[i] > v[j] && best[i] < best[j] + 1) {
  22.                 best[i] = best[j] + 1;
  23.                 p[i] = j;
  24.             }
  25.         }
  26.     }
  27.     for (int i = 1; i <= n; i++)
  28.         if (maxLen < best[i]) {
  29.             maxLen = best[i];
  30.             maxPos = i;
  31.         }
  32.     cout << maxLen << endl;
  33.     write(maxPos);
  34.     return 0;
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement