【学习笔记】动态规划

区间dp

牛客-牛妹爱数列

#include 
#define rep(i,a,b) for(int i=a;i
#define T int t ;cin >> t;while(t--)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-11;
const ll mod = 1e9 + 7;
ll dp[maxn][2] ,a[maxn] ;
int main(){
	memset(dp,0,sizeof dp) ;
	ll n ;
	cin >> n ;
	for(int i = 1 ; i <=n ;i++) cin >> a[i] ;
	for(int i = 1 ; i <= n ; i++){
		if(a[i]){
			dp[i][1] = dp[i-1][1] ;
			dp[i][0] = min(dp[i-1][0],dp[i-1][1]) +1  ;
		}
		else{
			dp[i][0] = dp[i-1][0] ;
			dp[i][1] = min(dp[i-1][0],dp[i-1][1]) +1  ;
		
		}
	}
	cout << min(dp[n][1]+1,dp[n][0]) << endl ;
	return 0 ;
}

你可能感兴趣的:(dp,学习笔记)