#include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; int n,a[5010]; int dfs(int l,int r,int sub) { if(l>r) return 0; int t=min_element(a+l,a+r+1)-a;//获取这个区间内最小的元素下标 return min(a[t]+dfs(l,t-1,a[t])+dfs(t+1,r,a[t])-sub,r-l+1); //最小次数等于,横着刷导致的之后刷的最小次数之和,竖着刷需要的次数,它们俩的最小值 } int main() { cin>>n; for(int i=0;i<n;i++) cin>>a[i]; cout<<dfs(0,n-1,0); }
递归解Codeforces Round #256 (Div. 2)C. Painting Fence
时间: 2024-10-06 00:16:13