Max Sub-matrix
Max Sub-matrix
教练找的题目,目前样列过了
题意:找子矩阵的最大周长
思路:先离散每列,再枚举列(n*n),在当前枚举的两列之间求每行的和(n*n*n),但是开两个数组,一个包含两列上的元素 一个不包含,这样可以处理出前i行当前这两列上的元素和。 当前两列中每行元素和知道 两列上前i项和知道就可以找出最大值
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <list>
#include <map>
#include <stack>
#include <vector>
#include <cstring>
#include <sstream>
#include <string>
#include <cmath>
#include <queue>
#include <stdlib.h>
#include <conio.h>
#include <bits/stdc++.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
const int N=;
const int MOD = 1e9+;
#define LL long long
void fre() {
freopen("in.txt","r",stdin);
} inline int r() {
int x=,f=;char ch=getchar();
while(ch>''||ch<'') {if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<='') { x=x*+ch-'';ch=getchar();}return x*f;
}
int T,R ,s; struct node{
int x,y,w;
node(){}
node(int xx,int yy,int ww):x(xx),y(yy),w(ww){}
bool operator <(const node &a)const{
return x<a.x;
}
}p[]; int c;
LL sum;
int col[];
LL l[],row[],row1[];
void init(){
R=r(),s=r();
clc(col,);
clc(l,);
clc(row,);
clc(row1,);
c=;
sum=;
for(int i=;i<R;i++){
for(int j=;j<s;j++){
int x;
x=r();
if(x){
sum+=x;
p[c]=node(i,j,x);
col[c++]=j;
}
}
}
} LL work(){
sort(p,p+c);
sort(col,col+c);
int n=unique(col,col+c)-col;
if(n<=) return sum;
LL ans=;
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){
int cmin=col[i],cmax=col[j];
int cnt=;
for(int k=;k<c;k++){
if(k==||p[k].x!=p[k-].x){
cnt++;
row[cnt]=row1[cnt]=;
l[cnt] = (cnt==)?:l[cnt-]+row1[cnt-]-row[cnt-];
}
int cnow=p[k].y;
int cost=p[k].w;
if(cnow>cmin&&cnow<cmax) row[cnt]+=cost;
if(cnow>=cmin&&cnow<=cmax) row1[cnt]+=cost;
}
// for(int i=1;i<=cnt;i++){
// printf("%d\n",l[i]);
// }
// cout<<endl;
// getch();
if(cnt<=) return sum;
LL maxn=;
for(int k=;k<=cnt;k++){
ans=max(ans,l[k]+row1[k]+maxn);
maxn=max(maxn,row[k]-l[k]);
}
}
}
return ans;
} int main(){
// fre();
T=r();
while(T--){
init();
LL ans=work();
printf("%lld\n",ans);
}
return ;
}
Max Sub-matrix的更多相关文章
- Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- leetcode@ [329] Longest Increasing Path in a Matrix (DFS + 记忆化搜索)
https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, find the ...
- ZOJ3578(Matrix)
Matrix Time Limit: 10 Seconds Memory Limit: 131072 KB A N*M coordinate plane ((0, 0)~(n, m)). I ...
- Java-螺旋方阵
用Java实现螺旋方阵 螺旋方阵:是指呈螺旋状的矩阵. 具体实现如下: public void screwMatrix() { System.out.print("请输入数字:") ...
- lintcode:最大子正方形
题目: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- PeopleRank从社交网络中发现个体价值
阅读导读: 1.什么是PeopleRank? 2.PeopleRank和PageRank有什么差别? 3.PR分析微博数据时,怎样对微博单个账号评分? 4.R语言怎样递归计算矩阵特征值? 5.怎样计算 ...
- shell脚本学习(五)
1.统计文件的行数.单词数.字符数 1)行数: wc -l file cat file | wc -l 2)单词数 wc -w file cat file | wc -w 3)统计字符数 wc -c ...
- python中的矩阵运算
摘自:http://m.blog.csdn.net/blog/taxueguilai1992/46581861 python的numpy库提供矩阵运算的功能,因此我们在需要矩阵运算的时候,需要导入nu ...
- 84. Largest Rectangle in Histogram
https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...
- 牛客练习赛24题解(搜索,DP)
A题,C题不讲,基础题(但是我要抨击一下这次比赛,卡cin,cout,卡的太狠了,根本就不让过的那种,QAQ) 链接:https://www.nowcoder.com/acm/contest/157/ ...
随机推荐
- Otto Product Classification Winner's Interview: 2nd place, Alexander Guschin ¯\_(ツ)_/¯
Otto Product Classification Winner's Interview: 2nd place, Alexander Guschin ¯\_(ツ)_/¯ The Otto Grou ...
- 看几道JQuery试题后总结(下篇)
感谢圆友的提醒 昨天下午完成了9道试题中的前4道,之后好多园友存在些疑惑和建议,在这里我一并说一下吧.首先对于昨天第一题可能存在误导,在JQuery中并没有innerHTML这个属性,不过我们可以将J ...
- python List&Set&Dict交集、并集、差集
1.python List交集.并集.差集 1). 获取两个list 的交集#方法一: a=[2,3,4,5] b=[2,5,8] tmp = [val for val in a if val in ...
- Spring mvc 模式小结
http://www.taobaotesting.com/blogs/2375 1.spring mvc简介 Spring MVC框架是一个MVC框架,通过实现Model-View-Controlle ...
- PRMonitor,学习SSDT HOOK的好例子,学习的内核HOOK的好例子(注意右边有一堆类似的例子)
http://download.csdn.net/detail/bolong5240/1060645
- 核心思想:早胜过一切,张小龙的Foxmail居然可以卖1200万
现在谁都可以做一个类似的软件,但是市场已经成熟了,满大街都是,也就没有人会来收购你的软件了.
- Permutation Test 置换检验(转)
Permutation Test 置换检验 显著性检验通常可以告诉我们一个观测值是否是有效的,例如检测两组样本均值差异的假设检验可以告诉我们这两组样本的均值是否相等(或者那个均值更大).我们在实验中经 ...
- ajax返回son数据
JSON 只是一种文本字符串.它被存储在 responseText 属性中 为了读取存储在 responseText 属性中的 JSON 数据,需要根据 JavaScript 的 eval 语句. 函 ...
- XMPP聊天客户端环境搭建
1.服务器选择:ejabberd,具体安装过程请参考:http://blog.csdn.net/linhanmin/article/details/9876819 2.客户端配置: 采用xmppfra ...
- xp主机用VMware9和10安装Ubuntu12.04后无法进入图像界面
xp主机用VMware9和10安装Ubuntu12.04后无法进入图像界面 备注:虚拟机安装Ubuntu12.04 64位版本 刚开始我用VMware-workstation-full-8.0.3来安 ...