标签:ems math print string cpp else bool main space
很显然有一个结论:最大不过1,最小不过-1
然后dp,设\(f[i][j]\)为满足前\(i\)个不下降,当前放的是\(j-2\),转移就比较好想了
具体方程看代码吧,终于有一个自己会写的题了
代码(写了好多没用的min):
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=1e6+1;
int n,a[maxn],f[maxn][4],ans;
int main()
{
read(n);
for(rg int i=1;i<=n;i++)read(a[i]),a[i]+=2;
memset(f,63,sizeof f);f[1][a[1]]=0;
for(rg int i=2;i<=n;i++)
{
if(a[i]==1)
{
f[i][1]=min(f[i-1][1],f[i][1]);
f[i][3]=min(f[i-1][3]+2,f[i][3]);
}
if(a[i]==2)
{
f[i][1]=min(f[i-1][1]+1,f[i][1]);
f[i][2]=min(f[i-1][2],min(f[i-1][1],f[i][2]));
f[i][3]=min(f[i-1][3]+1,f[i][3]);
}
if(a[i]==3)
{
f[i][1]=min(f[i-1][1]+2,f[i][1]);
f[i][2]=min(f[i-1][1]+1,f[i][2]);
f[i][3]=min(f[i-1][1],min(f[i-1][2],min(f[i-1][3],f[i][3])));
}
}
ans=min(f[n][1],min(f[n][2],f[n][3]));
if(ans<=1e9)printf("%d\n",ans);
else printf("BRAK\n");
}
标签:ems math print string cpp else bool main space
原文地址:https://www.cnblogs.com/lcxer/p/10375841.html