一个序列A, 每次可以 相邻的数相加为一个数字,求最少次数使得序列非降
f[i ]= min{ f [ j ] + i-j-1 } ,s[i]-s[j] >= s[j] -s[mn[j-1] ]
维护下前缀最小值mn[ i]
#include <iostream> #include <queue> #include <cstring> using namespace std; const int N =5002; int n,a[N],f[N],s[N],mn[N]; signed main(){ int i,j; cin>>n; memset(f,127,sizeof f); memset(mn,127,sizeof mn); for(i=1;i<=n;i++) cin>>a[i],s[i]=s[i-1]+a[i]; f[0]=mn[0]=0; for(i=1;i<=n;i++) for(j=0;j<i;j++){ if(mn[j]<=s[i]-s[j]){ mn[i]=min(mn[i],s[i]-s[j]); f[i]=min(f[i],f[j]+i-j-1); } } cout<<f[n]<<endl; }
标签:memset,Towers,mn,int,sizeof,include,CF229D From: https://www.cnblogs.com/towboa/p/17255834.html