D、 牛妹爱数列
思路
简单 DP, 表示把前
位都变成
需要的最小代价,
表示把前
位都变成
需要的最小代价。
转移的方程:
如果 :
如果 :
参考代码
#pragma GCC optimize(2) #include<bits/stdc++.h> using namespace std; const int man = 2e5+10; #define IOS ios::sync_with_stdio(0) #define ull unsigned ll #define uint unsigned #define pai pair<int,int> #define pal pair<ll,ll> #define IT iterator #define pb push_back #define fi first #define se second #define For(i,j,k) for (int i=(int)(j);i<=(int)(k);++i) #define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);--i) #define endl '\n' #define ll long long const ll mod = 1e9+7; int a[man],dp[man][2]; signed main() { #ifndef ONLINE_JUDGE //freopen("in.txt", "r", stdin); //freopen("out.txt","w",stdout); #endif int n;scanf("%d",&n); for(int i = 1;i <= n;++i){ scanf("%d",&a[i]); } for(int i = 1;i <= n;++i){ if(a[i]){ dp[i][0] = min(dp[i-1][0],dp[i-1][1]+1) + 1; dp[i][1] = min(dp[i-1][0]+1,dp[i-1][1]); }else{ dp[i][0] = min(dp[i-1][0],dp[i-1][1]+1); dp[i][1] = min(dp[i-1][1],dp[i-1][0]) + 1; } } printf("%d\n",min(dp[n][0],dp[n][1]+1)); return 0; }