Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
用动态规划,就是n^3In Reply To:奈何感觉算法复杂度为O(n^4) -。- Posted by:seu_enic at 2009-02-27 16:24:34 #include <iostream> using namespace std; short MaxSum(short *colSum, int n) { int maxSum = 0; int b=0; int i=0; for (i; i<=n-1; i++) { if (b>0) { b+=colSum[i]; } else { b = colSum[i]; } if (b > maxSum) { maxSum = b; } } return maxSum; } int main() { const short N = 100; short matrix[N][N]; short colSum[N]={0}; int i,j,k; int n; cin>>n; for (i=0; i<=n-1; i++) { for (j=0; j<=n-1; j++) { cin>>matrix[i][j]; } } int maxSubSum = 0; for (i=0; i<=n-1; i++) { for (k=0; k<=n-1; k++) { colSum[k] = 0; } for (j=i; j<=n-1; j++) { int maxTemp; for (k=0; k<=n-1; k++) { colSum[k] += matrix[j][k]; } maxTemp = MaxSum(colSum, n); if (maxTemp > maxSubSum) { maxSubSum = maxTemp; } } } cout<<maxSubSum<<endl; return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator