Codeforces 1207 C. Gas Pipeline(简单dp)

题目链接

题意: 给一个长度为 n n n 01 01 01 串,和建单位长度的管道的代价 a a a 和单位长度的柱子 b b b 的代价,有 4 4 4 种柱子,低管道代价为 a + b a+b a+b ,高管道代价为 a + 2 ∗ b a+2*b a+2b,上升管道代价为 2 ∗ a + b 2*a+b 2a+b,下降管道代价为 2 ∗ a + 2 ∗ b 2*a+2*b 2a+2b, 01 01 01串中 1 1 1 代表要建高管道,一段高管道前要建上升管道,后要建下降管道,如果一个地方既要上升管道有要下降管道,则那个地方必须为高管道,问建这个区域管道和柱子的最小代价
思路: 这个题可以用贪心写,但是好像不好处理,垃圾博主贪不过
换用 d p dp dp 来解决这个问题 。
一开始可以先用令一个数组来表示每一根柱子是 高还是低(会好理解一点而已,可以不用)。
d p [ i ] [ 0 ] dp[i][0] dp[i][0] 表示到第 i i i 根柱子是低的最小花费, d p [ i ] [ 1 ] dp[i][1] dp[i][1] 表示到第 i i i 根柱子是高的最小总花费。

当第 i i i 根柱子是高时,就只能转移 d p [ i ] [ 1 ] dp[i][1] dp[i][1],否则都需要转移,具体转移见代码

Code:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define ptch putchar
#define CLR(a) while(!(a).empty()) a.pop()

using namespace std;
inline LL read() {
    LL s = 0,w = 1;
    char ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-') w = -1;
        ch = getchar();
    }
    while(isdigit(ch))
        s = s * 10 + ch - '0',ch = getchar();
    return s * w;
}
inline void write(LL x) {
    if(x < 0)
        putchar('-'), x = -x;
    if(x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
#ifndef ONLINE_JUDGE
    clock_t prostart = clock();
#endif

const int maxn = 2e5 + 10;
const LL inf = 0x3f3f3f3f3f3f3f3f;
bool vis[maxn];
char s[maxn];
LL dp[maxn][2];

int main() {
#ifndef ONLINE_JUDGE
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif

    int t = read();
    while(t --){
        clr(vis,0); clr(dp,inf);
        LL n = read(),a = read(),b = read();
        scanf("%s",s + 1);
        for(int i = 1;i <= n;++ i)
            if(s[i] == '1') vis[i - 1] = vis[i] = 1;
        dp[0][0] = b;
        for(int i = 1;i <= n;++ i){
            if(vis[i] == 1)
                dp[i][1] = min(dp[i - 1][0] + a * 2,dp[i - 1][1] + a) + b * 2;
            else {
                dp[i][0] = min(dp[i - 1][0] + a + b,dp[i - 1][1] + 2 * a + b);
                dp[i][1] = min(dp[i - 1][0] + a * 2,dp[i - 1][1] + a) + b * 2;
            }
        }
        write(dp[n][0]); ptch('\n');
    }

#ifndef ONLINE_JUDGE
    cout << "time: " << 1.0 * (clock() - prostart) / CLOCKS_PER_SEC << " s" << endl;
#endif
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值