Java Codes by Gamer
Tuesday, September 07, 2004
 
Topcoder Open 2004 - Qualification Round
Ah, I sucked completely in the arena. I couldn't even submit easiest problem on time. Reason, I panicked. In the situations like these one can really know his/her true nature. After the time-out it took me just few minutes to solve the entire riddle. It is damn easy. But then they say "If you lose confidence you lose everything". A learning at least.

Problem Statement

At a recent competition, each competitor received an integer score between 0 and 100, inclusive. Now, you need to rank the competitors from first to last. First place (the highest score) is ranked 1, second is ranked 2, and so on. In the event of a tie, all of the tied competitors should receive the average of the ranks they would have received if there were put in some order (it doesn't matter what order, the average is always the same). For example, if there is a tie between two people for first place, then ordering them would give one competitor rank 1, and the other rank 2. The average of these is 1.5, so both competitors receive a rank of 1.5. You should return a double[] containing the ranks of the competitors, each of whose elements corresponds to the element of score with the same index.







import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;

class Value
{
double rank;
int score;
}
public class Rank
{
public double[] rank(int[] scores)
{
int bscores[] = new int[ scores.length ];

for( int count = 0; count < bscores.length; count++ )
{
bscores[count] = scores[count];
}

Value ranks[] = new Value[ scores.length ];

Arrays.sort( scores );


int k = 1;
for( int i = scores.length - 1; i >= 0; i-- )
{
ranks[ k - 1 ] = new Value();
ranks[ k - 1 ].rank = k;


ranks[ k - 1 ].score = scores[ i ];
k++;


}


for( int i = 0; i < scores.length; i++ )
{
int count = 0;
int jth = 0;
for( int j = 0; j < scores.length; j++ )
{
if( ranks[ i ].score == ranks[ j ].score && i != j )
{
count++;
jth = j;
}
}

if( count >= 1 )
{
int start = jth - count;
int end = start + count;


double sum = 0;
for( ;start <= end; start++ )
{
sum += ranks[ start ].rank;
}

sum /= (count+1);

start = jth - count;
for( ;start <= end; start++ )
{
ranks[ start ].rank = sum;
}



i += count;
}
}


double res[] = new double[ scores.length ];

for( int i = 0; i < res.length; i++ )
{
res[ i ] = ranks[ i ].rank;
}

for( int i = 0; i < scores.length; i++ )
{
int score = bscores[i];
int count = 0;
int index = 0;
for( int j = 0; j < scores.length; j++ )
{
if( ranks[j].score == score )
{
res[ i ] = ranks[ j ].rank;
break;
}
}
}


return res;

}
}




Comments:
Do you know how to do high score ranking and countdown timer using Java? I need help in it. Thanks alot!!!!
 
I don't understand why you made your program so long - it could have been smaller... but I'm currently doing C++, not Java. So I wouldn't know.

The countdown timer sounds interesting... there should be something in the library files that should make your job easier... dunno

What arena are you talking about? Some sort of competition? I'm only 16, but I've seen my share of challenges. More info plz!
 
Hi! I've been reading your website for a long time now and finally got the bravery to go ahead and give you a shout out from Kingwood Texas! Just wanted to tell you keep up the great work!

Also visit my site ... egyption statues
 
Post a Comment

<< Home

Powered by Blogger