uva 10827 - Maximum sum on a torus(dp)

题目链接http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1768

Problem H
Maximum sum on a torus
Input:
Standard Input

Output: Standard Output

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.

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

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 Output for 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
15

45


Problem setter: Jimmy Mårdell

Special Thanks: Derek Kisman, Md. Kamruzzaman

 
 
经典的最大连续和问题的小变形,从一串数变成矩阵,且行列首尾相连;
对付矩阵,降维,将多行累加到一行;
首尾相连,采用一般的复制延长数列的方法,但是注意要维护一个已选数列的长度,
当长度大于n时,要重新选择最佳策略,可以采用暴力搜索,代码如下
 
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;

int ori[160][160];
int sum[160][160];
int n;

int Dp(int cnt){
    int dp[160];
    int Max = ori[0][0];
    //计算从第1行到第n行所有行的最大dp值
    for(int i = 0;i < n;i++){
        //进行累加合并
        for(int j = 0;j < 2*n - 1;j++){
            sum[i][j] = sum[i][j] + ori[i+cnt][j];
        }
        int len = 1;
        dp[0] = sum[i][0];
        int tem_Max = dp[0];
        //把该行dp一遍
        for(int j = 1;j < 2*n - 1;j++){
            if(dp[j-1] <= 0){
                len = 1;
                dp[j] = sum[i][j];
            }
            else if(len == n){
                int pos = j;
                int pos_max = sum[i][j];
                int pos_sum = 0;
                for(int k = j;k > j-n;k--){
                    pos_sum += sum[i][k];
                    if(pos_sum > pos_max){
                        pos = k;
                        pos_max = pos_sum;
                    }
                }
                len = j - pos + 1;
                dp[j] = pos_max;
            }
            else{
                dp[j] = dp[j-1] + sum[i][j];
                len++;
            }
            tem_Max = max(tem_Max,dp[j]);
        }
        Max = max(tem_Max,Max);
    }
    return Max;
}

int main(){
    int t;
    int i,j;
    int ans;

    cin >> t;
    while(t--){
        memset(sum,0,sizeof(sum));
        ans = 0;
        cin >> n;
        for(i = 0;i < n;i++){
            for(j  = 0;j < n;j++){
                cin >> ori[i][j];
                ori[i+n][j] = ori[i][j];
                ori[i][j+n] = ori[i][j];
                ori[i+n][j+n] = ori[i][j];
            }
        }
        for(i = 0;i < n;i++){
            ans = max(ans,Dp(i));
        }
        cout << ans << endl;
    }

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值