cf_225/C_Barcode

本文介绍了一种使用动态规划(DP)算法解决特定场景下颜色选择问题的方法。问题要求控制每一列的颜色一致,通过定义状态dp[i][j]表示前i个满足条件且第i个颜色为j的最小花费,实现最优方案的选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

這個DP,感覺很簡單,由於題目要求控制每一列都需要一樣的顏色,所以row基本沒有起什麼作用
這裏我的設置的狀態dp[i][j] 表示前i個滿足題目要求且第i個的顏色爲j的最少花費
dp[i][j] = min{dp[k][!j]+cost[k+1][i]}, rst = min(dp[n][0], dp[n][1])

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define BLACK   0
#define COLOR   2
#define MAXN    1001

char graph[MAXN][MAXN];
int f[MAXN][COLOR], cost[MAXN][COLOR];

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
        freopen("test.in", "r", stdin);
#endif
        int row, col, x, y, rst_black, rst_white, tmp;
        while( ~scanf("%d %d %d %d", &row, &col, &x, &y) ) {
                for(int i = 0; i < row; i ++) {
                        scanf("%s", graph[i]);
                }
                for(int i = 0; i <= col; i ++) {
                        memset(f[i], 0x3F, sizeof(f[i]));
                }
                for(int j = 0; j < col; j ++) {
                        rst_black = rst_white = 0;
                        for(int i = 0; i < row; i ++) {
                                if( '.' == graph[i][j] ) {
                                        rst_black ++;
                                }
                                else if( '#' == graph[i][j] ) {
                                        rst_white ++;
                                }
                        }
                        cost[j+1][BLACK] = row-rst_white; cost[j+1][!BLACK] = row-rst_black;
                }
                f[0][BLACK] = f[0][!BLACK] = 0;
                for(int i = 1; i <= col; i ++) {
                        for(int k = x; k <= y && i-k+1 >= 1; k ++) {
                                rst_black = rst_white = 0;
                                for(int z = i; z >= i-k+1; z --) {
                                        rst_black += cost[z][BLACK]; rst_white += cost[z][!BLACK];
                                }
                                f[i][BLACK] = min(f[i][BLACK], f[i-k][!BLACK]+rst_black);
                                f[i][!BLACK] = min(f[i][!BLACK], f[i-k][BLACK]+rst_white);
                        }
                }
                printf("%d\n", min(f[col][BLACK], f[col][!BLACK]));
        }
        return 0;
}




{"success":true,"code":"200","message":"\u64cd\u4f5c\u6210\u529f","data":[{"OrderId":"15426","DXFName":"NC80\u5916\u6846\u6599","BarCad":"https:\/\/wincc.oss-cn-qingdao.aliyuncs.com\/Wincc_luopusijin\/material\/profile\/image\/barCad_82a9ff83-dd30-4085-b33f-9546fbebe47a.png","BarName":"NC80\u5916\u6846\u6599","BarColor":"\u5916\u5496\u5561\/\u5185\u8910\u8272","BarCode":"NC8020","BarLength":"6000","BarThick":"2","BarCount":"2","BaDatum":2.6,"ResultId":"875982","BarWidth":30,"BarHeight":40,"Bar":[{"BarName":"NC80\u5916\u6846\u5927\u9762\u5b50\u6599","BarNumber":1,"BarType":"A","BarWidth":40,"BarHeight":50,"BarMouth":18},{"BarName":"NC80\u5916\u6846\u5c0f\u9762\u5b50\u6599","BarNumber":2,"BarType":"B","BarWidth":46,"BarHeight":80,"BarMouth":10},{"BarName":"\u9694\u70ed\u676124I","BarNumber":3,"BarType":"O","BarWidth":12,"BarHeight":12,"BarMouth":10}]},{"OrderId":"15426","DXFName":"NC80\u4e2d\u6883\u6599","BarCad":"https:\/\/wincc.oss-cn-qingdao.aliyuncs.com\/Wincc_luopusijin\/material\/profile\/image\/barCad_cf2449bc-3c24-4dde-8c55-30cfaa3ba145.png","BarName":"NC80\u4e2d\u6883\u6599","BarColor":"\u5916\u5496\u5561\/\u5185\u8910\u8272","BarCode":"NC8008","BarLength":"6000","BarThick":"0","BarCount":"1","BaDatum":2.6,"ResultId":"875984","BarWidth":60,"BarHeight":50,"Bar":[{"BarName":"\u5b50\u4ef6","BarNumber":1,"BarType":"A","BarWidth":0,"BarHeight":0,"BarMouth":0},{"BarName":"\u5b50\u4ef6","BarNumber":2,"BarType":"B","BarWidth":0,"BarHeight":0,"BarMouth":0},{"BarName":"\u9694\u70ed\u676124C","BarNumber":3,"BarType":"O","BarWidth":0,"BarHeight":0,"BarMouth":0}]},{"OrderId":"15426","DXFName":"\u5916\u5f00\u5185\u6247\u6599","BarCad":null,"BarName":"\u5916\u5f00\u5185\u6247\u6599","BarColor":"\u5916\u5496\u5561\/\u5185\u8910\u8272","BarCode":"OD8004","BarLength":"6000","BarThick":"0","BarCount":"2","BaDatum":2.6,"ResultId":"875985","BarWidth":45,"BarHeight":55,"Bar":[{"BarName":"\u5b50\u4ef6","BarNumber":1,"BarType":"B","BarWidth":0,"BarHeight":0,"BarMouth":0},{"BarName":"\u5b50\u4ef6","BarNumber":2,"BarType":"B","BarWidth":0,"BarHeight":0,"BarMouth":0}]}]}能生成二维码吗
05-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值