Codeforces Round #612 (Div. 2) C. Garland

Title: https://codeforces.com/contest/1287/problem/C
Thinking a: greedy
the entire string into \ (0 \) segment and non \ (0 \) paragraph
readily available:
If a \ (0 \) end sections are both even or odd, even or both are filled fill odd, or \ (complexity + = 2 \)
If a \ (0 \) at both ends of segments of different parity, \ (. 1 Complexity = + \)
if a \ (0 \) segment has only one end, filled with it have that same period parity number, or \ (complexity + = 1 \)
will have both ends of the \ (0 \) length from small to large for the greedy
final determination only one end the \ (0 \) segment

#include<bits/stdc++.h>
 
using namespace std;
 
const int N=105;
 
int n;
int a[N];
int b[2][N];
int t[2];
int c[3];
int ans;
pair<int,int> st,ed;
 
void init()
{
    if((n&1)==0) c[0]=c[1]=n/2;
    else c[0]=n/2,c[1]=n/2+1;
}
void solve()
{
    int l=0,r=0,cnt=0;
    int pre=1;
    int f=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]==0)
        {
            if(cnt==0) l=pre&1;
            cnt++;
        }
        else
        {
            c[a[i]&1]--;
            if(a[i-1]!=0)
            {
                if((a[i]&1)!=(a[i-1]&1)) ans++;
            }
            if(cnt)
            {
                r=a[i]&1;
                if(!a[1]&&!f) f=1,st={cnt,r};
                else if(l==r) b[l][++t[l]]=cnt;
                else ans++;
                cnt=0;
            }
            pre=a[i];
        }
    }
    if(cnt) ed={cnt,l};
    sort(b[0]+1,b[0]+1+t[0]);
    sort(b[1]+1,b[1]+1+t[1]);
    for(int i=0;i<2;i++)
        for(int j=1;j<=t[i];j++)
        {
            if(c[i]>=b[i][j]) c[i]-=b[i][j];
            else ans+=2;
        }
    if(c[st.second]>=st.first) c[st.second]-=st.first;
    else ans++;
    if(c[ed.second]<ed.first) ans++; 
    cout<<ans<<endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    //freopen("in.txt","r",stdin);
    cin>>n;
    init();
    for(int i=1;i<=n;i++) cin>>a[i];
    solve();
    return 0;
}

Two ideas: DP
one dimension: length, two-dimensional: the remaining even number, D: the number of the parity of the current
limit:
When there is an even number and the first number is an even number or 0: \ (DP [. 1] [n-/ 2-1 ] [0] = 0 \)
when the first number is \ (0 \) or odd: \ (DP [. 1] [n-/ 2] [. 1] = 0 \)
state transition:
① current fill the even: \ ( dp [i] [j-1
] [0] = min (dp [i] [j-1] [0], dp [i-1] [j] [k] + (k == 1)) \) current fill odd ②: \ (DP [I] [J] [. 1] = min (DP [I] [J] [. 1], DP [. 1-I] [J] [K] + (K ==. 1) ) \)

#include<bits/stdc++.h>
 
using namespace std;
 
const int N=105;
 
int n;
int a[N];
int dp[N][N][2];
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    //freopen("in.txt","r",stdin);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    memset(dp,0x3f,sizeof dp);
    if(n>1&&(a[1]==0||a[1]%2==0)) dp[1][n/2-1][0]=0;
    if(a[1]==0||a[1]%2==1) dp[1][n/2][1]=0;
    for(int i=2;i<=n;i++)
        for(int j=0;j<=n/2;j++)
            for(int k=0;k<2;k++)
            {
                if(j>0&&(a[i]==0||a[i]%2==0)) dp[i][j-1][0]=min(dp[i][j-1][0],dp[i-1][j][k]+(k==1));
                if(i+j-1<n&&(a[i]==0||a[i]%2==1)) dp[i][j][1]=min(dp[i][j][1],dp[i-1][j][k]+(k==0));
            }
    cout<<min(dp[n][0][0],dp[n][0][1])<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/c4Lnn/p/12163898.html