Advertisement
BaoJIaoPisu

Untitled

Nov 11th, 2022
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using ll = long long;
  6. using ld = long double;
  7. using ull = unsigned long long;
  8.  
  9. using pii = pair<int, int>;
  10. using pll = pair<ll, ll>;
  11. using pld = pair<ld, ld>;
  12.  
  13. #define fi first
  14. #define se second
  15. #define pb push_back
  16. #define pf push_front
  17. #define mp make_pair
  18. #define ins insert
  19. #define btpc __builtin_popcount
  20. #define btclz __builtin_clz
  21.  
  22. #define sz(x) (int)(x.size());
  23. #define all(x) x.begin(), x.end()
  24. #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  25.  
  26. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  27.  
  28. int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
  29. int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  30. int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  31.  
  32. template<class X, class Y>
  33.     bool minimize(X &x, const Y &y) {
  34.         if (x > y)
  35.         {
  36.             x = y;
  37.             return true;
  38.         }
  39.         return false;
  40.     }
  41. template<class X, class Y>
  42.     bool maximize(X &x, const Y &y) {
  43.         if (x < y)
  44.         {
  45.             x = y;
  46.             return true;
  47.         }
  48.         return false;
  49.     }
  50.  
  51. const int MOD = 1e9 + 7; //998244353
  52.  
  53. template<class X, class Y>
  54.     void add(X &x, const Y &y) {
  55.         x = (x + y);
  56.         if(x >= MOD) x -= MOD;
  57.     }
  58.  
  59. template<class X, class Y>
  60.     void sub(X &x, const Y &y) {
  61.         x = (x - y);
  62.         if(x < 0) x += MOD;
  63.     }
  64.  
  65. /* Author : Le Ngoc Bao Anh, 12A5, LQD High School for Gifted Student*/
  66.  
  67. const ll INF = 1e9;
  68. const int N = 2000 + 10;
  69.  
  70. template <class T> struct FenwickTree {
  71.     int n;
  72.     vector<T> bit;
  73.  
  74.     FenwickTree(int _n = 0) {
  75.         n = _n;
  76.         bit.resize(n + 5);
  77.         for(int i = 1; i <= n; i++) bit[i] = 0;
  78.     }
  79.  
  80.     void update(int pos, T x) {
  81.         for(int i = pos; i <= n; i += i & (-i)) maximize(bit[i], x);
  82.     }
  83.  
  84.     T get(int pos) {
  85.         T ans = 0;
  86.         for(int i = pos; i > 0; i -= i & (-i)) maximize(ans, bit[i]);
  87.         return ans;
  88.     }
  89. };
  90.  
  91. int a[N], b[N], f[N][N];
  92. FenwickTree<int> pref[N], suff[N];
  93. int nxt[N][25];
  94.  
  95. void solve() {
  96.     int n, m; cin >> n >> m;
  97.     for(int i = 1; i <= n; i++) cin >> b[i];
  98.     vector<int> val;
  99.     for(int i = 1; i <= m; i++) {
  100.         cin >> a[i];
  101.         val.pb(a[i]);
  102.     }
  103.  
  104.     sort(all(val)); val.resize(unique(all(val)) - val.begin());
  105.  
  106.     for(int i = 1; i <= m; i++) {
  107.         a[i] = lower_bound(all(val), a[i]) - val.begin() + 1;
  108.     }
  109.  
  110.     // f[x][j] -> f[x][pos]
  111.     // f[i ; i < x][j] -> f[x][pos]
  112.     // f[i; i > x][j] -> f[x][pos];
  113.  
  114.     swap(n, m);
  115.     for(int i = 1; i <= m; i++) {
  116.         pref[i] = FenwickTree<int>(n);
  117.         suff[i] = FenwickTree<int>(n);
  118.     }
  119.  
  120.     for(int j = 1; j <= m; j++) {
  121.         pref[j].update(a[1], 1);
  122.         suff[j].update(n - a[1] + 1, 1);
  123.     }
  124.  
  125.     for(int i = 1; i <= 20; i++) nxt[m + 1][i] = INF;
  126.     for(int i = m; i >= 1; i--) {
  127.         for(int j = 1; j <= 20; j++) nxt[i][j] = nxt[i + 1][j];
  128.         nxt[i][b[i]] = i;
  129.     }
  130.  
  131.     for(int i = 2; i <= n; i++) {
  132.         for(int j = m; j >= 1; j--) {
  133.             int v = f[a[i]][j];
  134.             int pos = nxt[j + 1][b[j]];
  135.             if(pos < INF) {
  136.                 maximize(f[a[i]][pos], v + 1);
  137.                 pref[pos].update(a[i], v + 1);
  138.                 suff[pos].update(n - a[i] + 1, v + 1);
  139.             }
  140.         }  
  141.  
  142.         FenwickTree<int> p, s;
  143.         p = s = FenwickTree<int>(21);
  144.  
  145.         for(int j = 1; j <= m; j++) {
  146.             maximize(f[a[i]][j], p.get(b[j] - 1) + 1);
  147.             maximize(f[a[i]][j], s.get(20 - b[j]) + 1);
  148.             p.update(b[j], pref[j].get(a[i] - 1));
  149.             s.update(20 - b[j] + 1, suff[j].get(n - a[i]));
  150.             pref[j].update(a[i], f[a[i]][j]);
  151.             suff[j].update(n - a[i] + 1, f[a[i]][j]);
  152.         }
  153.     }
  154.  
  155.     int ans = 0;
  156.     for(int i = 1; i <= m; i++) maximize(ans, pref[i].get(n));
  157.     cout << n + m - 2 * ans;
  158. }
  159.  
  160. int main()
  161. {
  162.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  163.     #ifndef ONLINE_JUDGE
  164.     freopen("input.txt", "r", stdin);
  165.     freopen("output.txt", "w", stdout);
  166.     #else
  167.     //online
  168.     #endif
  169.  
  170.     int tc = 1, ddd = 0;
  171.     // cin >> tc;
  172.     while(tc--) {
  173.         //ddd++;
  174.         //cout << "Case #" << ddd << ": ";
  175.         solve();
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement