跳到正文

个人记录

2

wangsy2014约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年2月3日
更新
2026年2月3日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2505;
int n,m,a[N],mid,dp[N];
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
        int x;
		cin>>x;
        if(x==2)
        {
            x=-1;
        }
        a[i]=a[i-1]+x;
	}
    memset(dp,0x3f,sizeof(dp));
    dp[0]=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            int sum=a[i]-a[j-1];
            sum=abs(sum);
            if(sum<=m || sum==i-j+1)
            {
                dp[i]=min(dp[i],dp[j-1]+1);
            }
        }
    }
    cout<<dp[n];
	return 0;
 }
 /*

5 1
2
2
1
2
2

 */
原文