mr_dot_convict

11456-Trainsorting-UVa-mr.convict

Jun 21st, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 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. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  14. #define PREC     cout.precision (10); cout << fixed
  15. #define bg(x)    " [ " << #x << " : " << (x) << " ]"
  16. #define x        first
  17. #define y        second
  18. // using ll = long long;
  19.  
  20. #define debug(args...) { \
  21.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  22.    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  23. }
  24. void err(istream_iterator<string> it) { it->empty();
  25.    cerr << " (Line : " << __LINE__ << ")" << '\n';
  26. }
  27. template<typename T, typename... Args>
  28. void err(istream_iterator<string> it, T a, Args... args) {
  29.     cerr << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  30.     err(++it, args...);
  31. }
  32.  
  33. const int N = (int)1e6 + 10;
  34. vector <int> ar;
  35. int L[N], U[N];
  36. int n, szl, szu;
  37.  
  38. void solve() {
  39.    reverse(ar.begin(), ar.end());
  40.  
  41.    szl = szu = 0;
  42.    int mx_sz = 0;
  43.    for (int i = 0; i < n; ++i) {
  44.       int ll = 0, hl = szl - 1;
  45.       while (ll <= hl) { // LIS
  46.          int g = (ll + hl)/2;
  47.          if (ar[i] > L[g]) ll = g + 1;
  48.          else hl = g - 1;
  49.       }
  50.       if (ll == szl) ++szl;
  51.       L[ll] = ar[i];
  52.  
  53.       int lu = 0, hu = szu - 1;
  54.       while (lu <= hu) { // LDS
  55.          int g = (lu + hu)/2;
  56.          if (ar[i] < U[g]) lu = g + 1;
  57.          else hu = g - 1;
  58.       }
  59.       if (lu == szu) ++szu;
  60.       U[lu] = ar[i];
  61.  
  62.       int LIS = ll + 1, LDS = lu + 1;
  63.  
  64.       mx_sz = max(mx_sz, LIS + LDS -1);
  65.    }
  66.    cout << mx_sz << '\n';
  67. }
  68.  
  69. void read() {
  70.    cin >> n;
  71.    ar.assign(n, 0);
  72.    for (int i = 0; i < n; ++i) cin >> ar[i];
  73. }
  74.  
  75. signed main() {
  76.    IOS; PREC;
  77.    int tc; cin >> tc;
  78.    while (tc--) read(), solve();
  79.  
  80.    return EXIT_SUCCESS;
  81. }
Add Comment
Please, Sign In to add comment