Advertisement
CooBin

lzs

Nov 13th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. // I'm the bone of my code. Syntax is my body, Data is my blood ...
  2. #include<bits/stdc++.h>
  3. #define FILE "lzs"
  4. using namespace std;
  5.  
  6. const int mod = 1e9 + 9;
  7. const int N = 2e4 + 10;
  8. const int inf = 1e9 + 10;
  9.  
  10. int n, m;
  11. int a[N], b[N];
  12.  
  13. int vis[N];
  14. int dp[N][2], num[N][2];
  15.  
  16. void add(int &a, int &b, int c, int d)
  17. {
  18.     if (a < c) a = c, b = d;
  19.     else
  20.     if (a == c) b = ((b + d) % mod + mod) % mod;
  21. }
  22.  
  23. void solve()
  24. {
  25.     for (int i = 1; i <= n; i++)
  26.         dp[i][0] = 0, dp[i][1] = -inf;
  27.     // 0 la chan, 1 la le
  28.     for (int i = 1; i <= n; i++)
  29.     {
  30.         // i is da best
  31.         int best[2] = {0, -inf};
  32.         int acc[2] = {1, 0};
  33.  
  34.         for (int j = 1; j <= m; j++)
  35.         {
  36.             if (a[i] == b[j]) // always reset
  37.             {
  38.                 for (int type = 0; type < 2; type++)
  39.                     dp[j][type] = 0, num[j][type] = 0,
  40.                     add(dp[j][type], num[j][type], best[1 - type] + 1, acc[1 - type]);
  41.                 continue;
  42.             }
  43.  
  44.             int curType = (a[i] > b[j] ? 1 : 0);
  45.             if (vis[ b[j] ])
  46.             {
  47.                 int old = vis[ b[j] ];
  48.                 assert(b[old] == b[j]);
  49.                 add(best[curType], acc[curType], dp[old][curType], -num[old][curType]);
  50.             }
  51.             vis[ b[j] ] = j;
  52.             add(best[curType], acc[curType], dp[j][curType], num[j][curType]);
  53.  
  54.         }
  55.         for (int j = 1; j <= m; j++) vis[ b[j] ] = 0;
  56.     }
  57.  
  58.     int resx = 0, resy = 0;
  59.     for (int i = m; i >= 1; i--)
  60.         if (!vis[b[i]])
  61.         {
  62.             add(resx, resy, dp[i][1], num[i][1]),
  63.             vis[b[i]] = 1;
  64.         }
  65.  
  66.     cout << resx << ' ' << resy << '\n';
  67. }
  68.  
  69. int main()
  70. {
  71.     if (fopen("main.in", "r"))
  72.         assert(freopen("main.in", "r", stdin));
  73.     else
  74.     if (fopen(FILE".in", "r"))
  75.         assert(freopen(FILE".in", "r", stdin)),
  76.         assert(freopen(FILE".out", "w", stdout));
  77.    
  78.     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  79.    
  80.     cin >> n;
  81.     for (int i = 1; i <= n; i++) cin >> a[i];
  82.     cin >> m;
  83.     for (int i = 1; i <= m; i++) cin >> b[i];
  84.  
  85.     solve();
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement