hihocoder #1580 : Matrix (DP)
#1580 : Matrix
描述
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,然后再求一个最大子矩阵和。
【分析】考虑到普通求最大子矩阵的方法,先枚举上下行,然后对每一列求和再dp,如果我们要通过修改某个值
来获得更大的ans时,那么换掉的肯定是这个子矩阵中的最小值,那么我们枚举上下行时,dp[i][0/1]表示以第i
列结尾的矩阵1:已经修改过了/0:还没修改 获得的最大子矩阵值,然后分情况DP就行了。有一种情况就是当你的
最大值是取完所有矩阵而且不修改值时,当前最大值 不与ans更新,因为题目要求必须修改。#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 3e2+;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,t,cas;
int a[N][N],m,p,sum[N];
int mn[N],dp[N][],len[N];
bool flag;
int solve(){
dp[][]=dp[][]=;
int ret=-inf;len[]=;
for(int i=;i<=m;i++){
if(dp[i-][]>){
dp[i][]=dp[i-][]+sum[i];
len[i]=len[i-]+;
}
else {
dp[i][]=sum[i];
len[i]=;
}
if(i==){
dp[i][]=sum[]-mn[]+p;
}
else {
dp[i][]=max(sum[i]-mn[i]+p,dp[i-][]+sum[i]-mn[i]+p);
dp[i][]=max(dp[i][],dp[i-][]+sum[i]);
}
ret=max(ret,dp[i][]);
if(flag&&len[i]==m)continue;
ret=max(ret,dp[i][]);
}
return ret;
}
int main(){
while(~scanf("%d%d%d",&n,&m,&p)){
for(int i = ; i <= n; ++i){
for(int j = ; j <= m; ++j){
scanf("%d", &a[i][j]);
}
}
int ans = -inf;
for(int i = ; i <= n; ++i){
met(sum,);met(mn,inf);flag=false;
for(int j = i; j <=n; ++j){
for(int k = ; k <= m; ++k){
sum[k]+=a[j][k];
mn[k]=min(mn[k],a[j][k]);
}
if(i==&&j==n)flag=true;
ans=max(ans,solve());
}
}
printf("%d\n",ans);
}
return ;
}
hihocoder #1580 : Matrix (DP)的更多相关文章
- [CSP-S模拟测试]:matrix(DP)
题目描述 求出满足以下条件的$n\times m$的$01$矩阵个数:(1)第$i$行第$1~l_i$列恰好有$1$个$1$.(2)第$i$行第$r_i~m$列恰好有$1$个$1$.(3)每列至多有$ ...
- CSP模拟赛 Matrix(DP)
题面 求出满足以下条件的 n*m 的 01 矩阵个数: (1)第 i 行第 1~li 列恰好有 1 个 1. (2)第 i 行第 ri~m 列恰好有 1 个 1. (3)每列至多有 1 个 1. n, ...
- matrix(dp)
matrix Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Sub ...
- 牛客网多校训练第一场 B - Symmetric Matrix(dp)
链接: https://www.nowcoder.com/acm/contest/139/B 题意: 求满足以下条件的n*n矩阵A的数量模m:A(i,j) ∈ {0,1,2}, 1≤i,j≤n.A(i ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- Leetcode#867. Transpose Matrix(转置矩阵)
题目描述 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:[[1 ...
随机推荐
- C11工具类:字符转换
1.数值类型和字符串转换 1.1 数值转换为字符 std::string to_string(int value); std::string to_string(long value); std::s ...
- [php]apache的权限解释
格式如下: <Directory d:/...> Order allow,deny Allow from all Allow from 127.0.0.1 Deny from 110.0. ...
- mysql查询日期相关的
今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ...
- NYOJ 35 表达式求值 (字符串处理)
题目链接 描述 ACM队的mdd想做一个计算器,但是,他要做的不仅仅是一计算一个A+B的计算器,他想实现随便输入一个表达式都能求出它的值的计算器,现在请你帮助他来实现这个计算器吧. 比如输入:&quo ...
- windows下面安装Python和pip教程
第一步,先来安装Python.windows下面的Python安装一般是通过软件安装包安装而不是命令行,所以首先要在Python的官方主页上面下载最新的Python安装包.下载地址是:https:// ...
- hdu 5319 Painter(杭电多校赛第三场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5319 Painter Time Limit: 2000/1000 MS (Java/Others) ...
- pycharm显示行号
在PyCharm 里,显示行号有两种办法: 1,临时设置.右键单击行号处,选择 Show Line Numbers. 但是这种方法,只对一个文件有效,并且,重启PyCharm 后消失. 2,永久设置. ...
- Ubuntu10.04 下安装RabbitVCS
安装RabbitVCS的方法步骤如下: 1.sudo add-apt-repository ppa:rabbitvcs/ppa #将rabbitvcs的添加到源里面.(次操作会提示是否要添 ...
- 64_n2
nodejs-from-0.1.3-4.fc26.noarch.rpm 11-Feb-2017 15:01 9982 nodejs-from2-2.1.0-6.fc26.noarch.rpm 11-F ...
- ACM International Collegiate Programming Contest World Finals 2014
ACM International Collegiate Programming Contest World Finals 2014 A - Baggage 题目描述:有\(2n\)个字符摆在编号为\ ...