uva10827-Maximum sum on a torus(矩阵最大和的变形)
题目;uva10827-Maximum sum on a torus(矩阵最大和的变形)
题目大意:就是uva108的变形,矩阵能够连通,就是能够从后面连到前面。这里把矩阵复制三遍,然后又一次生成一个大的矩阵,就能够解决联通的问题。再枚举矩阵的起点和终点全部情况,保留最大值就能够了。
比如:1 2 3
2 3 4
新的矩阵: 1 2 3 1 2 3
2 3 4 2 3 4
1 2 3 1 2 3
2 3 4 2 3 4
代码:
#include <stdio.h>
#include <string.h> const int N = 200;
const int INF = 0x3f3f3f3f;
int mat[N][N], temx[N], temy[N];
int n; int Max (const int x, const int y) {return x > y? x: y;} int main () { int t;
scanf ("%d", &t);
while (t--) { scanf ("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) { scanf ("%d", &mat[i][j]);
mat[i][j + n] = mat[i + n][j] = mat[i + n][j + n] = mat[i][j];
} int mm = -INF;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { memset (temx, 0, sizeof (temx));
for (int x = i; x < i + n; x++) {
for (int y = j; y < j + n; y++) { if (y == j)
temy[y] = mat[x][y];
else
temy[y] = temy[y - 1] + mat[x][y];
temx[y] += temy[y];
mm = Max (mm, temy[y]);
mm = Max (mm, temx[y]);
}
}
}
}
printf ("%d\n", mm);
}
return 0;
}
uva10827-Maximum sum on a torus(矩阵最大和的变形)的更多相关文章
- UVa 10827 - Maximum sum on a torus
题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...
- UVA 10827 Maximum sum on a torus 最大矩阵和
题目链接:UVA - 10827 题意描述:给出一个n*n矩阵,把第一行和最后一行粘一起,把第一列和最后一列粘一起,形成一个环面,求出这个环面中最大的矩阵和. 算法分析:首先复制n*n这个矩阵,形成由 ...
- UVA 10827 Maximum sum on a torus (LA)
算法入门经典训练指南88页练习 ::这道题只要把原矩阵扩大4倍,那么其跟最大子矩阵的题目就很类似,把二维转化成一维,求最大的序列和,不过这个序列的长度不能超过n. 长度不能超过n? 那这道题又跟hdu ...
- ural 1146. Maximum Sum
1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...
- UVa 108 - Maximum Sum(最大连续子序列)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- URAL 1146 Maximum Sum(最大子矩阵的和 DP)
Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...
- ural 1146. Maximum Sum(动态规划)
1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...
- POJ2479 Maximum sum[DP|最大子段和]
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39599 Accepted: 12370 Des ...
- 最大子矩阵和 URAL 1146 Maximum Sum
题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...
随机推荐
- 快速排序:升序+降序----java实现
快速排序思路:先把第一个元素令为low下标,最后一个为high下标.并把第一个元素令为temp来作为标准元素.以标准元素来调整数组,使比标准元素小的都在标准元素前,比标准元素大的都在标准元素后.这样一 ...
- [LeetCode55]Jump Game
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- [LeetCode235]Lowest Common Ancestor of a Binary Search Tree
题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in th ...
- android 处理器crash刊物
日志记录程序是为了方便各种异常情况,为了方便日后的维修方案进行维修,程序无法百分百健康,完美,有必要保存在日志中代码.易于维护.Java了一个接口UncaughtExceptionHandler,Th ...
- JFinal 的源代码超具体的分析DB+ActiveRecord
我记得有人告诉我."面试一下spring源代码.看ioc.aop源代码"那为什么要看这些开源框架的源代码呢,事实上非常多人都是"应急式"的去读.就像读一篇文章一 ...
- StackExchange.Redis 使用
StackExchange.Redis 使用 - 事件(五) 摘要: ConnectionMultiplexer 可以注册如下事件ConfigurationChanged- 配置更改时Configur ...
- 关闭 MsMpEng.exe
MsMpEng.exe是Windows Defender 自动保护服务的核心引擎. 系统是win8.1 最近发现MsMpEng.exe是任务管理器里面最占内存的一个程序,且无法强制结束程序.偶然发现一 ...
- Nginx搭建反向代理服务器过程详解(转)
一.反向代理 我们都知道,80端口是web服务的默认端口,其他主机访问web服务器也是默认和80端口进行web交互,而一台服务器也只有一个80端口,这是约定俗成的标准. 我们来看下面两个场景: 1.服 ...
- Windows 事件查看器(收集)
原文:Windows 事件查看器(收集) 事件查看器相当于一本厚厚的系统日志,可以查看关于硬件.软件和系统问题的信息,也可以监视 Windows 的安全事件 提示:除了可以在"控制面板→管理 ...
- DeviceIoControl的使用说明
应用程序和驱动程序的通信过程是:应用程序使用CreateFile函数打开设备,然后用DeviceIoControl与驱动程序进行通信,包含读和写两种操作.还能够用ReadFile读数据用WriteFi ...