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的更多相关文章

  1. Longest Increasing Path in a Matrix

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  2. 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 ...

  3. ZOJ3578(Matrix)

    Matrix Time Limit: 10 Seconds      Memory Limit: 131072 KB A N*M coordinate plane ((0, 0)~(n, m)). I ...

  4. Java-螺旋方阵

    用Java实现螺旋方阵 螺旋方阵:是指呈螺旋状的矩阵. 具体实现如下: public void screwMatrix() { System.out.print("请输入数字:") ...

  5. lintcode:最大子正方形

    题目: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...

  6. PeopleRank从社交网络中发现个体价值

    阅读导读: 1.什么是PeopleRank? 2.PeopleRank和PageRank有什么差别? 3.PR分析微博数据时,怎样对微博单个账号评分? 4.R语言怎样递归计算矩阵特征值? 5.怎样计算 ...

  7. shell脚本学习(五)

    1.统计文件的行数.单词数.字符数 1)行数: wc -l file cat file | wc -l 2)单词数 wc -w file cat file | wc -w 3)统计字符数 wc -c ...

  8. python中的矩阵运算

    摘自:http://m.blog.csdn.net/blog/taxueguilai1992/46581861 python的numpy库提供矩阵运算的功能,因此我们在需要矩阵运算的时候,需要导入nu ...

  9. 84. Largest Rectangle in Histogram

    https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...

  10. 牛客练习赛24题解(搜索,DP)

    A题,C题不讲,基础题(但是我要抨击一下这次比赛,卡cin,cout,卡的太狠了,根本就不让过的那种,QAQ) 链接:https://www.nowcoder.com/acm/contest/157/ ...

随机推荐

  1. C#取中间文本

    /// <summary> /// 取中间文本 + static string GetMiddleStr(string oldStr,string preStr,string nextSt ...

  2. Hadoop之RPC

           Hadoop的RPC主要是通过Java的动态代理(Dynamic Proxy)与反射(Reflect)实现,代理类是由java.lang.reflect.Proxy类在运行期时根据接口, ...

  3. Java 垃圾回收机制

    1.delete是C++里面用于释放内存的运算符,而不是Java. 2.当发现某个对象的引用计数为0时,就将对象列入待回收列表中,并不是马上予以销毁. 3.System.gc()仅仅是一个回收请求,J ...

  4. POJ 3904 Sky Code

    题意:给定n个数ai, ai <= 10000, n <= 10000, 从中选出4个数要求gcd为1,这样的集合有多少个? 分析:首先总共集合nCr(n, 4) = n*(n-1)*(n ...

  5. 如何:在 Winform 动态启动、控制台命令行?

    需求   winForm 程序输出类型为 windows 程序(不是命令行程序)   在运行时想输入一些信息编译开发调试,如何实现这一功能 解答:  AllocConsole.FreeConsole ...

  6. POJ3083——Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...

  7. 【iOS开发】iOS7 兼容及部分细节

    1:statusBar字体为白色 在plist里面设置View controller-based status bar appearance 为 NO:设置statusBarStyle 为 UISta ...

  8. JavaScript Function.apply() 函数详解

    apply()函数用于调用当前函数functionObject,并可同时使用指定对象thisObj作为本次函数执行时函数内部的this指针引用. 该函数属于Function对象,所有主流浏览器均支持该 ...

  9. 编程概念--使用async和await的异步编程

    Asynchronous Programming with Async and Await You can avoid performance bottlenecks and enhance the ...

  10. Android-xUtils框架介绍(三)

    继续介绍xUtils的最后两个模块:DbUtils和HttpUtils.首先先介绍第一个SQLite数据库操纵的简单ORM框架,只要能理解xUtils为我们提供的api,相信你也能熟练的把DbUtil ...