ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1146. Maximum Sum

Teh Ming Han Why does my program not work? [5] // Problem 1146. Maximum Sum 26 May 2002 20:32
//I received an error: Crash (ACCESS_VIOLATION) from the judge

// USU Problem 1146
// Maximum Sum
// Done by Teh Ming Han

#include <iostream.h>
#include <fstream.h>

int main(){
   int x1,y1,x2,y2,n,c,max=0;
   int sum[100][100], work[100][100]={0};

   ifstream infile("maxsum.in");
   infile>>n;
   for (x1=1;x1<=n;x1++)
      for (y1=1;y1<=n;y1++)
         infile>>sum[x1][y1]; //read data
   infile.close();

   for (x1=1;x1<=n;x1++)
      for (y1=1;y1<=n;y1++)
         for (x2=1;x2<=x1;x2++)
            for (y2=1;y2<=y1;y2++)
               work[x1][y1] += sum[x2][y2];

   for (x1=1;x1<=n;x1++)
      for (y1=1;y1<=n;y1++)
         for (x2=x1;x2<=n;x2++)
            for (y2=y1;y2<=n;y2++){
               c = work[x2][y2] - work[x1-1][y2] - work[x2][y1-1] +
work[x1-1][y1-1];
               if (c>max) max=c;
            }

   ofstream outfile("maxsum.out");
   outfile<<max;
   outfile.close();
   return 0;
}
Nebula Re: Why does my program not work? // Problem 1146. Maximum Sum 16 Apr 2004 13:52
N may be as large as 100

lucky~
Vlad Veselov You'll get TLE. [2] // Problem 1146. Maximum Sum 16 Apr 2004 16:30
As I see your program is O(N^4) and it's to many operations when N = 100.
sloboz Re: You'll get TLE. [1] // Problem 1146. Maximum Sum 16 Apr 2004 20:22
your algo is good, theoretically is N^4 but practically fits in time.
First you must not use files, and second put 200 or 110 or something little larger than 100. You'll get AC (I sent your source with these modifications).
Ciprian Re: what's wrong // Problem 1146. Maximum Sum 20 May 2008 13:59
u use files.
try using scanf/cin
printf/cout
Lakers Re: Why does my program not work? // Problem 1146. Maximum Sum 18 Nov 2011 22:14
use
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
        b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1] + a[i][j];
    }
}