knightL

Yandex E

Oct 9th, 2021
1,790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <numeric>
  3. #include <algorithm>
  4. #include <set>
  5. #include <map>
  6. #include <queue>
  7. #include <cassert>
  8. #include <vector>
  9. #include <string>
  10. using namespace std;
  11.  
  12. typedef long long ll;
  13.  
  14. const int MAXN = 100;
  15. const int INF = 1000000000;
  16.  
  17. char buf[MAXN+1];
  18.  
  19. int a[MAXN][MAXN];
  20. int rowSum[MAXN][MAXN+1];
  21.  
  22. int dp[2][MAXN+1][MAXN*MAXN+1];
  23. int minDp[MAXN+1][MAXN*MAXN+1];
  24.  
  25. int main()
  26. {
  27.     int n,m;
  28.     scanf("%d%d",&n,&m);
  29.     for(int i=0;i<n;i++)
  30.     {
  31.         scanf("%s", buf);
  32.         for(int j=0;j<m;j++)
  33.             a[i][j] = buf[j]-'0';
  34.     }
  35.  
  36.     int total = 0;
  37.     for(int i=0;i<n;i++)
  38.     {
  39.         rowSum[i][0]=0;
  40.         for(int j=0;j<m;j++)
  41.             rowSum[i][j+1] = rowSum[i][j] + a[i][j];
  42.  
  43.         total += rowSum[i][m];
  44.     }
  45.  
  46.     int t=0;
  47.     for(int i=0;i<=m;i++)
  48.         for(int j=0;j<=total;j++)
  49.             dp[t][i][j] = INF;
  50.     dp[t][m][total] = 0;
  51.  
  52.     for(int k=n-1;k>=0;k--, t=1-t)
  53.     {
  54.         for(int i=0;i<=m;i++)
  55.             for(int j=0;j<=total;j++)
  56.                 dp[1-t][i][j] = INF;
  57.  
  58.         for(int j=0;j<=total;j++)
  59.         {
  60.             minDp[m][j] = dp[t][m][j];
  61.             for(int i=m-1;i>=0;i--)
  62.                 minDp[i][j] = min(minDp[i+1][j], dp[t][i][j]);
  63.         }
  64.  
  65.         for(int j=0;j<=total;j++)
  66.         {
  67.             for(int i=0;i<=m && i+j<=total;i++)
  68.             {
  69. //              dp[1-t][i][j] = INF;
  70. //              for(int c=i;c<=m;c++)
  71. //                  dp[1-t][i][j] = min(dp[1-t][i][j], dp[t][c][j+i] + rowSum[k][m-i]);
  72.                 dp[1-t][i][j] = min(INF, minDp[i][j+i] + rowSum[k][m-i]);
  73.             }
  74.         }
  75.     }
  76.  
  77.     int res = INF;
  78.     for(int i=0;i<=m;i++)
  79.         res = min(res, dp[t][i][0]);
  80.  
  81.     printf("%d\n", res);
  82.  
  83.     return 0;
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment