591 - Box of Bricks

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

Moderator: Board moderators

Maarten
Experienced poster
Posts: 108
Joined: Sat Sep 27, 2003 5:24 pm

Post by Maarten »

Did you notice this:
Output a blank line after each set.
orca
New poster
Posts: 4
Joined: Mon Dec 15, 2003 6:48 am

591

Post by orca »

:-?
help me!
I dont understand "Output a blank line after each set"
I tried
printf("Set #%d\nThe minimum number of moves is %d.\n\n",i+1,min);
but it didnt work

i cant find the bugs!

#include<stdio.h>
int main(){
int n[50][50],min=0,j,k,i=0,sum=0;
scanf("%d",&n[0]);
while(n[0]!=0){
for(j=1;j<=n[0];j++){
scanf("%d",&n[j]);
}
i++;
scanf("%d",&n[0]);
}
i=0;
while(n[0]!=0){
min=sum=0;
for(j=1;j<=n[0];j++){
sum+=n[j];
}
sum/=n[0];
for(j=1;j<=n[0];j++){
if(n[i][j]-sum>0)
min+=n[i][j]-sum;
}
printf("Set #%d\nThe minimum number of moves is %d.\n\n",i+1,min);
i++;
}
return 0;
}
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel »

Hi Orca,
Your output format is perfectly alright.

Your mistake is on the declaration of the array size. The problems says
1<=n<=50 and you declared the array as n[50][50]. You are trying to access n[][50] in your code but you can not do that. Change to n[50][51]
and you will get AC.

By the way instead of storing all the inputs, consider it one by one. That is take one set of input, process it, output the answer, and take the next set and so on. In this way it will take less memory and the coding becomes much more efficient.

Hope it helps.
:wink:
orca
New poster
Posts: 4
Joined: Mon Dec 15, 2003 6:48 am

Post by orca »

:D
thx a lot


i stored all of the input because the sample input of #591
is
6
5 2 4 1 7 5
0

if i do it one by one the input/output will be
6
5 2 4 1 7 5
Set #1
The minimum number of moves is 5.
....0

so i stored all of them and my input/output will be

6
5 2 4 1 7 5
0

Set #1
The minimum number of moves is 5.
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel »

Congrats! 8)

But the judge will not see the input order. It will just write the output in anoter file and then compare it with that of the judge's. So it doesn't matter at which part of your code you write the ouput.
Bye.
:wink:
orca
New poster
Posts: 4
Joined: Mon Dec 15, 2003 6:48 am

Post by orca »

:P

thx for telling me that
linh
New poster
Posts: 1
Joined: Sun Mar 28, 2004 9:00 am

591 wrong question?

Post by linh »

hi. i am a bit confused on the problem. seems like the problem is asking what is the subtration or additions one have to do. But from what I understand, shouldn't it be only 3 moves?

5 2 4 1 7 5

1 2 3 4 5 6 <col>
avg is 4 so
column moving
7 -> 4 (3blks)
1 -> 2 (1blk)
6 -> 2 (1blk)

3 moves.

Is this problem is kinda like binpacking problem?
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel »

One move is equivalent to the displacement of one block.

So, 7->4 is 3 blocks which is actually three moves.

Hope it helps. :wink:
vadiu
New poster
Posts: 10
Joined: Mon Jul 19, 2004 6:35 pm

591 - Box of Bricks

Post by vadiu »

This problem seems to be easy but i don't really know why I get WA when I submit it. I have read all the posts about this problem, but I can't still understand what's wrong in this code. Can someone help me?

[c]#include "stdio.h"


int x, i, heights[51], z, number=1, media, moves=0;

int calc();

int main()
{

for(x=1; ; x++)
{
moves= 0;

scanf("%d", &number);
if(number==0)
return 0;

for(i=0; i<number; i++)
scanf("%d", &heights);

media = calc();

for(z=0; z<i; z++)
if(heights[z]-media>0)
moves+=heights[z]-media;

printf("Set #%d\nThe minimum number of moves is %d.\n\n",x,moves);

}

return 0;
}

int calc()
{
int a, b=0;
for(a=0; a<=i; a++)
b+=heights[a];

return b/i;
}[/c]

Thanks...
abishek
Experienced poster
Posts: 131
Joined: Mon Dec 15, 2003 5:41 am

Post by abishek »

simple mistake i think
for(a=0; a<=i; a++)
b+=heights[a[;
that is a<i i think.
vadiu
New poster
Posts: 10
Joined: Mon Jul 19, 2004 6:35 pm

Post by vadiu »

Thanks a lot. I got AC. :D
Wei
New poster
Posts: 23
Joined: Sat Jul 24, 2004 5:37 pm
Contact:

591...I got an Accepted P.E.

Post by Wei »

I got an Accepted P.E...But I didn't know why~~~
Can someone tell me???
[c]
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,i,b,sum,c,min,k;
scanf("%d",&a);
k=1;
while (a!=0)
{
int num[a],ans[a];
num[a]=0;
sum=0;
min = 0;
for (i=1;i<=a;i++)
{
scanf("%d",&b);
num=b;
sum = sum+num;
}
c = sum/a;
for (i=1;i<=a;i++)
{
ans=abs(num-c);
min=min+ans;
}
printf("Set #%d\n",k);
printf("The minimum number of moves is %d.\n\n",min/2);
k++;
scanf("%d",&a);
}
}[/c]
59557RC
New poster
Posts: 26
Joined: Sun Mar 20, 2005 9:28 pm
Location: bangladesh
Contact:

591:why-PE

Post by 59557RC »

can anyone pls tell me why i got PE on 591:

#include<stdio.h>
int main(void)
{


long b[50]; long c,i,avg,dif;long sum=0;long total=0;long s=0;

scanf("%ld",&c);
while(c!=0){ sum=0;
total=0;

for(i=0;i<c;i++){ scanf("%ld",&b);

sum=sum+b; }
avg= sum / c;
for(i=0;i<c;i++){ if (b > avg) {dif=b-avg; total=total+dif;
} }
s++;
printf("Set #%ld\n",s);
printf("The minimum number of moves is %ld.\n\n",total);
scanf("%ld",&c);

}
return 0;
}
aaa
mohiul alam prince
Experienced poster
Posts: 120
Joined: Sat Nov 01, 2003 6:16 am
Location: Dhaka (EWU)

Post by mohiul alam prince »

Hi
i have submited ur program and got AC without PE.
this is a ok program which u have posted for problem 591

MAP
kckckc
New poster
Posts: 4
Joined: Thu Feb 10, 2005 1:52 pm

591-Box of Bricks (Can you tell me why I get Compile ERROR)?

Post by kckckc »

//591-Box of Bricks
//belows is my code
//Can you tell me why I get Compile ERROR ?
//Thanks

#include<stdio.h>
int main(void)
{
int set = 0,n,i,all,box[50],avg,move[100]={0};

while(scanf("%d",&n)==1)
{

all=avg=0;

if(n==0)break;
set++;

for(i=1;i<=n;i++)
{
scanf("%d",&box[i-1]);
all=all+box[i-1];
}

avg = all/n;

for(i=1;i<=n;i++)
{
if(box[i-1]>avg)
move[set-1] = move[set-1] + box[i-1] - avg;
}
}

for(i=1;i<=set;i++)
{
printf("Set #%d\n",i);
printf("The minimum number of moves is %d.\n\n",move[i-1]);
}
return 0;
}
Post Reply

Return to “Volume 5 (500-599)”