【Codeforces 448 C. Painting Fence】分治

2 篇文章 0 订阅

cf448C
• 每块木板宽度均为1,高度为h[i]
• n块木板连接为宽度为n的栅栏
• 每次可以刷一横或一竖(上色)
• 最少刷多少次可以使得栅栏被全部上色
• 1 ≤ n ≤ 5000

沙雕数据结构选手上来定义四个状态
横横 - 0
竖竖 - 1
横竖 - 2
竖横 - 3

然后WA 因为他竖的长度不一定一定要删一个

比如
5
2 2 1 5 1
沙雕数据选手之前题意 直接 4 刀呗
但是题目可以是3刀
WA的代码

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);

const int MAX_N = 5025;
ll arr[MAX_N];
ll Max(ll a,ll b) {return a>b?a:b;}
ll Min(ll a,ll b) {return a<b?a:b;}
namespace sgt
{
    #define mid ((l+r)>>1)
    ll s[MAX_N<<2][4],L[MAX_N<<2],R[MAX_N<<2];//0 - row , 1 -clc, 2 clc+row
    void up(int rt)
    {
        L[rt] = L[rt<<1];
        R[rt] = R[rt<<1|1];
        s[rt][0] = Min(s[rt<<1][0]+s[rt<<1|1][0] - Min(R[rt<<1],L[rt<<1|1]),s[rt<<1][2]+s[rt<<1|1][3]);
        s[rt][1] = Min(s[rt<<1][1]+s[rt<<1|1][1],s[rt<<1][3]+s[rt<<1|1][2]-Min(R[rt<<1],L[rt<<1|1]));
        s[rt][2] = Min(s[rt<<1][0]+s[rt<<1|1][1],s[rt<<1][2]+s[rt<<1|1][1]);
        s[rt][3] = Min(s[rt<<1][1]+s[rt<<1|1][0],s[rt<<1][1]+s[rt<<1|1][3]);
    }
    void build(int rt,int l,int r)
    {
        if(l==r)
        {
            L[rt] = R[rt] = arr[l];
            s[rt][0] = s[rt][2] = s[rt][3] =  arr[l];
            s[rt][1] = 1;
            return ;
        }
        build(rt<<1,l,mid);
        build(rt<<1|1,mid+1,r);
        up(rt);
    }
    #undef mid
}


int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n;
    scanf("%d",&n);
    for(int i = 1;i<=n;++i) scanf("%lld",&arr[i]);
    sgt::build(1,1,n);
    printf("%lld\n",Min(Min(sgt::s[1][0],sgt::s[1][1]),Min(sgt::s[1][2],sgt::s[1][3])));
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


其实我们发现 如果你想横着刷 那么你最低的minn一定要刷完 这是一定的
所以我们就可以分治 答案 = min(区间全是竖的方案,minn+∑多个小区域答案)
如果宽度为1 那么最多一刀

AC代码

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);

/*namespace sgt
{
    #define mid ((l+r)>>1)

    #undef mid
}*/
const int MAX_N = 5025;
ll arr[MAX_N];
ll dfs(int l,int r)
{
    if(l==r) return 1;
    ll minn = INF;
    for(int i = l;i<=r;++i) minn = min(minn,arr[i]);
    for(int i = l;i<=r;++i) arr[i]-=minn;
    int p = l;
    ll ans = 0;
    for(int i = l;i<=r;++i)
    {
        if(arr[i]&&(i==r||!arr[i+1])) ans+=dfs(p,i);
        if(!arr[i]&&i<r&&arr[i+1]) p = i+1;
    }
    return min(1ll*(r-l+1),minn+ans);
}
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n;
    scanf("%d",&n);
    for(int i = 1;i<=n;++i) scanf("%lld",&arr[i]);
    printf("%lld\n",dfs(1,n));
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值