Alex_tz307

Fundamente XI - 206

Sep 20th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. inline void drum(int x, int mx, int poz, int prev, vector < int >& a, vector < int >& lg) {
  6.     if(!poz)
  7.         return;
  8.     if(lg[poz] == mx && (prev == - 1 || a[poz] + x <= a[prev])) {
  9.         drum(x, mx - 1, poz - 1, poz, a, lg);
  10.         cout << a[poz] << ' ';
  11.     }
  12.     else
  13.         if(mx > 0)
  14.             drum(x, mx, poz - 1, prev, a, lg);
  15. }
  16.  
  17. int main() {
  18.     ios_base::sync_with_stdio(false);
  19.     cin.tie(nullptr);
  20.     cout.tie(nullptr);
  21.     int n, x;
  22.     cin >> n >> x;
  23.     vector < int > a(n + 1);
  24.     for(int i = 1; i <= n; ++i)
  25.         cin >> a[i];
  26.     vector < int > lg(n + 1);
  27.     lg[1] = 1;
  28.     int mx = 0, poz = 0;
  29.     for(int i = 2; i <= n; ++i) {
  30.         int ans = 0;
  31.         for(int j = 1; j < i; ++j)
  32.             if(lg[j] > ans && a[j] + x <= a[i])
  33.                 ans = lg[j];
  34.         lg[i] = ans + 1;
  35.         if(mx < lg[i]) {
  36.             mx = lg[i];
  37.             poz = i;
  38.         }
  39.     }
  40.     cout << mx << '\n';
  41.     drum(x, mx, poz, -1, a, lg);
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment