Advertisement
pb_jiang

CF1286A

Oct 12th, 2022
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
  5. template <typename... Args> void logger(string vars, Args &&... values)
  6. {
  7.     cerr << vars << " = ";
  8.     string delim = "";
  9.     (..., (cerr << delim << values, delim = ", "));
  10.     cerr << endl;
  11. }
  12.  
  13. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  14. using ll = long long;
  15. using pii = pair<int, int>;
  16.  
  17. int n;
  18. int cnt[2];
  19. int a[1 << 7], zc[1 << 7];
  20.  
  21. int dp[1 << 7][1 << 7][2]; // dp[i][j][0/1] means in i-th place, using j th odd number, ending in even/odd number
  22.                            // the min score we can get
  23.  
  24. int main(int argc, char **argv)
  25. {
  26.     int max_v = 0x3F3F3F3F;
  27.     memset(dp, 0x3F, sizeof(dp));
  28.     scanf("%d", &n);
  29.     for (int i = 1; i <= n; ++i) {
  30.         scanf("%d", a + i);
  31.         if (a[i])
  32.             cnt[a[i] % 2]++, zc[i] = zc[i - 1];
  33.         else
  34.             zc[i] = zc[i - 1] + 1;
  35.     }
  36.     int ocap = (n + 1) / 2 - cnt[1];
  37.     int ecap = n / 2 - cnt[0];
  38.  
  39.     if (a[1] == 0) {
  40.         dp[1][0][1] = max_v;
  41.         dp[1][1][1] = 0;
  42.         dp[1][0][0] = 0;
  43.         dp[1][1][0] = max_v;
  44.     } else {
  45.         int f = a[1] % 2;
  46.         dp[1][0][f] = 0;
  47.         dp[1][1][f] = max_v;
  48.         dp[1][0][f ^ 1] = max_v;
  49.         dp[1][1][f ^ 1] = max_v;
  50.     }
  51.  
  52.     for (int i = 2; i <= n; ++i) {
  53.         int f = a[i] % 2;
  54.         if (a[i]) {
  55.             for (int j = 0; j <= ocap; ++j) {
  56.                 dp[i][j][f] = min(dp[i - 1][j][0] + (f == 0 ? 0 : 1), dp[i - 1][j][1] + (f == 1 ? 0 : 1));
  57.                 dp[i][j][f ^ 1] = max_v;
  58.             }
  59.         } else {
  60.             for (int j = 0; j <= zc[i] && j <= ocap; ++j) {
  61.                 dp[i][j][0] = min(dp[i - 1][j][0], dp[i - 1][j][1] + 1);
  62.             }
  63.             for (int j = 1; j <= zc[i] && j <= ocap; ++j) {
  64.                 dp[i][j][1] = min(dp[i - 1][j - 1][0] + 1, dp[i - 1][j - 1][1]);
  65.             }
  66.         }
  67.     }
  68.     printf("%d\n", min({dp[n][ocap][0], dp[n][ocap][1]}));
  69.     return 0;
  70. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement