#256 (Div. 2)C. Painting Fence

题意:给出篱笆的高度,宽度都是1,我们用宽度为1的刷子刷,只能横着或者竖着刷,连续的,问最少多少次全刷完

思路:我们可以全部竖着刷,N次,然后我们如果横着的话就是连续的非0数列中最短的和这个连续的数列都竖着刷比较,DFS

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=5002;
 4 int a[N];
 5
 6 int n;
 7
 8 int  dfs(int l,int r){
 9     int Min=1e9+7;
10     int sum=0;
11     if(l>r) return 0;
12     for(int i=l;i<=r;i++){
13         Min=min(Min,a[i]);
14     }
15     for(int i=l;i<=r;i++)
16         a[i]-=Min;
17     sum+=Min;
18   //  cout<<l<<" "<<r<<" "<<sum<<endl;
19     int ll=l;
20     for(int i=l;i<=r;i++){
21         if(a[i]==0){
22             sum+=min(i-ll,dfs(ll,i-1));
23
24             ll=i+1;
25         }
26     }
27     if(ll<=r){
28         sum+=min(r-ll+1,dfs(ll,r));
29     }
30     return sum;
31 }
32 int main(){
33     scanf("%d",&n);
34     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
35     cout<<min(n,dfs(1,n))<<endl;;
36
37 }
时间: 2024-11-06 09:32:02

#256 (Div. 2)C. Painting Fence的相关文章

递归解Codeforces Round #256 (Div. 2)C. Painting Fence

#include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; in

Codeforces Round #256 (Div. 2) C. Painting Fence

C. Painting Fence Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no

Codeforces Round #256 (Div. 2) C. Painting Fence(分治贪心)

题目链接:http://codeforces.com/problemset/problem/448/C ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

Codeforces Round #256 (Div. 2) C. Painting Fence (搜索 or DP)

[题目链接]:click here~~ [题目大意]:题意:你面前有宽度为1,高度给定的连续木板,每次可以刷一横排或一竖列,问你至少需要刷几次. Sample Input Input 5 2 2 1 2 1 Output 3 Input 2 2 2 Output 2 Input 1 5 Output 1 搜索: // C #ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include &

Codeforces Round #256 (Div. 2/C)/Codeforces448C_Painting Fence(分治)

解题报告 给篱笆上色,要求步骤最少,篱笆怎么上色应该懂吧,,,刷子可以在横着和竖着刷,不能跳着刷,,, 如果是竖着刷,应当是篱笆的条数,横着刷的话,就是刷完最短木板的长度,再接着考虑没有刷的木板,,, 递归调用,,, #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define inf 999999999999999 using namespace

Codeforces Round #256 (Div. 2)——Painting Fence

题目连接 题意: n个木条,输入n个木条的高度,每个木条的宽度均为1.现在有长宽均为1的刷子,每次可以选择横着刷或者竖着刷,每次刷的时候不能离开木条,问将所有木条均涂色至少需要刷几次.(刷的时候可以经过已经被刷过的地方) n (1?≤?n?≤?5000),1?≤?ai(高度)?≤?109 分析: 分析一下横着和竖着的关系:假设现在已经有一个操作集合S是答案,那么集合中的操作顺序是可以改变的,即横和竖的顺序可以改变(因为可以经过已经涂色的木条),那么不妨先横着涂色.对于当前[l,r]的区间,答案不

Codeforces Round #256 (Div. 2) C (448C)Painting Fence

分治!首先是一大块,贪行刷和竖刷的最小,再转化为小块............ AC代码如下: #include<iostream> #include<cstring> #include<cstdio> using namespace std; int n; int a[5005]; int solve(int l,int r) { int i,j; int len=r-l+1; int height=a[l]; for(i=l;i<=r;i++) if(a[i]&

Codeforces Round #256 (Div. 2) A/B/C/D

A. Rewards 水题 #include<cstdio> #include<iostream> #include<cstring> using namespace std; int main() { int a1,a2,a3,b1,b2,b3,s,t1,t2,sum1,sum2; while(scanf("%d%d%d",&a1,&a2,&a3)!=EOF) { scanf("%d%d%d",&

Codeforces #256 Div.2

B. Suffix Structure 1. 先判断s去掉一些元素是否能构成t,如果可以就是automaton 判断的方法也很简单,two pointer,相同元素同时++,不相同s的指针++,如果t能全找到,那么s能够去掉元素构成t. bool f(string s, string t) { int i = 0, j = 0; while (i < s.size() && j < t.size()) { if (s[i] == t[j]) { i++; j++; } else