108 - Maximum Sum

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

new_tim
New poster
Posts: 4
Joined: Fri Sep 27, 2002 8:13 pm

prob 108 - WA

Post by new_tim »

I always got the wrong answer...
why??

[c]
#include <stdio.h>

void main()
{
int size;
int sum;
int max;
int A[101][101];
int B[101][101];

while(scanf("%d\n", &size) == 1)
{
sum = 0;
for(int i = 0; i <= 100; i++)
{
for(int j = 0; j <= 100; j++)
{
A[j] = 0;
B[j] = 0;
}
}
if(size == 0)
max = 0;
else {

for(int m = 1; m <= size; m++)
{
for(int n = 1; n <= size; n++)
{
scanf("%d", &A[m][n]);
if(m == 1 && n == 1)
max = A[m][n];
// set the initial max to the first number in the array
else if(max < A[m][n])
max = A[m][n];
// otherwise, check if each number in 1X1 square
}
}

for(int t1 = 1; t1 <= size; t1++)
{
for(int t2 = 1; t2 <= size; t2++)
{
for(int t3 = 1; t3 <= t1; t3++)
{
for(int t4 = 1; t4 <= t2; t4++)
{
B[t1][t2] = B[t1][t2]+A[t3][t4];
}
}
}
}

for(int a1 = 1; a1 <= size; a1++)
{
for(int a2 = 1; a2 <= size; a2++)
{
for(int a3 = a1; a3 <= size; a3++)
{
for(int a4 = a2; a4 <= size; a4++)
{
sum = B[a3][a4] - B[a3][a2-1] - B[a1-1][a4] + B[a1-1][a2-1];
if(max < sum)
max = sum;
}
}
}
}
printf("%d\n", max);
}
}
}
[/c]
Ilham Kurnia
New poster
Posts: 31
Joined: Sat Nov 17, 2001 2:00 am
Contact:

108 -- algorithm faster than O(N^3)???

Post by Ilham Kurnia »

Hi,

I'm wondering is there any algorithm faster than O(N^3) to solve 108??
I've heard rumours that O(N^2) exists, but I can't confirm it.

Cheers, :)

Ilham
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

I don't think , that exists algorithm which solve this question in O(N^2) time ... this is opinion my and people from my university ... ;-) but maybe we missed something ;-)

Cheers
Dominik
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

I don't think , that exists algorithm which solve this question in O(N^2) time ... this is opinion my and people from my university ... ;-) but maybe we missed something ;-)

Cheers
Dominik
mag
New poster
Posts: 4
Joined: Wed Oct 16, 2002 6:22 am
Location: Novosibirsk
Contact:

108 - multiple input???

Post by mag »

Is there multiple input in 108?

I've seen some solutions on this board using multiple input and some solutions that doesn't use multiple input....
wbr, Mikhail A Gusarov aka MAG
mailto:gusarov@ccfit.nsu.ru
eric30
New poster
Posts: 1
Joined: Thu Nov 14, 2002 12:01 pm

108 Compile Error

Post by eric30 »

Hello,

I'm so confused with that I have got so many times "Compile Error".
I hope that someone could help me find where's my problem.
Followings are my Error Message(problem 108):

/usr/lib/crt1.o: In function `_start':
/usr/lib/crt1.o(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
Balon
New poster
Posts: 8
Joined: Tue Nov 26, 2002 6:00 am

Post by Balon »

The input consists of an NxN array of integers
so, there is only one case in one input, not multiple.
Boffin
New poster
Posts: 2
Joined: Mon Sep 30, 2002 8:50 pm
Location: Israel
Contact:

108. Presentation Error

Post by Boffin »

Hello, All.
I posted problem 108 and it was accepted.
But I got a letter with:

"Warning: Your program would get a Presentation Error in a true contest.
The 24-hours judge interpretes it as an "Accepted" problem."

I print my answer:
[c]printf("%d",maxSum);[/c]
What's wrong?
Should I print
[c]printf("%d\n",maxSum);[/c]
and why?

What "Presentation Error" means?
passwd
New poster
Posts: 14
Joined: Thu Dec 05, 2002 11:20 pm

P 108 - Time Limit Exceeded

Post by passwd »

hi,

I'm getting Time Limit Exceeded, and I don't know why ...
If someone knows, please, advice !

Code: Select all

[c]
#include <stdio.h>
/*#define DEBUG*/

int main() {

  int n;
  int i,j;

  scanf("%d", &n);

  {
     int v[n][n];
     int k=0,m=0,maxsum=-1,aux=0,p,q;
     if(n==0) { printf("0\n"); exit(0); };
	 for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&v[i][j]);

     if(n==1) {
       printf("%d\n",v[0][0]);
       return 0;
     }

     #ifdef DEBUG
         printf("\n");
    	 for(i=0;i<n;i++) {
		   for(j=0;j<n;j++) printf("%d  ",v[i][j]);
		   printf("\n");
    	 }
         printf("\n");
	 #endif

     for(i=0;i<n-1;i++) {
       for(j=i+1;j<n;j++) {
         for(k=0;k<n-1;k++) {
           for(m=k+1;m<n;m++) {
             /*calcula a soma do retangulo*/
             for(p=i;p<=j;p++) {
               for(q=k;q<=m;q++) aux+=v[p][q];
             }
             if(aux>maxsum) maxsum=aux;
             aux=0;
           }
         }
       }
     }

     printf("%d\n",maxsum);
  }

return 0;
}
[/c]
thanks !!
Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry »

Use Dynamic Programming
nghiank
New poster
Posts: 31
Joined: Wed Nov 20, 2002 3:10 pm
Contact:

Post by nghiank »

test
Last edited by nghiank on Sun Apr 22, 2007 3:40 pm, edited 1 time in total.
JTK1996
New poster
Posts: 4
Joined: Thu Nov 21, 2002 3:10 pm

Post by JTK1996 »

Hi,

The innermost for-loop is run 10^6 times, I think that is too much. Dynamic Programming, as mentioned before, sounds like a good
way to reduce time consumption.
junjieliang
Experienced poster
Posts: 169
Joined: Wed Oct 31, 2001 2:00 am
Location: Singapore

Post by junjieliang »

If you know how to solve the 1-d problem in O(n) time, without DP you can get O(n^4) algorithm, which (although not the best) will pass the time limit.
peterlin
New poster
Posts: 5
Joined: Wed Dec 18, 2002 4:46 pm

Post by peterlin »

Excuse me,
May I ask what's the algo is O(n^3) for this problem ?

Best wishes,
Peter
peterlin
New poster
Posts: 5
Joined: Wed Dec 18, 2002 4:46 pm

Post by peterlin »

Excuse me,
May I ask what's the algo is O(n^3) for this problem ?

Best wishes,
Peter
Post Reply

Return to “Volume 1 (100-199)”