hdu 1081To The Max
生活随笔
收集整理的這篇文章主要介紹了
hdu 1081To The Max
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
http://acm.hdu.edu.cn/showproblem.php?pid=1081
題意:求子矩陣的和的最大值
思路:把多維轉(zhuǎn)化為一維,只要會(huì)一維的就簡單了。。。
To The Max
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4842????Accepted Submission(s): 2289
As an example, the maximal sub-rectangle of the array:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
is in the lower left corner:
9 2
-4 1
-1 8
and has a sum of 15.
?
Input The input consists of an N x N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N 2 integers separated by whitespace (spaces and newlines). These are the N 2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].?
Output Output the sum of the maximal sub-rectangle.?
Sample Input 4 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2?
Sample Output 15 View Code 1 #include<stdio.h> 2 #include<string.h> 3 #define Max(x,y) x>y?x:y 4 #define INF -0x7fffffff/2 5 int max_sub(int x[],int n) 6 { 7 int max=x[0]; 8 int sum=0; 9 int i; 10 for(i=0;i<n;i++) 11 { 12 sum+=x[i]; 13 if(sum<0) 14 { 15 sum=0; 16 } 17 if(max<sum) 18 { 19 max=sum; 20 } 21 } 22 23 return max; 24 } 25 int main() 26 { 27 int t; 28 int i,j,k; 29 int aa[105][105]; 30 int bb[105]; 31 int maxx; 32 33 while(~scanf("%d",&t)) 34 { 35 maxx=INF; 36 for(i=0;i<t;i++) 37 { 38 for(j=0;j<t;j++) 39 { 40 scanf("%d",&aa[i][j]); 41 } 42 } 43 44 for(i=0;i<t;i++) 45 { 46 memset(bb,0,sizeof(bb)); 47 for(j=i;j<t;j++) 48 { 49 for(k=0;k<t;k++) 50 { 51 bb[k]+=aa[j][k]; 52 } 53 54 maxx=maxx>max_sub(bb,t)?maxx:max_sub(bb,t); 55 } 56 } 57 printf("%d\n",maxx); 58 } 59 }?
轉(zhuǎn)載于:https://www.cnblogs.com/1114250779boke/archive/2012/08/05/2624312.html
總結(jié)
以上是生活随笔為你收集整理的hdu 1081To The Max的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu1728 广搜
- 下一篇: 构造数独 算法及代码实现