#1580 : Matrix

时间限制: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,然后再求一个最大子矩阵和。
【分析】考虑到普通求最大子矩阵的方法,先枚举上下行,然后对每一列求和再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)的更多相关文章

  1. [CSP-S模拟测试]:matrix(DP)

    题目描述 求出满足以下条件的$n\times m$的$01$矩阵个数:(1)第$i$行第$1~l_i$列恰好有$1$个$1$.(2)第$i$行第$r_i~m$列恰好有$1$个$1$.(3)每列至多有$ ...

  2. CSP模拟赛 Matrix(DP)

    题面 求出满足以下条件的 n*m 的 01 矩阵个数: (1)第 i 行第 1~li 列恰好有 1 个 1. (2)第 i 行第 ri~m 列恰好有 1 个 1. (3)每列至多有 1 个 1. n, ...

  3. matrix(dp)

    matrix Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Sub ...

  4. 牛客网多校训练第一场 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 ...

  5. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  6. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  7. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  8. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  9. Leetcode#867. Transpose Matrix(转置矩阵)

    题目描述 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:[[1 ...

随机推荐

  1. Excel 报表导入导出

    使用 Excel 进行报表的导入导出,首先下载相关的 jar 和 excel util. Excel Util 下载地址 引入依赖: <!-- poi office --> <dep ...

  2. STL在算法比赛中简单应用

    STL基础 和 简单的贪心问题 STL(Standard Template Library) 即 标准模板库. 它包含了诸多在计算机科学领域里所常用的基本数据结构和算法.这些数据结构可以与标准算法一起 ...

  3. SQL语句(二十二)—— 权限授予和回收(作业练习)

    CREATE TABLE course ( Cno ) NOT NULL, Cname ) DEFAULT NULL, Cpno ) DEFAULT NULL, Ccredit smallint DE ...

  4. [Luogu 1351] NOIP2014 联合权值

    [Luogu 1351] NOIP2014 联合权值 存图,对于每一个点 \(u\),遍历它的所有邻接点.以 \(u\) 为中转点的点对中,\((x,y)\) 的联合权值 \(w_x \cdot w_ ...

  5. ELK 企业内部日志分析系统

    生产环境配置 亿级规模,建议64G内存+8核CPU ES JVM占用一半内存 生产环境的3节点的集群 https://blog.csdn.net/xuduorui/article/details/79 ...

  6. 遍历hashmap

    转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: Map map = new HashM ...

  7. [php]http的状态码

    1.分类 100~199 表示成功接受请求,要求客户端继续提交下一次请求才能完成整个过程处理. 200~299 表示成功接收请求并已完成整个处理过程,常用200 300~399 为完成请求,客户需进一 ...

  8. ajax做显示信息以后用ajax、Bootstrp做弹窗显示信息详情

    1.用ajax做弹窗显示信息详情 nation.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...

  9. .net APIHelper client获取数据

    using Newtonsoft.Json; using System.Net.Http.Headers; public static class APIHepler { public static ...

  10. Mac 终端自动补全忽略大小写

    打开终端,输入:nano .inputrc 在里面粘贴上以下语句: set completion-ignore-case onset show-all-if-ambiguous onTAB: menu ...