mr_dot_convict

481-What-goes-up.UVa.mr.convict

Jun 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include         <bits/stdc++.h>
  9. using namespace std;
  10. #pragma GCC      optimize ("Ofast")
  11. #pragma GCC      optimize ("unroll-loops")
  12. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  13.  
  14. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  15. #define PREC     cout.precision (10); cout << fixed
  16. #define bg(x)    " [ " << #x << " : " << (x) << " ]"
  17. #define x        first
  18. #define y        second
  19. using ll = long long;
  20.  
  21. #define debug(args...) { \
  22.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  23.    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  24. }
  25. void err(istream_iterator<string> it) { it->empty();
  26.    cerr << " (Line : " << __LINE__ << ")" << '\n';
  27. }
  28. template<typename T, typename... Args>
  29. void err(istream_iterator<string> it, T a, Args... args) {
  30.     cerr << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  31.     err(++it, args...);
  32. }
  33.  
  34. const int N = (int)1e6 + 10;
  35. int A[N], L[N], Par[N], idx[N];
  36. int n, sz;
  37.  
  38. void LIS() {
  39.    n = 0;
  40.    sz = 0;
  41.    int tmp;
  42.    while (cin >> tmp) {
  43.       A[n] = tmp, ++n;
  44.    }
  45.  
  46.    int last_idx = -1;
  47.    for (int i = 0; i < n; ++i) {
  48.       int l = 0, h = sz - 1;
  49.       while (l <= h) {
  50.          int g = (l + h)/2;
  51.          if (A[i] > L[g]) l = g + 1;
  52.          else h = g - 1;
  53.       }
  54.       L[l] = A[i];
  55.       idx[l] = i;
  56.       if (l == sz) ++sz;
  57.       Par[i] = (l == 0 ? -1 : idx[l-1]);
  58.    }
  59.  
  60.    cout << sz << '\n' << "-\n";
  61.    vector <int> sol;
  62.    for (int i = last_idx + 1; i < n; ++i)
  63.       if (A[i] > A[last_idx]) last_idx = i;
  64.  
  65.    last_idx = idx[sz-1];
  66.    while (true) {
  67.       sol.push_back(A[last_idx]);
  68.       last_idx = Par[last_idx];
  69.       if (last_idx == -1) break;
  70.    }
  71.  
  72.    reverse(sol.begin(), sol.end());
  73.    for (int v : sol) cout << v << '\n';
  74. }
  75.  
  76. signed main() {
  77.    IOS; PREC;
  78.    LIS();
  79.  
  80.    return EXIT_SUCCESS;
  81. }
Add Comment
Please, Sign In to add comment