原题:
A grid that wraps both horizontally and vertically is called a torus. Given a torus where each cell contains an integer, determine the sub-rectangle with the largest sum. The sum of a sub-rectangle is the sum of all the elements in that rectangle. The grid below shows
a torus where the maximum sub-rectangle has been shaded.

Input
The first line in the input contains the number of test cases (at most 18). Each case starts with an integer N (1 ≤ N ≤ 75) specifying the size of the torus (always square). Then follows N lines describing the torus, each line containing N integers between -100 and 100, inclusive.
Output
For each test case, output a line containing a single integer: the maximum sum of a sub-rectangle within the torus.
Sample Input
2
5
1 -1 0 0 -4
2 3 -2 -3 2
4 1 -1 5 0
3 -2 1 -3 2
-3 2 4 1 -4
3
1 2 3
4 5 6
7 8 9
Sample Output
15
45

中文:
同样是让你找最大子矩阵和,但是四边可以跨边相邻。
送一组数据
3
4 -3 6
-2 -4 -5
7 0 1

答案是18,也就是四个角

ac代码

#include <bits/stdc++.h>
using namespace std;
int t,n,table[400][400],sum[400];
int solve()
{int ans=INT_MIN;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){memset(sum,0,sizeof(sum));for(int k=i+1;k<=n+i;k++){int tmp=0;for(int s=j+1;s<=n+j;s++){sum[s]+=table[k][s];tmp+=sum[s];ans=max(tmp,ans);}}}}return ans;
}int main()
{ios::sync_with_stdio(false);cin>>t;while(t--){cin>>n;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){cin>>table[i][j];table[i+n][j]=table[i][j+n]=table[i+n][j+n]=table[i][j];}}int ans=solve();cout<<ans<<endl;}return 0;
}

tle代码

#include <bits/stdc++.h>
using namespace std;int table[202][202];
int sum[202][202];
int tmp[202];
int n;
int SubSeq(int a[])
{int ans=INT_MIN,tmp;for(int i=1;i<=n;i++)a[i+n]=a[i];for(int k=1;k<n;k++){tmp= a[k];int s = a[k];for(int i = k+1 ;i <= n+k-1 ;i++){if( s > 0)s += a[i];elses = a[i];tmp = max(tmp , s);}ans=max(ans,tmp);}return ans;
}int solve()
{int max1 = INT_MIN;int max2 = INT_MAX;for(int s=n*2;s>n;s--){memset(sum , 0 ,sizeof(sum));for(int i = n ;i >= 1; i--){for(int j = n ;j >= 1 ;j--)sum[i][j] = sum[i+1][j] + table[s-(n-i)][j];}for(int i = n ;i >= 1 ;i--){for(int j  = n+1 ;j > i ;j--){for(int k = 1 ;k <= n ;k++)tmp[k] = sum[i][k] - sum[j][k];ans = max(ans , SubSeq(tmp));}}}return ans;
}int main()
{ios::sync_with_stdio(false);int t;cin>>t;while(t--){cin>>n;for(int i = 1 ;i <= n ;i++){for(int j = 1 ;j <=n ;j++){cin>>table[i][j];table[i+n][j]=table[i][j];}}//    cout<<endl;cout<<solve()<<endl;}return 0;
}

思路:

可以跨边的最大子矩阵和,可以联想到环形的最大子段和。
求解环形的最大子段和的思路有两个
比如要求的数据是a[]={11,-3,7}
那么其中一种方案是把a扩展成两倍长度,变为{11,-3,7,11,-3,7},然后每次枚举长度不超过3的最大子段和,时间复杂度为o(n^2)
另外一种方法是找到a的最小子段和,然后用a的所有和减去最小子段和就是环形的最大子段和了,挺好理解。时间复杂为o(n)

那么对于这道题,是不是可以借鉴一下求解环形最大子段和的o(n)算法的思路呢,比如求跨边最大子矩阵和,可以先求出最小子矩阵和,然后再用矩阵的总和减去?可惜方法是错误的,如下图
蓝色圈是最小子矩阵,红色圈的和是该矩阵最大子矩阵的答案。

考虑最大子段和的分治方法,可不可以利用此思想把二维矩阵分割来计算?简单的想了一下,首先需要把这个矩阵扩展出4倍来,表示成对边都相邻的情况。然后限定取和的区域大小不超过n*n来运算,时间复杂度大概是n^3logn。不过实现起来貌似很复杂,而且仅仅是个思路而已。

最后也是最粗暴的方法,直接扩展出四倍来,枚举选择区域大小不超过n*n,然后直接计算和取最大值。

uva 10827 Maximum sum on a torus相关推荐

  1. [UVA 10827] Maximum sum on a torus

    图片加载可能有点慢,请跳过题面先看题解,谢谢 这个题..没什么好讲的.. 把矩阵复制,4个拼到一起,二维前缀和+O(n^4) 枚举就过了..但是这个范围.. 不知道给的什么水数据.. 辣鸡题目.. $ ...

  2. URAL 1146 Maximum Sum(最大子矩阵的和 DP)

    Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...

  3. NOIP前夕:noi.openjudge,Maximum sum

    Maximum sum 总Time Limit: 1000msMemory Limit: 65536kB Description Given a set of n integers: A={a1, a ...

  4. 简单英文题 16 Maximum Sum Not Exceeding K(python)

    http://noi.openjudge.cn/english/16/ """ 简单英文题 16 Maximum Sum Not Exceeding K(AC) http ...

  5. [Swift]LeetCode1031. 两个非重叠子数组的最大和 | Maximum Sum of Two Non-Overlapping Subarrays...

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  6. D28:Maximum sum(最大和,翻译)

    原题:OpenJudge - 1481:Maximum sum 翻译: 描述:给定一组n个整数:A={a1,a2,...,an},我们定义函数d(A)如下: t1 t2 d(A) = max{ ∑ai ...

  7. 英语翻译 Maximum sum

    OpenJudge - 1481:Maximum sum 2022 2.10 Given a set of n integers: A={a1, a2,..., an}, we define a fu ...

  8. SPOJ KGSS Maximum Sum (线段树)

    SPOJ KGSS Maximum Sum 题意:求区间最大值与第二大值之和 思路:线段树维护两个最大值 /********************************************** ...

  9. Maximum Sum UVA - 108(连续子序列最大和—变形之子矩阵最大和)

    题目大意:给出 n*n 的矩阵,找每隔数字之和最大的子矩阵,输出最大和.  解题思路:枚举矩阵左上和右下的坐标,分别合并子矩阵的每列,使得二维转化为一维,然后利用连续子序列最大和去做就行. Time ...

  10. [Swift]LeetCode1043. 分隔数组以得到最大和 | Partition Array for Maximum Sum

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

最新文章

  1. php 二维数组排序函数,php自定义二维数组排序函数array
  2. 我以前一直没有真正理解支持向量机,直到我画了一张图!
  3. matlab 用fplot和plot作出函数图像
  4. 神策沙龙回顾:大数据技术和金融、房产、理财的深度结合
  5. golang基本数据类型默认值
  6. Tomcat(三):日志
  7. 成对抗网络代码全解析, 详细代码解析(TensorFlow, numpy, matplotlib, scipy)
  8. Skyfree退休公告
  9. springboot 银联支付(扫码支付)
  10. 专转本-数学考试大纲
  11. 辗转相除法——求最大公约数(易懂详解)
  12. 关于QQ的相关代码收集整理
  13. Windows10 - 使用命令行批量修改文件后缀名
  14. 我的世界显示服务器领地指令,我的世界服务器领地指令有哪些 指令详细介绍...
  15. 2016年10月9日 星期日 --出埃及记 Exodus 18:20
  16. 大炮打蚊子(c语言易懂版)
  17. 上课笔记--台大政治学基础之美国选举制度与意识形态
  18. 北理计算机学院2004年机试真题
  19. 掌握JavaScript
  20. Qt笔记(二十)之实现窗口定时关闭

热门文章

  1. D. Bouncing Boomerangs
  2. python入门教程陈孟林_Python基础教程学习路线
  3. inline-block元素高度为0,父级仍被撑起,问题元凶—strut
  4. PHP中冒号加引号,冒号的五种用法 冒号引号的三种用法
  5. React - 函数作为子组件
  6. 【jpa】简介和项目生成、API-初级入门
  7. 趣味点名软件_网传川大教授用刷脸软件点名 无人逃课
  8. mysql graler_安装Linux后常用的操作以及踩坑记录
  9. HALCON学习论坛
  10. 现代计算机网络的前沿技术,现代计算机网络的前沿技术分析