1286A

1286A - Garland

玄学特判+贪心

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(ll  i=a;i<=b;i++)
#define dec(i,a,b)      for(ll  i=a;i>=b;i--)
#define forn(i, n)      for(ll i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846;
const double eps = 1e-6;
const int mod = 998244353;
const int N = 2e5 + 5;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
ll lcm(ll m, ll n)
{
    return m * n / gcd(m, n);
}
bool prime(int x) {
    if (x < 2) return false;
    for (int i = 2; i * i <= x; ++i) {
        if (x % i == 0) return false;
    }
    return true;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}
inline int add(int x, int y) {
    return ((x%mod)+(y%mod))%mod;
}
inline int sub(int x, int y) {
    x -= y;
    return x < 0 ? x += mod : x;
}
inline int mul(int x, int y) {
    return (1ll * (x %mod) * (y % mod))%mod;
}
inline int Inv(int x) {
    return qpow(x, mod - 2);
}

int main()
{
    int n,res=0;
    cin >> n;
    vector<int> a(n + 1);
    int odd = (n + 1) / 2, even = n / 2, cnt = 0;
    priority_queue<int, vector<int>, greater<int>> oddq, evenq, mixq;
    rep(i, 1, n)
    {
        cin >> a[i];
        if (a[i] != 0)
        {
            if (a[i] % 2)
                odd--;
            else
                even--;
            if (a[i - 1] != 0)
            {
                if (a[i] % 2 != a[i - 1] % 2)
                    res++;
            }
        }
        else
            cnt++;
    }
    if (n == 1)
    {
        cout << 0 << endl;
        return 0;
    }
    if (cnt == n)
    {
        cout << 1 << endl;
        return 0;
    }
    int pos0 = 1, pos1, pos2 = n;
    for (; pos0 <= n && a[pos0] == 0; pos0++);
    for (; pos2 >= 0 && a[pos2] == 0; pos2--);
    pos1 = pos0;
    while (pos0 < pos2)
    {
        int last = a[pos0] % 2, len = 0;
        pos0++;
        while (pos0 <= pos2 && a[pos0] == 0)
        {
            pos0++;
            len++;
        }
        if (pos0 > pos2)
            break;
        if (len)
        {
            int cur = a[pos0] % 2;
            if (last != cur)
                mixq.push(len);
            else
            {
                if (cur)
                    oddq.push(len);
                else
                    evenq.push(len);
            }
        }
    }
    while (!oddq.empty() && oddq.top()<=odd)
    {
        odd -= oddq.top();
        oddq.pop();
    }
    while (!evenq.empty() && evenq.top() <= even)
    {
        even -= evenq.top();
        evenq.pop();
    }
    if (a[pos1] % 2)
    {
        if (odd >= pos1 - 1)
            odd -= pos1 - 1;
        else
            res++;
    }
    else
    {
        if (even >= pos1 - 1)
            even -= pos1 - 1;
        else
            res++;
    }
    if (a[pos2] % 2)
    {
        if (odd >= n-pos2)
            odd -= n - pos2;
        else
            res++;
    }
    else
    {
        if (even >= n - pos2)
            even -= n - pos2;
        else
            res++;
    }
    cout << res + 2 * oddq.size() + mixq.size() + evenq.size() * 2 << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/dealer/p/13245156.html