标签:namespace input scanf line fence nim inf until oca
"Holiday is coming, holiday is coming, hurray hurray!" shouts Joke in the last day of his college. On this holiday, Joke plans to go to his grandmother‘s house located in Schematics village. Joke‘s grandmother‘s house is more than a hundred years old. Joke is very kind hearted, so he wants to help renovate the house by painting the fence. The fence consists of N vertical boards placed on a line. The boards are numbered 1 to N from left to right, and each of them has the length of 1 meter and the height of Ai meters.
Joke‘s grandmother has a paintbrush that can be used to paint the fence. That paintbrush has a length of 1 meter. Joke paints the fence by brushing either horizontally or vertically, but the paint is expensive so Joke wants to minimize the number of paintbrush stroke. On each stroke, the paintbrush will make either a horizontal or vertical line. Also, the paintbrush must be touching the fence for the entire duration of the stroke. Joke also does not want to paint previously panted segment before. Help Joke to find the minimum number of stroke until the entire fence is covered with paint.
First line contains a number N, the number of boards on the fence. The second line contains N numbers, A1, A2, A3 ... An representing the height of each board.
Minimum number of stroke to paint the entire fence.
5 2 2 1 2 1
3
2 2 2
2
1 5
1
题意:又N个宽度为1的相邻围栏,每个有高度a[i],现在有一把宽度为1的刷子,可以横着刷或者竖着刷,问最少多少次刷完。
思路:对于每个区间,我们的最优情况的全部竖着刷, 或者横着刷全部公有的部分,其他的继续讨论。
由于每次最小横着刷一个,所以讨论次数不超过N。复杂度低于O(N^2)
#include<bits/stdc++.h> using namespace std; const int maxn=5010; const int inf=1e9+7; int a[maxn]; int solve(int L,int R,int H) { if(L==R) return 1; int ht=inf,res=0,r; for(int i=L;i<=R;i++) ht=min(ht,a[i]); for(int i=L;i<=R;i++){ if(ht==a[i]) continue; r=i; while(r<R&&a[r+1]>ht) r++; res+=solve(i,r,ht); i=r+1; } res=min(R-L+1,res+ht-H); return res; } int main() { int N,i; scanf("%d",&N); for(i=1;i<=N;i++) scanf("%d",&a[i]); printf("%d\n",solve(1,N,0)); return 0; }
标签:namespace input scanf line fence nim inf until oca
原文地址:https://www.cnblogs.com/hua-dong/p/9090995.html