【杂题】 codeforces 448C Painting Fence

用类似线段树的方法。。找到线段中一个最小值。。分开递归下去。。如果有多个最小值不用管的,最后把大小和R-L+1比较一下,小的传上去就行啦。。。

#include <iostream>  
#include <queue>  
#include <stack>  
#include <map>  
#include <set>  
#include <bitset>  
#include <cstdio>  
#include <algorithm>  
#include <cstring>  
#include <climits>  
#include <cstdlib>
#include <cmath>
#define maxn 5005
#define eps 1e-10
#define mod 1000000009
#define INF 99999999  
#define lowbit(x) (x&(-x))  
#define lson o<<1, L, mid  
#define rson o<<1 | 1, mid+1, R  
typedef long long LL;
using namespace std;

int a[maxn];
int dfs(int L, int R, int done)
{
	if(R < L) return 0;
	if(L == R) {
		if(a[L]>done) return 1;
		else return 0;
	}
	int mi = INF, i, aa;
	for(i = L; i <= R; i++)
		if(mi > a[i] - done) {
			mi = a[i] - done;
			aa = i;
		}
	return min(R-L+1, dfs(L, aa-1, done+mi)+dfs(aa+1, R, done+mi)+mi);
}
int main(void)
{
	int i, n;
	scanf("%d", &n);
	for(i = 1; i <= n; i++) scanf("%d", &a[i]);
	printf("%d\n", dfs(1, n, 0));
	return 0;
}


你可能感兴趣的:(codeforces)