Dang_Quan_10_Tin

SURF

Sep 4th, 2021
1,910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <vector>
  5. #define task "SURF"
  6. using namespace std;
  7. using ll = long long;
  8. using ld = long double;
  9.  
  10. constexpr int N = 1e4 + 2;
  11. constexpr int mod = 2019;
  12. constexpr int Inf = 1e9 + 7;
  13. int m, n, a[N], b[N];
  14. int dp[N][2], f[N][2], ans(0);
  15. int cur[N];
  16. vector<int> pos[N];
  17.  
  18. void Read()
  19. {
  20.     cin >> m >> n;
  21.  
  22.     for (int i = 1; i <= m; ++i)
  23.         cin >> a[i];
  24.  
  25.     for (int i = 1; i <= n; ++i)
  26.     {
  27.         cin >> b[i];
  28.         pos[b[i]].emplace_back(i);
  29.     }
  30. }
  31.  
  32. inline void Add(int &x, int y)
  33. {
  34.     x += y;
  35.     if (x >= mod)
  36.         x -= mod;
  37.     else if (x < 0)
  38.         x += mod;
  39. }
  40.  
  41. void calDP(int sign)
  42. {
  43.     dp[0][0] = dp[0][1] = 0;
  44.     f[0][0] = f[0][1] = 1;
  45.     b[0] = sign;
  46.  
  47.     for (int i = 1; i <= m; ++i)
  48.     {
  49.         int max0(-Inf), max1(-Inf), cnt0(0), cnt1(0);
  50.         for (int j = 0; j <= n; ++j)
  51.             if (b[j] > a[i])
  52.             {
  53.                 if (max1 < dp[j][1])
  54.                 {
  55.                     max1 = dp[j][1];
  56.                     cnt1 = f[j][1];
  57.                 }
  58.                 else if (max1 == dp[j][1])
  59.                     Add(cnt1, f[j][1]);
  60.             }
  61.             else if (b[j] < a[i])
  62.             {
  63.                 if (max0 < dp[j][0])
  64.                 {
  65.                     max0 = dp[j][0];
  66.                     cnt0 = f[j][0];
  67.                 }
  68.                 else if (max0 == dp[j][0])
  69.                     Add(cnt0, f[j][0]);
  70.             }
  71.             else
  72.             {
  73.                 if (max1 + 1 >= dp[j][0])
  74.                 {
  75.                     dp[j][0] = max1 + 1;
  76.                     f[j][0] = cnt1;
  77.                 }
  78.                 if (max0 + 1 >= dp[j][1])
  79.                 {
  80.                     dp[j][1] = max0 + 1;
  81.                     f[j][1] = cnt0;
  82.                 }
  83.             }
  84.         for (int j = (int)pos[a[i]].size() - 1; j > 0; --j)
  85.         {
  86.             if (dp[pos[a[i]][j]][0] == dp[pos[a[i]][j - 1]][0])
  87.                 Add(f[pos[a[i]][j]][0], -f[pos[a[i]][j - 1]][0]);
  88.  
  89.             if (dp[pos[a[i]][j]][1] == dp[pos[a[i]][j - 1]][1])
  90.                 Add(f[pos[a[i]][j]][1], -f[pos[a[i]][j - 1]][1]);
  91.         }
  92.     }
  93. }
  94.  
  95. void Solve()
  96. {
  97.     calDP(0);
  98.  
  99.     int maxn = *max_element(&dp[0][0], (&dp[0][0]) + N * 2);
  100.  
  101.     for (int i = 1; i <= n; ++i)
  102.     {
  103.         if (dp[i][0] == maxn)
  104.             (ans += f[i][0]) %= mod;
  105.         if (dp[i][1] == maxn)
  106.             (ans += f[i][1]) %= mod;
  107.         dp[i][0] = f[i][0] = dp[i][1] = f[i][1] = 0;
  108.     }
  109.  
  110.     calDP(N);
  111.  
  112.     int maxn2 = *max_element(&dp[0][0], (&dp[0][0]) + N * 2);
  113.  
  114.     //cout << maxn << " " << maxn2 << "\n";
  115.  
  116.     if (maxn2 > maxn)
  117.     {
  118.         maxn = maxn2;
  119.         ans = 0;
  120.     }
  121.  
  122.     for (int i = 1; i <= n; ++i)
  123.     {
  124.         //cout << i << ": " << dp[i][0] << " " << dp[i][1] << " " << f[i][0] << " " << f[i][1] << "\n";
  125.         if (dp[i][0] == maxn)
  126.             (ans += f[i][0]) %= mod;
  127.         if (dp[i][1] == maxn)
  128.             (ans += f[i][1]) %= mod;
  129.     }
  130.  
  131.     if (maxn < 3)
  132.     {
  133.         cout << 0;
  134.         return;
  135.     }
  136.  
  137.     cout << maxn << "\n"
  138.          << ans;
  139. }
  140.  
  141. int32_t main()
  142. {
  143.     ios_base::sync_with_stdio(0);
  144.     cin.tie(0);
  145.     cout.tie(0);
  146.     if (fopen(task ".INP", "r"))
  147.     {
  148.         freopen(task ".INP", "r", stdin);
  149.         freopen(task ".OUT", "w", stdout);
  150.     }
  151.     Read();
  152.     Solve();
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment