iCow [Jeffrey Wang, 2008]
Fatigued by the endless toils of farming, Farmer John has decided to
try his hand in the MP3 player market with the new iCow. It is an MP3
player that stores N songs (1 <= N <= 1,000) indexed 1 through N
that plays songs in a "shuffled" order, as determined by Farmer John's
own algorithm:
* Each song i has an initial rating Ri (1 <= Ri <=
10,000).
* The next song to be played is always the one with the
highest rating (or, if two or more are tied, the highest rated song
with the lowest index is chosen).
* After being played, a song's rating is set to zero, and
its rating points are distributed evenly among the other N-1 songs.
* If the rating points cannot be distributed evenly (i.e.,
they are not divisible by N-1), then the extra points are parceled out
one at a time to the first songs on the list (i.e., R_1, R_2, etc. --
but not the played song) until no more extra points remain.
This process is repeated with the new ratings after
the next song is played.
Determine the first T songs (1 <= T <= 1000)
that are played by the iCow.
INPUT FORMAT:
* Line 1: Two space-separated integers: N and T
* Lines 2..N+1: Line i+1 contains a single integer: Ri
SAMPLE INPUT (file icow.in):
3 4
10
8
11
INPUT DETAILS:
The iCow contains 3 songs, with ratings 10, 8, and 11, respectively.
You must determine the first 4 songs to be played.
OUTPUT FORMAT:
* Lines 1..T: Line i contains a single integer that is the i-th
song that the iCow
plays.
SAMPLE OUTPUT (file icow.out):
3
1
2
3
OUTPUT DETAILS:
The ratings before each song played are:
R_1 R_2 R_3
10 8 11 ->
play #3 11/2 = 5, leftover = 1
16 13 0 ->
play #1 16/2 = 8
0 21 8
-> play #2 21/2 = 10, leftover = 1
11 0 18 ->
play #3 ...
This program is a "pure programming" task. It requires nothing more
than the implementation of a set of rules. The example even showed how
one of the potentially troublesome rules actually worked.
The program below is commented to show how each step is performed. The
only potentially tricky part is the use of two variables for the
distribution: one to count how many pieces get distributed and the
second to say where they go. The second variable is the trick for
implementing the "Don't redistribute back to the currently playing
song" rule.
#include <stdio.h>
#include <stdlib.h>
void main() {
int bestrate,
bestj, i, j, k, n, t, r[1000+1];
int
evenlydistribute, leftover;
FILE
*fin = fopen ("icow.in", "r");
FILE *fout =
fopen ("icow.out", "w");
fscanf (fin,
"%d %d", &n, &t);
for (i = 0; i
< n; i++)
fscanf (fin, "%d", &r[i]);
for (i = 0; i <
t; i++) { /* play t
songs */
/* find highest rated song */
bestrate = -1;
for (j = 0; j < n; j++) {
if (r[j] > bestrate) { /* best, lowest index */
bestj = j;
bestrate = r[j];
}
}
fprintf (fout, "%d\n",
bestj+1); /* play it */
evenlydistribute = r[bestj]/(n-1);
leftover = r[bestj] % (n-1);
r[bestj] = 0;
for (j = 0; j < n; j++)
if (j != bestj)
r[j] += evenlydistribute;
for (k = j = 0; j < leftover; j++, k++) {
if (j == bestj) k++;
r[k]++;
}
}
}