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 ...
随机推荐
- 51nod 1629 B君的圆锥
1629 B君的圆锥 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 B君要用一个表面积为S的圆锥将白山云包起来. B君希望包住的白山云体积尽量 ...
- Codeforces 807 B T-Shirt Hunt
B. T-Shirt Hunt http://codeforces.com/problemset/problem/807/B time limit per test 2 seconds memory ...
- React-music 全家桶项目
React-Music 全家桶项目 一.简介 该项目是基于React全家桶开发的一个音乐播放器,技术栈采用:Webpack + React + React-redux + React-router + ...
- macbook 安装任意来源
sudo spctl --master-disable Comand+r Csrutil disable Reboot
- git创建新分支推送到远程
1.创建本地分支 git branch 分支名,例如:git branch 2.0.1.20120806 注:2.0.1.20120806是分支名称,可以随便定义. 2.切换本地分支 git ch ...
- arch点击硬盘无法挂载
出现问题如下 在使用xfce4桌面的时候在点击硬盘图标时可以挂载虽然要求你输入root密码 但是在使用openbox的时候点击硬盘图标却出现如下提示,权限的问题 Not authorized to p ...
- http 之cookie和session
cookie和session 关于http: 1.http是:无状态.短连接 2.http的请求生命周期:给服务端发送一个请起头,通过域名提取url,通过路由关系匹配,再通过函数+html进行模板加 ...
- Lempel-Ziv algorithm realization
Lempel-Ziv 复杂度程序 随着人们对非线性方法的分析越加深入,他们发现,虽然关联维度和最大李雅谱诺夫指数在分析脑电时具有一定的帮助,但是它们对数据的依赖性太强,对干扰和噪 声太敏感,而且要得到 ...
- Mysql自带profiling性能分析工具使用分享
1. show variables like '%profiling%';(查看profiling信息) 2. set profiling=1;(开启profiling) 3. 执行S ...
- pandas安装及使用
一. 安装pandas1. Anaconda 安装pandas.Python和SciPy最简单的方式是用Anaconda.Anaconda是关于Python数据分析和科学计算的分发包.2 ...