10107 - What is the Median?

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

Moderator: Board moderators

ei01036
New poster
Posts: 12
Joined: Wed Jan 15, 2003 1:13 am

10107 - What is the Median?

Post by ei01036 » Tue Feb 04, 2003 9:37 pm

Hi! I'm getting WA with this one and it seems preety simple! is there any tricky input or something? if you know something, please post!
thanks

Red Scorpion
Experienced poster
Posts: 192
Joined: Sat Nov 30, 2002 5:14 am

Post by Red Scorpion » Fri Feb 07, 2003 8:02 am

Be aware of Leading and Trailing Spaces in input !!

Here's sample input :
0
1
9999
8732
57
9
0
101
919
999999
29100010
100000000
90999
0
1
339992
1228
1
1
0

Output:
0
0
1
4366
57
33
9
33
57
79
101
510
919
510
101
510
919
510
101
79

Hope this helps.
GOOD LUCK
RED SCORPION

rumel_new
New poster
Posts: 3
Joined: Tue Feb 25, 2003 5:44 pm
Location: Dhaka

problem 10107

Post by rumel_new » Tue Feb 25, 2003 5:55 pm

what is the wrong of this problem?

Code: Select all

# include <stdio.h>
# include <math.h>
# include <ctype.h>
 void main(){
   long i,j,k,n,a[100],c=0;
   i=0;
   while(1==scanf("%ld",&a[i])){
	c++;
   for (k=0;k<c;k++)
      for (j=k+1;j<c;j++)
	if (a[i]<a[j]){
	  int t=a[i];
	  a[i]=a[j];
	  a[j]=t;
	 }
   long d=floor(c/2);
   if (c%2==1){
	printf ("%ld\t",a[d]);
	printf("\n");
   }
	if (c%2==0){
	     int e=(a[d]+a[d-1])/2;
	     printf ("%ld\t",e);
	     printf("\n");
	}
	i++;
   }
}
[/cpp]
We are SKARBAN.
We will make another world.

Hisoka
Experienced poster
Posts: 120
Joined: Wed Mar 05, 2003 10:40 am
Location: Indonesia

Post by Hisoka » Sat Mar 08, 2003 12:19 pm

you can test the sample I/O with sample from red scorpion. if you pass for that sample I/O, I think you not get WA, maybe you can get TLE or RE. RE because your array must be 10000 and your TLE because for sorting you use bubble sort. You only need 1 looping (1 for), because after input your data input have been sorted before. :)

User avatar
Pier
New poster
Posts: 38
Joined: Thu Mar 27, 2003 9:12 pm
Location: Aguascalientes, Mexico
Contact:

All test OK, but still WA!

Post by Pier » Tue May 27, 2003 6:54 pm

Any help appreciated!

[pascal] {$N+}
Var
m: extended;
i,j,t: longint;
n: array [1..10000] of longint;

Begin
i:= 1;
While (not eof(input)) do
begin
read(input,n);
for j:= i downto 2 do
if n[j-1]>n[j] then begin
t:= n[j];
n[j]:= n[j-1];
n[j-1]:= t;
end
else break;
if odd(i) then writeln(output,n[(i shr 1)+1])
else begin
m:= n[(i shr 1)+1];
m:= (m + n)/2;
writeln(output,trunc(m));
end;
Inc(i);
end;
End.[/pascal]
There are 10 kind of people on this world: those who understand binary and those who don't!

User avatar
Pier
New poster
Posts: 38
Joined: Thu Mar 27, 2003 9:12 pm
Location: Aguascalientes, Mexico
Contact:

Post by Pier » Sat Jun 28, 2003 4:15 am

I'll appreciate if anyone could help me with my code.

Thanks!
There are 10 kind of people on this world: those who understand binary and those who don't!

Trinity
New poster
Posts: 12
Joined: Tue Jun 10, 2003 3:40 pm

Post by Trinity » Mon Jun 30, 2003 2:56 am

I`m also having an WA problem...

What the hell means trailing spaces???

Is it something like 999 999 = 999999 ???

Thanx!

User avatar
Pier
New poster
Posts: 38
Joined: Thu Mar 27, 2003 9:12 pm
Location: Aguascalientes, Mexico
Contact:

Post by Pier » Mon Jun 30, 2003 5:31 am

I think "Leading and Trailing spaces" are before and after the number, not in between. But maybe there's where I'm wrong.
There are 10 kind of people on this world: those who understand binary and those who don't!

Trinity
New poster
Posts: 12
Joined: Tue Jun 10, 2003 3:40 pm

Post by Trinity » Tue Jul 01, 2003 5:40 pm

OK...

So that means that it could have more than one number at the same line ?!

Something like...

9999 9999 =

9999
9999

Or it has nothing to do with it and I'm just insane?!

Thanx
Trinity

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong

Post by Observer » Wed Jul 02, 2003 2:46 am

No...

Each line contains ONE number only!!!

By the way, Insertion Sort is quite eough for this qq.......
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

User avatar
Pier
New poster
Posts: 38
Joined: Thu Mar 27, 2003 9:12 pm
Location: Aguascalientes, Mexico
Contact:

Still WA!

Post by Pier » Tue Jul 22, 2003 6:40 am

I changed my bubble sort for insertion sort, but I still get WA... It's deprecing to get WA so many times on a easy problem...

[pascal]
{$N+}
Var
key: real;
i,j: longint;
n: array [1..10000] of real;

Begin
i:= 1;
While (not eof(input)) do
begin
read(input,n);

key:= n; j:= i-1;
While (j >=1) and (n[j] >key) do
begin
n[j+1]:= n[j];
Dec(j);
end;
n[j+1]:= key;

if odd(i) then writeln(output,round(n[(i shr 1)+1]))
else writeln(output,round((n[(i shr 1)+1] + n)/2));
Inc(i);
end;
End.
[/pascal]
There are 10 kind of people on this world: those who understand binary and those who don't!

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong

Post by Observer » Tue Jul 22, 2003 8:05 am

Pier, two obvious mistakes:

1. Why use round?! You should use trunc instead!

2. When reading the input, you should use readln instead of read. Otherwise, you might get Runtime Error!

I modified your code this way and got ACC!!! Try it! :D
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

User avatar
Pier
New poster
Posts: 38
Joined: Thu Mar 27, 2003 9:12 pm
Location: Aguascalientes, Mexico
Contact:

Post by Pier » Wed Jul 23, 2003 3:47 am

Hi!

Thanks a lot! I was using int:0:0 intead of round, but I changed it just to see if that helped and then forgot to change it back when I posted the question.

Also, I usually use readln instead of read but I don't understand why this makes WA! I thought they should work similar in this kind of problem.
There are 10 kind of people on this world: those who understand binary and those who don't!

lovemagic
Learning poster
Posts: 52
Joined: Thu Oct 02, 2003 11:38 am

10107 TLE

Post by lovemagic » Sat Oct 18, 2003 12:16 pm

why my program gets TLE?
#include <stdio.h>
#include <stdlib.h>

long input[10050];

int sort(const void *a,const void *b){
long p =*(long *)a,q =*(long *)b;
return p-q;
}

void main(){
long i=0;
while(scanf("%ld",&input)!=EOF){
i++;
qsort(input,i,sizeof(long),sort);
if(i%2==0)
printf("%ld\n",(input[i/2-1]+input[i/2])/2);
else
printf("%ld\n",input[(i-1)/2]);
}
}
khobaib

bery olivier
Learning poster
Posts: 90
Joined: Sat Feb 15, 2003 1:39 am
Location: Paris, France
Contact:

Post by bery olivier » Sun Oct 19, 2003 10:05 am

You do not have to sort your entire array for each input.
Not AC yet Image AC at last Image

Post Reply

Return to “Volume 101 (10100-10199)”