2017北京网络赛 hihocoder 1580 Matrix (dp很好的思维题)
来源:互联网 发布:基于单片机电子秤 编辑:程序博客网 时间:2024/04/27 16:41
时间限制:1000ms
单点时限:1000ms
内存限制:256MB
描述
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.
样例输入
3 3 4
-100 4 4
4 -10 4
4 4 4
3 3 -1
-2 -2 -2
-2 -2 -2
-2 -2 -2
样例输出
24
-1
大致题意:给你一个矩阵,你必须将其中的某个值改变成p,求最大子矩阵和
思路:膜一发dalao的题解http://blog.csdn.net/luricheng/article/details/78074046
我的理解:对于一般的求最大子矩阵的和,我们一般是枚举上下界O(n^2),然后再在O(n)的时间内求出最大连续子段和。然后对于这题,我们需要修改某个值变成p,那么在求最大连续子段这过程中我们可以加个二维dp,dp[i][0]表示此时以第i列结尾未改变值时的最大连续子段和,dp[i][1]表示此时以第i列结尾改变了值p的最大连续子段和。假设sum[i]为此时固定上下行后第i列所有值的和,minval[i]为固定上下行后第i列中最小的那个元素的值,那么转移方程即为
dp[i][0]=max(do[i-1][0],0)+sum[i];
dp[i][1]=max(dp[i-1][1]+sum[i],max(do[i-1][0],0)+sum[i]-minval[i]+p);
最后我们还需要考虑一下特殊情况,就是当我们所选的范围是整个矩形时,我们还需找到其中最小的值改成p
代码如下
#include<bits/stdc++.h> using namespace std; const int N=305;const int inf=-1e9-7;const int INF=1e9+7;int dp[N][2];int mat[N][N];int sum[N];int minval[N];int n,m,p;int solve1(){ int ans=inf; dp[0][0]=sum[0]; dp[0][1]=sum[0]-minval[0]+p; ans=max(dp[0][0],dp[0][1]); for(int i=1;i<m;i++) { dp[i][0]=max(dp[i-1][0],0)+sum[i]; dp[i][1]=max(dp[i-1][1]+sum[i],max(dp[i-1][0],0)+sum[i]-minval[i]+p); ans=max(ans,max(dp[i][0],dp[i][1])); } return ans;}int solve2()//特判下,因为此时上下界为0到n-1,可能会选取整个矩形{ int ans=inf; for(int i=0;i<m;i++) { int summ=0; int minn=INF; for(int j=i;j<m;j++) { summ+=sum[j]; minn=min(minn,minval[j]); if(i==0&&j==m-1) ans=max(ans,summ-minn+p); else ans=max(ans,max(summ,summ-minn+p)); } } return ans;}int main(){ while(scanf("%d%d%d",&n,&m,&p)!=EOF) { for(int i=0;i<n;i++) for(int j=0;j<m;j++) scanf("%d",&mat[i][j]); int ans=inf; for(int i=0;i<n;i++)//枚举行的上界 { for(int f=0;f<=m;f++)//初始化 { sum[f]=0; minval[f]=INF; } for(int j=i;j<n;j++)//枚举行下界 { for(int k=0;k<m;k++) { sum[k]+=mat[j][k]; minval[k]=min(minval[k],mat[j][k]); } if(i==0&&j==n-1) ans=max(ans,solve2()); else ans=max(ans,solve1()); } } printf("%d\n",ans); } return 0; }
- 2017北京网络赛 hihocoder 1580 Matrix (dp很好的思维题)
- 2017北京网络赛hihocoder 1580 matrix(dp)
- 2017北京网络赛 Hihocoder 1580 Matrix DP
- 2017北京网赛 hihocoder #1580 : Matrix 【DP】
- Hihocoder 1580 Matrix【思维+Dp+RMQ】
- 2017 ACM-ICPC北京网络赛: C. Matrix(DP)
- 2017 ACM-ICPC 亚洲区(北京赛区)网络赛C.Matrix (DP)
- hihocoder 1636(2017北京区域赛J)(区间dp)
- hihocoder 1580 Matrix dp+RMQ (带修改的最大子阵和)
- hihocoder 1232 || 2015北京网络赛F题 树连剖分
- hihoCoder 1227 & 2015 北京网络赛 A题
- hihocoder 1388 2016北京网络赛1006 Periodic Signal(卡精度的FFT)
- hihocoder 1388 2016北京网络赛1006 Periodic Signal(卡精度的FFT)
- hihocoder 1580 Matrix [dp+枚举+rmq]
- hihocoder 1582 : Territorial Dispute (计算几何 凸包)(2017 北京网络赛E)
- hihocoder-1586---Minimum(2017-icpc-网络赛-北京赛区)(线段树)
- 2017北京网络赛 && hihocoder 1582 Territorial Dispute(凸包)
- hihoCoder 北京网络赛 #1234 : Fractal
- C语言学习之记一道dfs例题
- 压测工具siege
- Matlab R2012a/b反复激活无效+license checkout failed解决方案
- Oracle 查询表/关联表中不重复的记录
- 跟着某乎玩python(一)
- 2017北京网络赛 hihocoder 1580 Matrix (dp很好的思维题)
- Java加解密与数字签名
- 跟着某乎玩python(二)
- 操作系统实验之磁盘调度
- 【Mybatis】通过自动回复机器人学Mybatis---基础版(4下)
- [Ubuntu] 修改键位 — ctrl 和 caps lock 互换
- scanf和gets的用法和区别
- SSH三大框架的工作原理及流程
- 命令sar,nlode监控系统的网卡流量