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/ ...
随机推荐
- FileFilter
FileFilter 下面的例子中我们创建了一个FileFilter类,此类根据文件名的扩展名是否为.png来筛选文件.创建FileFilter实例之后需要将此实例作为参数传给File的listFil ...
- Android Activity交互及App交互
Android交互--------->Intent Activity之间----->Explicit Intent App之间--------->Implicit Intent
- static 内部类
一般情况下是不可以用static修饰类的.如果一定要用static修饰类的话,通常static修饰的是匿名内部类. 在一个类中创建另外一个类,叫做成员内部类.这个成员内部类可以静态的(利用static ...
- WAF SSI
http://www.2cto.com/Article/201405/299154.html
- linux踢人命令 pkill踢人用法
首先使用who命令查看在线用户,然后踢人. 强制踢人命令格式:pkill -kill -t tty 解释: pkill -kill -t 踢人命令 tty 所踢用户的TTY或者pts/x(x代表数字) ...
- overrides final method getUnknownFields.()Lcom/google/protobuf/UnknownFieldSet 错误解决
使用java代码连接hbase服务器报错: java.lang.VerifyError: class org.apache.hadoop.hdfs.protocol.proto.ClientName ...
- ArcGIS学习记录-Excel和Txt中XY点数据生成点Shape文件方法
(一)Excel中XY点数据生成点Shape文件方法 1.Excel表如下: 2.点击ArcGIS中的"+"号按钮,添加数据.选择第一步中制作好的Excel文件,点击Add按钮 ...
- fiddler代理
对于前段开发人员,fiddler 是一个必不可少的调试神器.下载地址:http://www.telerik.com/download/fiddler. 有同学也许会碰到,chrome 下 fiddle ...
- Pomelo服务器琐碎方法
1.获取客户端ip地址:session__session__.__socket__remoteAddress.ip 2.日志文件无法打印到文件,删除node_modules/pomelo/node_m ...
- poj1151Atlantis(离散化+扫描线)
http://poj.org/problem?id=1151 http://www.cnblogs.com/kane0526/archive/2013/02/26/2934214.html这篇博客写的 ...