codeforces 225C Barcode

矩阵操作与三维动态规划算法
本文探讨了一种解决特定矩阵问题的算法,利用三维动态规划实现最优解,重点介绍了状态转移方程与边界条件的设定。

题意:给你一个矩阵,只包含 '#' 和 '.',现在每一列必须相同,最多连续y列最少连续x列相同。

解题思路:3维dp,dp[i][j][k] 表示 第i列 ,状态j,是状态j的第K行。

解题代码:

  1 // File Name: 225c.cpp
  2 // Author: darkdream
  3 // Created Time: 2015年03月09日 星期一 09时49分05秒
  4 #include<climits>
  5 #include<vector>
  6 #include<list>
  7 #include<map>
  8 #include<set>
  9 #include<deque>
 10 #include<stack>
 11 #include<bitset>
 12 #include<algorithm>
 13 #include<functional>
 14 #include<numeric>
 15 #include<utility>
 16 #include<sstream>
 17 #include<iostream>
 18 #include<iomanip>
 19 #include<cstdio>
 20 #include<cmath>
 21 #include<cstdlib>
 22 #include<cstring>
 23 #include<ctime>
 24 #define LL long long
 25 using namespace std;
 26 char str[1005][1005];
 27 int dp[1005][2][1005];
 28 int cw[1005];
 29 int cb[1005];
 30 int main(){
 31   int n , m ,x ,y;
 32   memset(dp,-1,sizeof(dp)) ;
 33   scanf("%d %d %d %d",&n,&m,&x,&y);
 34   for(int i = 1;i <= n;i ++)
 35   {
 36      scanf("%s",&str[i][1]);
 37   }
 38   for(int i = 1;i <= m;i ++)
 39   {
 40     for(int j = 1;j <= n;j ++)
 41     {
 42       if(str[j][i] == '#')
 43           cw[i] ++ ;
 44     }
 45     cb[i] = n - cw[i];
 46   }
 47   dp[1][0][1] = cb[1];
 48   dp[1][1][1] = cw[1];
 49   for(int i= 2;i <= m;i ++)
 50   {
 51      int mxcb = -1;
 52      for(int j = 1;j <= y ;j ++)
 53      {
 54           if(dp[i-1][0][j] != -1)
 55           {
 56             dp[i][0][j+1] = dp[i-1][0][j] + cb[i];
 57             if(j >= x)
 58             {
 59              if(mxcb == -1)
 60                  mxcb = dp[i-1][0][j];
 61              else 
 62                  mxcb = min(mxcb,dp[i-1][0][j]);
 63             }
 64           }
 65      }
 66      int mxcw = -1; 
 67      for(int j = 1;j <= y ;j ++)
 68      {
 69           if(dp[i-1][1][j] != -1)
 70           {
 71             dp[i][1][j+1] = dp[i-1][1][j] + cw[i];
 72             if(j >= x)
 73             {
 74              if(mxcw == -1)
 75                  mxcw = dp[i-1][1][j];
 76              else 
 77                  mxcw = min(mxcw,dp[i-1][1][j]);
 78             }
 79           }
 80      }
 81      //printf("%d %d\n",mxcw,mxcb);
 82      if(mxcw != -1)
 83          dp[i][0][1] = mxcw + cb[i];
 84      if(mxcb != -1)
 85          dp[i][1][1] = mxcb + cw[i];
 86   }
 87   int ans = INT_MAX; 
 88   for(int i = 0 ;i < 2;i ++)
 89       for(int j = x; j <= y; j ++)
 90       {
 91         if(dp[m][i][j] != -1)
 92           ans = min(dp[m][i][j],ans);
 93       }
 94   /*for(int i = 1;i <= m;i ++)
 95   {
 96    for(int s = 0 ; s <= 1 ; s ++)
 97    {
 98     for(int j= 1;j <= y ;j ++)
 99         printf("%d ",dp[i][s][j]);
100      printf("****");
101    }
102     printf("\n");
103   }*/
104   printf("%d\n",ans);
105 return 0;
106 }
View Code

 

转载于:https://www.cnblogs.com/zyue/p/4323206.html

### Codeforces Problem 1332C Explanation The provided references pertain specifically to problem 742B on Codeforces rather than problem 1332C. For an accurate understanding and solution approach for problem 1332C, it's essential to refer directly to its description and constraints. However, based on general knowledge regarding competitive programming problems found on platforms like Codeforces: Problem 1332C typically involves algorithmic challenges that require efficient data structures or algorithms such as dynamic programming, graph theory, greedy algorithms, etc., depending upon the specific nature of the task described within this particular question[^6]. To provide a detailed explanation or demonstration concerning **Codeforces problem 1332C**, one would need direct access to the exact statement associated with this challenge since different tasks demand tailored strategies addressing their unique requirements. For obtaining precise details about problem 1332C including any sample inputs/outputs along with explanations or solutions, visiting the official Codeforces website and navigating to contest number 1332 followed by examining section C is recommended. ```python # Example pseudo-code structure often seen in solving competitive coding questions. def solve_problem_1332C(input_data): # Placeholder function body; actual logic depends heavily on the specifics of problem 1332C. processed_result = process_input(input_data) final_answer = compute_solution(processed_result) return final_answer input_example = "Example Input" print(solve_problem_1332C(input_example)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值