ZhongJiaXuan

CF1286A - Garland

Dec 14th, 2023 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4.     ios::sync_with_stdio(false);
  5.     cin.tie(0);
  6.     int n, v;
  7.     cin >> n;
  8.     vector<vector<int>> dp(n / 2 + 1, vector<int>(2));
  9.     for (int i = 1; i <= n / 2; i++) {
  10.         dp[i][0] = 1e9;
  11.         dp[i][1] = 1e9;
  12.     }
  13.     for (int i = 0; i < n; i++) {
  14.         cin >> v;
  15.         for (int j = n / 2; j >= 0; j--) {
  16.             if (v == 0 || v % 2 > 0) {
  17.                 dp[j][1] = min(dp[j][1], dp[j][0] + 1);
  18.             } else {
  19.                 dp[j][1] = 1e9;
  20.             }
  21.             if (j > 0 && v % 2 == 0) {
  22.                 dp[j][0] = min(dp[j - 1][0], dp[j - 1][1] + 1);
  23.             } else {
  24.                 dp[j][0] = 1e9;
  25.             }
  26.         }
  27.     }
  28.     cout << min(dp[n / 2][0], dp[n / 2][1]) << endl;
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment