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]<height)
        height=a[i];
    for(i=l;i<=r;i++)
        a[i]-=height;
    int ans=height;
    for(i=l;i<=r;i++)
    {
        if(a[i]!=0)
        {
            int ll,rr;
            ll=i;
            j=i+1;
            while(j<=r&&a[j]!=0)
                j++;
            rr=j-1;
            ans+=solve(ll,rr);
        }
    }
    if(len<ans)
        ans=len;
    return ans;

}

int main()
{
    int i,j;
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
            cin>>a[i];
       int ans= solve(1,n);
       cout<<ans<<endl;
    }

    return 0;
}

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

时间: 2024-08-01 21:54:32

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

Codeforces Round #256 (Div. 2) B (448B) Suffix Structures

题意就是将第一个字符串转化为第二个字符串,支持两个操作,一个是删除,一个是更换字符位置. 简单的字符串操作!! AC代码如下: #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define M 50010 #define inf 100000000 using namespace std; char a[1005],b[1005]; int la,lb; b

Codeforces Round #288 (Div. 2)B(字符串)

题意:交换两个数,使得交换后的数是偶数且尽可能大: KEY:分情况,1末尾数为偶数,即找到比它小的偶数交换,假如没有比它小的偶数,不用交换.2末尾数是奇数,再分情况,<1>全都是奇数(这个可以在一开始就判断掉),即输出-1,<2>有一个偶数,无论如何谁大谁小都要交换,<3>全部偶数都比奇数大,从n - 2(n是字符串长度)开始找到一个偶数交换即可,<4>如果有一些偶数比末尾数大,有些比它小,即从0开始找到比奇数小的那个偶数交换即可.其实是分类讨论.(有更好的

Codeforces Round #597 (Div. 2)D(最小生成树)

/*每个点自己建立一座发电站相当于向超级源点连一条长度为c[i]的边,连电线即为(k[i]+k[j])*两点间曼哈顿距离,跑最小生成树(prim适用于稠密图,kruscal适用于稀疏图)*/ #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int fa[2007];int x[2007],y[2007];int c[2007],k[2007];long long m[2007][2007];vecto

Hot Days Codeforces Round #132 (Div. 2) D(贪婪)

Description The official capital and the cultural capital of Berland are connected by a single road running through n regions. Each region has a unique climate, so the i-th (1?≤?i?≤?n) region has a stable temperature of ti degrees in summer. This sum

Hot Days Codeforces Round #132 (Div. 2) D(贪心)

Description The official capital and the cultural capital of Berland are connected by a single road running through n regions. Each region has a unique climate, so the i-th (1?≤?i?≤?n) region has a stable temperature of ti degrees in summer. This sum

题解——Codeforces Round #508 (Div. 2) T3 (贪心)

贪心的选取最优解 然后相减好 记得要开long long #include <cstdio> #include <algorithm> #include <cstring> #include <set> #include <queue> #define int long long using namespace std; int ansa=0,ansb=0,posa=1,posb=1,n,a[1000100],b[1000100]; bool c

Codeforces Round #618 (Div. 1)C(贪心)

把所有数看作N块,后面的块比前面的块小的话就合并,这个过程可能会有很多次,因为后面合并后会把前面的块均摊地更小,可能会影响更前面地块,像是多米诺骨牌效应,从后向前推 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 double a[1000007]; 5 vector<double>vv[1000007]; 6 int main(){ 7 //ios::sync_wi

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 Round #256 (Div. 2)——Painting Fence

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