タイトル:https://codeforces.com/contest/1287/problem/Cは
貪欲:思考
の中に文字列全体を(0 \)\セグメントと非\(0 \)段落
:容易に入手できる
場合は、\(0 \)端部であっても、またはその両方が奇数のフィルを充填し、またはされ、両方の偶数または奇数である\(複雑+ = 2 \)
場合\(0 \) 、異なるパリティのセグメントの両端(1複雑= + \)\
A場合\(0 \)セグメントは、それが充填された一方の端部のみを有し有する同じ周期パリティ数、または\(複雑+ = 1 \)
の両端になります\(0 \)貪欲のために大小規模の長さを
、最終的な決意一端のみ\(0 \)セグメント
#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;
}
二つのアイデア:DP
つの寸法:長さ、二次元:残りの偶数、D:現在のパリティの数
限界:
が偶数であり、最初の番号が偶数または0:\(DP [N- / 2-1 [1] ] [0] = 0 \)
最初の数である場合、\(0 \)または奇数:\(DP [1] [N- / 2] [1] = 0 \)。
状態遷移:
①も現在の充填:\( DP [I]、[J-1
] [0] =分(DP [I]、[J-1] [0]、DP [I-1]〜[J] [K] +(K == 1))\) 現在の充填奇数②:\(DP [I] [J] =分(DP [I]、[J]、DP [J] [K] +(K == 1)[1] [1] [1-Iは。]。 )\)
#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;
}