HDU 1081 To The Max

转换成最大子序列和

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <cstring>
 5 
 6 #define MAX 200
 7 using namespace std;
 8 
 9 int main(){
10     int n;
11     while(cin >>n){
12         int arr[MAX][MAX]={0};
13         for(int i = 1; i<= n; i ++ ){
14             for(int j = 1; j <= n; j ++ ){
15                 cin>>arr[i][j];
16                 arr[i][j] += arr[i-1][j];
17             }
18         }
19         int maxSum =-1<<30 ;
20         for(int i = 0; i < n; i ++ ){
21             for(int j = i+1; j <= n; j ++){
22                 int thisSum = 0;
23                 for(int k = 1; k <= n; k ++){
24                     thisSum += arr[j][k] - arr[i][k];
25                     if(maxSum < thisSum) maxSum = thisSum;
26                     else if(thisSum < 0) thisSum = 0;
27                 }
28             }
29         }
30         cout<<maxSum<<endl;
31     }
32 }

 

posted @ 2013-04-10 16:48  OpenSoucre  阅读(127)  评论(0编辑  收藏  举报