hiho 1580 Matrix(dp 降维压缩)
来源:互联网 发布:js 修改style属性 编辑:程序博客网 时间:2024/06/13 06:27
#1580 : Matrix
- 样例输入
3 3 4-100 4 44 -10 44 4 43 3 -1-2 -2 -2-2 -2 -2-2 -2 -2
- 样例输出
24-1
描述
Once upon a time, there was a little dog YK. One day, he went to an antique shop and was impressed by a beautiful picture. YK loved it very much.
However, YK did not have money to buy it. He begged the shopkeeper whether he could have it without spending money.
Fortunately, the shopkeeper enjoyed puzzle game. So he drew a n × m matrix on the paper with integer value ai,j in each cell. He wanted to find 4 numbers x, y, x2, and y2(x ≤ x2, y ≤ y2), so that the sum of values in the sub-matrix from (x, y) to (x2, y2) would be the largest.
To make it more interesting, the shopkeeper ordered YK to change exactly one cell's value into P, then to solve the puzzle game. (That means, YK must change one cell's value into P.)
If YK could come up with the correct answer, the shopkeeper would give the picture to YK as a prize.
YK needed your help to find the maximum sum among all possible choices.
输入
There are multiple test cases.
The first line of each case contains three integers n, m and P. (1 ≤ n, m ≤ 300, -1000 ≤ P ≤ 1000).
Then next n lines, each line contains m integers, which means ai,j (-1000 ≤ ai,j ≤ 1000).
输出
For each test, you should output the maximum sum.
这题最大的坑点就是 必须修改一个数但是这个数却不一定出现在所选的矩阵中
(需要特别考虑得情况 当所选的矩阵是整个矩阵时 应该正逆各枚举一遍序列)
#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <bitset>#include <algorithm>#include <climits>using namespace std;const int N = 400;typedef long long LL;int n, m;LL p, sum[N][2], dp[N][4], a[N][N];// 0从1开始 1不从1开始 3最大的不选值LL get(int x, int y){ int flag=0; LL res0=-1000000000,res1=-1000000000,ans=-1000000000; for(int i=1;i<=m;i++) { if(i==1) { dp[i][0]=sum[i][0]; dp[i][1]=sum[i][1]; } else { if(dp[i-1][0]+sum[i][0]>sum[i][0]) dp[i][0]=dp[i-1][0]+sum[i][0]; else flag=1,dp[i][0]=sum[i][0]; if(dp[i-1][0]+sum[i][1]>sum[i][1]) dp[i][1]=dp[i-1][0]+sum[i][1]; else dp[i][1]=sum[i][1]; if(dp[i-1][1]+sum[i][0]>dp[i][1]) dp[i][1]=dp[i-1][1]+sum[i][0]; } res0=max(res0,dp[i][0]); res1=max(res1,dp[i][1]); } if(x!=1||y!=n) { ans=max(ans,res0); ans=max(ans,res1); } else { ans=max(ans,res1); if(res0!=dp[m][0]) ans=max(ans,res0); else if(flag) ans=max(ans,res0); else { res0=-10000000000; LL h=0; for(int i=1;i<m;i++) ans=max(ans,dp[i][0]); for(int i=m;i>1;i--) { if(i!=m) { if(h+sum[i][0]>sum[i][0]) h=h+sum[i][0]; else h=sum[i][0]; } else h=sum[i][0]; ans=max(ans,h); } } } return ans;}int main(){ while(scanf("%d %d %lld", &n, &m, &p)!=EOF) { for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { scanf("%lld", &a[i][j]); } } if(n==1&&m==1) { printf("%lld\n",p); continue; } LL flag=-1, ans, cnt; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) sum[j][0]=a[i][j],sum[j][1]=p; cnt=get(i,i); if(flag==-1||cnt>ans) ans=cnt; flag=1; for(int k=i+1; k<=n; k++) { for(int j=1; j<=m; j++) { sum[j][1]=max(sum[j][0]+p,sum[j][1]+a[k][j]); sum[j][0]+=a[k][j]; } cnt=get(i,k); if(cnt>ans) ans=cnt; } } printf("%lld\n",ans); } return 0;}
- hiho 1580 Matrix(dp 降维压缩)
- Hiho 1044 dp+状态压缩
- hiho 8 状态压缩dp
- Hiho 1044 状态压缩dp
- HIHO #1044 : 状态压缩·一(状态压缩dp)
- hiho 1270(dp)
- 【DP】HIHO 1078
- hiho 数位dp
- hiho 9 状态压缩二
- hiho一下 第172周《Matrix Sum》
- HiHo 1037 数字三角形 (dp)
- hiho 12 树的dp
- hihocoder 1580 Matrix [dp+枚举+rmq]
- Hihocoder 1580 Matrix【思维+Dp+RMQ】
- 状态压缩二 (hiho 1048)
- hiho一下第八周(状态压缩)
- hiho 1233 Boxes(状态压缩+BFS)
- HiHo #1044 : 状态压缩·一
- 日期操作
- 奇异值分解(SVD) --- 几何意义
- Leetcode5. Longest Palindromic Substring
- 转发 VS重定向
- 智能指针不一定能杜绝内存泄漏
- hiho 1580 Matrix(dp 降维压缩)
- shell笔记收藏
- Spring JDBC-事务方法嵌套调用解读
- js判断浏览器与版本号
- 基于rem的移动端布局注意事项
- 移动架构02_UML类图
- 【C++】 标准运算符
- 在一个千万级的数据库查寻中,如何提高查询效率?
- IntelliJ idea在Spring配置无错误时,装配对象出现 Could not autowire. no beans of "XXX" type found