## 410 - Station Balance

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

Moderator: Board moderators

Revenger
Experienced poster
Posts: 132
Joined: Sun Apr 14, 2002 12:27 pm
Location: Russia

### 410 Station Balance

Can anyone give me a test on which my program fails? It's get WA, but I think that it is correct.

[pascal]Program p410;

Const MaxS = 10;
MaxC = 5;

Var Inf : Array[1..MaxS]of Integer;
Cam : Array[1..MaxC]of Integer;
AM,IM : Extended;
C,S,i,j,t : Integer;
ic,jc,k,p : Integer;
ok : Boolean;

begin
t:=0;
While Not Eof(InPut) Do begin

Writeln('Set #',t);
for i:=1 to C do begin
Write(i:2,':');
if Inf[i*2-1]>0 then Write(' ',Inf[i*2-1]);
if Inf[i*2]>0 then Write(' ',Inf[i*2]);
Writeln;
end;
Writeln('IMBALANCE = ',IM:0:5);
end;
end.[/pascal]
Last edited by Revenger on Sat Jul 06, 2002 6:49 pm, edited 1 time in total.

xenon
Learning poster
Posts: 100
Joined: Fri May 24, 2002 10:35 am
Location: Scheveningen, Holland
The chambers are numbered 0,1,2,3 and 4, your prog outputs 1,2,3,4,5.
That's an obvious error. Haven't looked at the rest of your algo.

Revenger
Experienced poster
Posts: 132
Joined: Sun Apr 14, 2002 12:27 pm
Location: Russia
It still getting WA

[pascal]Program p410;

Const MaxS = 10;
MaxC = 5;

Var Inf : Array[1..MaxS]of Integer;
Cam : Array[1..MaxC]of Integer;
AM,IM : Extended;
C,S,i,j,t : Integer;
ic,jc,k,p : Integer;
ok : Boolean;

begin
t:=0;
While Not Eof(InPut) Do begin

Writeln('Set #',t);
for i:=1 to C do begin
Write((i-1):2,':');
if Inf[i*2-1]>0 then Write(' ',Inf[i*2-1]);
if Inf[i*2]>0 then Write(' ',Inf[i*2]);
Writeln;
end;
Writeln('IMBALANCE = ',IM:0:5);
end;
end.[/pascal]
Last edited by Revenger on Sat Jul 06, 2002 6:50 pm, edited 1 time in total.

xenon
Learning poster
Posts: 100
Joined: Fri May 24, 2002 10:35 am
Location: Scheveningen, Holland
I really think you should better check your output formatting against the description before you dump your code on the message board. But hey, that's only my opinion
The final line of output for each set should be a blank line.
I added one writeln statement to your code, and got it accepted. So have a try...

PS. Since the code you published is allmost the solution, and thus a big spoiler, I suggest you remove it. But still, it's only my opinion. No hard feelings, mate

Revenger
Experienced poster
Posts: 132
Joined: Sun Apr 14, 2002 12:27 pm
Location: Russia
Thank you very much!
Of cource, I will delete my code

Zuberul
New poster
Posts: 28
Joined: Sun Oct 24, 2004 9:46 pm
Location: dhaka
Contact:

### 410-station balance

I used greedy for this proble.
but got WA.
please give me some I/O.

roticv
Learning poster
Posts: 63
Joined: Sat Dec 11, 2004 9:28 am
Hello does anyone know why I keep getting WA for 410?

Code: Select all

``````#include <stdio.h>
#include <math.h>

int n,m,z,i,j,k,h,f,p,q,ave,total,sub;
int specimen[20],sorted[20],visited[20],visited2[20];
double imbalance;

int compare(const void *a, const void *b){
if (*(int*)a > *(int*)b)
return 1;
return -1;
}

int main(){
z = 0;
while (scanf("%d %d ",&m,&n)!=EOF){
z++;
ave = 0;
for (i=0;i<n;i++){
scanf("%d ",&specimen[i]);
sorted[i] = specimen[i];
visited[i] = 0; visited2[i] = 0;
ave += specimen[i];
}
sorted[i] = 0;
specimen[i] = 0;
p = m;
q = n;
if (n%2 == 1)
n++;
total = 0;
qsort(sorted,n,sizeof(int),compare);
printf("Set #%d\n",z);
for (i=0;i<m;i++){
printf("%2d:",i);
for (j=0;j<q;j++)
if (visited[j]!=1){
visited[j] = 1;
printf(" %d",specimen[j]);
for (k=0;k<n;k++)
if (sorted[k] == specimen[j] && visited2[k] != 1)
break;
if (sorted[n-k-1]!=0){
printf(" %d",sorted[n-k-1]);
}
visited2[k] = 1; visited2[n-k-1] = 1;
for (h=0;h<q;h++)
if (sorted[n-k-1] == specimen[h] && visited[h]!=1){
visited[h] = 1;
break;
}
sub = sorted[k] + sorted[n-k-1];
sub = sub*p - ave;
if (sub < 0)
sub = -sub;
total += sub;
break;
}
printf("\n");
}
imbalance = total;
imbalance /= p;
printf("IMBALANCE = %.5lf\n\n",imbalance);
}
return 0;
}

``````

kane116
New poster
Posts: 8
Joined: Sun Aug 07, 2005 5:35 am

### 410 - Station Balance

What's wrong with it? Help me please, thank you!

Code: Select all

``````#include <iostream>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int dat[10];
int sto[5][2];
int s, c, cot;
double AM, IM;
double CM[5];

int init()
{   for (int i = 0; i < 10; i++)
{   dat[i] = 0;
}

for (int i = 0; i < 5; i++)
{   CM[i] = 0;
for (int j = 0; j < 2; j++)
{   sto[i][j] = 0;
}
}

s = 0;
c = 0;
AM = 0;
IM = 0;
}

int inpt()
{   cin >> c >> s;
for (int i = 0; i < s; i++)
{   cin >> dat[i];
AM = AM + dat[i];
}
AM = AM / c;
}

int sorting()
{   int tmp;

for (int i = 0; i <= s; i++)
{   for (int j = 0; j <= s; j++)
{   if (dat[i] < dat[j])
{  tmp = dat[i];
dat[i] = dat[j];
dat[j] = tmp;
}
}
}
}

int storing()
{   for (int i = 0; i < c; i++)
{   sto[i][0] = dat[i];
CM[i] = CM[i] + dat[i];
}
for (int i = 0; i < c; i++)
{   sto[c - i - 1][1] = dat[c + i];
CM[c - i - 1] = CM[c - i - 1] + dat[c + i];
}
}

int outpt()
{   int tmp;
char str[10];
cout << "Set #" << cot << endl;
for (int i = 0; i < c; i++)
{   cout << " " << i << ": ";

for (int j = 0; j < 2; j++)
{   if (sto[i][j] != 0)
{  cout << sto[i][j];
}
if (sto[i][0] != 0)
{  cout << " ";
}
}
cout << endl;
}
printf("IMBLANCE = %.5f\n", IM);
cout << endl;
}

int cal()
{   for (int i = 0; i < c; i++)
{   IM = IM + fabs(CM[i] - AM);
}
}

int main()
{   cot = 0;
while (! cin.eof())
{   init();
cot++;
inpt();
sorting();
storing();
cal();
outpt();
}
}``````

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:
There are some problems in your code.

Code: Select all

``````int init()
{   for (int i = 0; i < 10; i++)
{   dat[i] = 0;
}

for (int i = 0; i < 5; i++)
{   CM[i] = 0;
for (int j = 0; j < 2; j++)
{   sto[i][j] = 0;
}
}

s = 0;
c = 0;
AM = 0;
IM = 0;
} ``````
You are declaring i as an integer in every 'for()' statement. Its bad programming practice. You should declare local variables after u declare the function. You should write...

Code: Select all

``````int init()
{
int i,j;
for (i = 0; i < 10; i++)
{   dat[i] = 0;
}

for (i = 0; i < 5; i++)
{   CM[i] = 0;
for (j = 0; j < 2; j++)
{   sto[i][j] = 0;
}
}

s = 0;
c = 0;
AM = 0;
IM = 0;
} ``````
You are getting wrong answer because u missed something. The problem states that
The first number (1<=C<=5) defines the number of chambers in the centrifuge and the second number (1<=S<=2C) defines the number of specimens in the input set.
So, S<C is correct. So there can be inputs like

Input:

Code: Select all

``````5 3
1 2 3``````
And output should be

Output:

Code: Select all

``````Set #1
0: 3
1: 2
2: 1
3:
4:
IMBALANCE = 5.20000``````
Hope it works.
Ami ekhono shopno dekhi...
HomePage

roticv
Learning poster
Posts: 63
Joined: Sat Dec 11, 2004 9:28 am
How come it is not

Set #1
0: 1 2
1: 3
2:
3:
4:
IMBALANCE = 3.60000

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:
Did you notice this....
CM
is the Chamber Mass of chamber i and is computed by summing the masses of the specimens assigned to chamber i.
AM
is the Average Mass of the chambers and is computed by dividing the sum of the masses of all specimens by the number of chambers (C).

Code: Select all

``````5 3
1 2 3
Now There are 5 chambers, 3 specimens. Total mass 1+2+3 = 6
And average = 6/5 = 1.20
So, AM = 1.20.``````
Now the output is

Code: Select all

``````Set #1
0: 3
1: 2
2: 1
3:
4:
IMBALANCE = 5.20000
Imbalance = |3 - AM | + |2 - AM | + |1 - AM | + |0 - AM | + |0 - AM |
= |3 - 1.2| + |2 - 1.2| + |1 - 1.2| + |0 - 1.2| + |0 - 1.2|
=    1.8    +    .8     +    .2     +    1.2    +   1.2
= 5.2
``````
But your output is

Code: Select all

``````Set #1
0: 1 2
1: 3
2:
3:
4:
IMBALANCE = 3.60000
Imbalance = |3 - AM | + |3 - AM | + |0 - AM | + |0 - AM | + |0 - AM |
= |3 - 1.2| + |3 - 1.2| + |0 - 1.2| + |0 - 1.2| + |0 - 1.2|
=    1.8    +    1.8    +   1.2     +    1.2    +   1.2
= 7.2
So, your output is wrong.
``````
Any other problems?
Ami ekhono shopno dekhi...
HomePage

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:
Ye, I have question

Why output should be:

Code: Select all

``````Set #1
0: 3
1: 2
2: 1
3:
4:
IMBALANCE = 5.20000
``````
and not

Code: Select all

``````Set #1
0: 1
1: 2
2: 3
3:
4:
IMBALANCE = 5.20000
``````
or any other ordering with the same imbalance ?

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)

roticv
Learning poster
Posts: 63
Joined: Sat Dec 11, 2004 9:28 am
Thanks alot. I just realised I made a stupid mistake and that is why I kept getting WA. Anyway, personally I think the order the chambers are printed is not important as it is not clearly stated and there is a special correction program.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:
To Dominik Michniewski, it is a special correction program. So, any ordering with the same IMBALANCE is correct.
Ami ekhono shopno dekhi...
HomePage

heckler
New poster
Posts: 1
Joined: Mon Sep 12, 2005 1:53 pm
Hey everyone

I