Advertisement
Dang_Quan_10_Tin

Dãy con lượn sóng

Oct 19th, 2022
1,364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <vector>
  5.  
  6. using namespace std;
  7. using ll = long long;
  8.  
  9. constexpr int N = 1e4 + 5;
  10. constexpr ll mod = 2019;
  11.  
  12. int n, m;
  13. int a[N], b[N], last[N], pre[N];
  14. int16_t dp[N][N][2], cnt[N][N][2];
  15.  
  16. void Read()
  17. {
  18.     cin >> m >> n;
  19.  
  20.     for (int i = 1; i <= m; ++i)
  21.         cin >> a[i];
  22.     for (int i = 1; i <= n; ++i)
  23.         cin >> b[i];
  24. }
  25.  
  26. void Solve()
  27. {
  28.     // Prepare last
  29.  
  30.     for (int i = 1; i <= n; ++i)
  31.     {
  32.         last[i] = pre[b[i]];
  33.         pre[b[i]] = i;
  34.     }
  35.  
  36.     // Proccess
  37.  
  38.     int res(0), amount(0);
  39.  
  40.     for (int i = 1; i <= m; ++i)
  41.         for (int j = 1, ans[2] = {0, 0}, dem[2] = {1, 1}; j <= n; ++j)
  42.         {
  43.             // t == 0
  44.             {
  45.                 dp[i][j][0] = dp[i - 1][j][0];
  46.                 cnt[i][j][0] = cnt[i - 1][j][0];
  47.  
  48.                 if (b[j] < a[i])
  49.                 {
  50.                     if (ans[0] < dp[i - 1][j][1])
  51.                     {
  52.                         ans[0] = dp[i - 1][j][1];
  53.                         dem[0] = cnt[i - 1][j][1];
  54.                     }
  55.                     else if (ans[0] == dp[i - 1][j][1])
  56.                     {
  57.                         dem[0] += cnt[i - 1][j][1];
  58.                         if (last[j] > 0 && dp[i - 1][j][1] == dp[i - 1][last[j]][1])
  59.                             dem[0] -= cnt[i - 1][last[j]][1];
  60.  
  61.                         dem[0] %= mod;
  62.                     }
  63.                 }
  64.                 else if (b[j] == a[i])
  65.                 {
  66.                     dp[i][j][0] = ans[0] + 1;
  67.                     cnt[i][j][0] = dem[0];
  68.                 }
  69.             }
  70.  
  71.             // t == 1
  72.             {
  73.                 dp[i][j][1] = dp[i - 1][j][1];
  74.                 cnt[i][j][1] = cnt[i - 1][j][1];
  75.  
  76.                 if (b[j] > a[i])
  77.                 {
  78.                     if (ans[1] < dp[i - 1][j][0])
  79.                     {
  80.                         ans[1] = dp[i - 1][j][0];
  81.                         dem[1] = cnt[i - 1][j][0];
  82.                     }
  83.                     else if (ans[1] == dp[i - 1][j][0])
  84.                     {
  85.                         dem[1] += cnt[i - 1][j][0];
  86.                         if (last[j] > 0 && dp[i - 1][j][0] == dp[i - 1][last[j]][0])
  87.                             dem[1] -= cnt[i - 1][last[j]][0];
  88.  
  89.                         dem[1] %= mod;
  90.                     }
  91.                 }
  92.                 else if (b[j] == a[i])
  93.                 {
  94.                     dp[i][j][1] = ans[1] + 1;
  95.                     cnt[i][j][1] = dem[1];
  96.                 }
  97.             }
  98.         }
  99.  
  100.     for (int j = 1; j <= n; ++j)
  101.         for (int t = 0; t < 2; ++t)
  102.         {
  103.             if (res < dp[m][j][t])
  104.             {
  105.                 res = dp[m][j][t];
  106.                 amount = cnt[m][j][t];
  107.             }
  108.             else if (res == dp[m][j][t])
  109.             {
  110.                 amount += cnt[m][j][t];
  111.  
  112.                 if (last[j] > 0 && dp[m][j][t] == dp[m][last[j]][t])
  113.                     amount -= cnt[m][last[j]][t];
  114.  
  115.                 amount %= mod;
  116.             }
  117.         }
  118.  
  119.     cout << res << "\n"
  120.          << (amount + mod) % mod;
  121. }
  122.  
  123. int32_t main()
  124. {
  125.     ios_base::sync_with_stdio(0);
  126.     cin.tie(0);
  127.     cout.tie(0);
  128.     Read();
  129.     Solve();
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement