UVA10827 - Maximum sum on a torus

题目链接

题意:给出一个环形矩阵,也就是第一行和最后一行是相连的,第一列和最后一列是相连的,求最大的子矩阵的和

思路:只要将矩阵复制四个,那么就可以按照求一个矩阵内最大子矩阵之和的做法去做,即枚举所有子矩阵的和,更新最大值。要注意在转换后的大矩阵,枚举的子矩阵规格是有范围的。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 205;

int arr[MAXN][MAXN], sum[MAXN];
int n, Max;

int deal() {
    int temp;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            memset(sum, 0, sizeof(sum));
            for (int k = i; k < i + n; k++) {
                temp = 0;
                for (int l = j; l < j + n; l++) {
                    sum[l] += arr[k][l];
                    temp += sum[l];
                    if (Max < temp)
                        Max = temp;
                }
            }
        }
    }
    return Max;
}

int main() {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                scanf("%d", &arr[i][j]);
                arr[i][j + n] = arr[i + n][j] = arr[i + n][j + n] = arr[i][j];
            }
        }
        Max = -INF;
        int ans = deal();
        printf("%d\n", ans);
    }
    return 0;
}

UVA10827 - Maximum sum on a torus

时间: 2024-08-02 09:46:32

UVA10827 - Maximum sum on a torus的相关文章

UVA 10827 Maximum sum on a torus

算法入门经典训练指南88页练习 ::这道题只要把原矩阵扩大4倍,那么其跟最大子矩阵的题目就很类似,把二维转化成一维,求最大的序列和,不过这个序列的长度不能超过n. 长度不能超过n? 那这道题又跟hdu 3415 HDU 3415 Max Sum of Max-K-sub-sequence (单调队列) 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorit

UVA 10827 Maximum sum on a torus 最大矩阵和

题目链接:UVA - 10827 题意描述:给出一个n*n矩阵,把第一行和最后一行粘一起,把第一列和最后一列粘一起,形成一个环面,求出这个环面中最大的矩阵和. 算法分析:首先复制n*n这个矩阵,形成由4个这样小矩阵组成的大矩阵,然后在这个大矩阵里找出最大矩阵和,一看貌似和poj1050这道题有些相似,但是这道题数据大一些,O(150^4)应该会超时吧.我们可以这样想,先枚举这个最大和矩阵的左上角在大矩阵中的位置,然后枚举最大和矩阵的行数和列数,在枚举过程中处理最大和,然后更新答案即可. 1 #i

1481:Maximum sum

1481:Maximum sum 总时间限制:  1000ms 内存限制:  65536kB 描述 Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: t1 t2 d(A) = max{ ∑ai + ∑aj | 1 <= s1 <= t1 < s2 <= t2 <= n } i=s1 j=s2 Your task is to calculate d(A). 输入

maximum sum

uva,10684 1 #include <iostream> 2 #include <cstdio> 3 #define maxn 10005 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 int a[maxn]; 10 while(scanf("%d",&n),n) 11 { 12 for(int i=0;i<n;i++) 13 scanf("%d",&a[

sicily 1091 Maximum Sum (动规)

1 //1091.Maximum Sum 2 //b(i,j) = max{b(i,j-1)+a[j], max(b(i-1,t)+a[j])} (t<j) 3 #include <iostream> 4 using namespace std; 5 6 int main() { 7 int t; 8 cin>>t; 9 while (t--) { 10 int n; 11 cin>>n; 12 int a[n+1]; 13 for (int i = 1; i &

UVa 108 - Maximum Sum(最大连续子序列)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=44  Maximum Sum  Background A problem that is simple to solve in one dimension is often much more difficult to solve in more th

UVA108 - Maximum Sum(最大连续和)

题意:从一个n*n的矩阵中找出和最大的子矩阵 思路:最大连续和的求解.我们可以将二维的转化为一维进行计算.sum[i][j]表示以(1, 1)和(i, j)为对角的矩阵的和.之后只要逐个枚举每个可能出现的值,保存最大值即可. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x3

Round #169 (Div. 2)C. Little Girl and Maximum Sum

1.用退化的线段树(也就是没有区间查询)做... 2.注意longlong. #include<cstdio>#include<cstring> #include<iostream>#include<algorithm>using namespace std;int n,q;int a[200010],s[200010];int main(){    scanf("%d%d",&n,&q);for(int i=1;i<

Codeforces Round #169 (Div. 2)C. Little Girl and Maximum Sum

传送门 Description The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries,