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 ...
随机推荐
- CF835 C 前缀和
100*100规模上第一象限坐标系上有1e5规模的点,每个点随时间在同一个值域内(最大10)周期递增,但初始值不同,给出一个矩阵和时间询问此时范围内点的值的和. 预处理初始时刻不同权值下的二维前缀和, ...
- Python学习笔记(十八)@property
# 请利用@property给一个Screen对象加上width和height属性, # 以及一个只读属性resolution: # -*- coding: utf-8 -*- class Scree ...
- 强大的jQuery网格插件 ParamQuery
ParamQuery是一种轻量级的jQuery网格插件,基于用于用户界面控制.具有一致API的优秀设计模式jQueryUI Widget factory创建,能够在网页上展示各种类似于Excel和Go ...
- 【BZOJ】3524: [Poi2014]Couriers
[算法]主席树 [题解]例题,记录和,数字出现超过一半就递归查找. 主席树见[算法]数据结构 #include<cstdio> #include<algorithm> #inc ...
- SDUT 3917
UMR 现在手里有 n 张康纳的表情,最上面一张是玛吉呀巴库乃.现在 UMR 如果每次把最上面的 m 张牌移到最下面而不改变他们的顺序及朝向,那么至少经过多少次移动玛吉呀巴库乃才会又出现在最上面呢? ...
- 2017ACM暑期多校联合训练 - Team 5 1001 HDU 6085 Rikka with Candies (模拟)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- HDU 1045 Fire Net (深搜)
题目链接 Problem DescriptionSuppose that we have a square city with straight streets. A map of a city is ...
- Masquerade strikes back Gym - 101911D(补题) 数学
https://vjudge.net/problem/Gym-101911D 具体思路: 对于每一个数,假设当前的数是10 分解 4次,首先 1 10 这是一对,然后下一次就记录 10 1,这样的话直 ...
- 蓝色的cms企业记账管理后台模板源码——后台
链接:http://pan.baidu.com/s/1bpxKGBP 密码:suda
- 深入理解Spring系列之三:BeanFactory解析
转载 https://mp.weixin.qq.com/s?__biz=MzI0NjUxNTY5Nw==&mid=2247483824&idx=1&sn=9b7c2603093 ...