USACO DEC07 Problem 'cheat' Analysis

by Rob Kolstad

This problem is one of data structure manipulation. The explicit example in the directions shows everything one needs to solve the task:

The simplest brute force approach is simple until it comes time to "put the top card on the bottom". One can implement this in a few ways:

My solution (below) uses the third method since it's pretty darn simple to implement:
#include <stdio.h>

int cmp (int *a, int *b) { return *a - *b; }

main () {
    FILE *fin = fopen ("cheat.in", "r");
    FILE *fout = fopen ("cheat.out", "w");
    int n, k, p, i, j;
    int deck[100000 * 11], decktop, deckbottom, betsy[100000], nbetsy;
    int player;
    fscanf (fin, "%d %d %d", &n, &k, &p);

    for (i = 0; i < k; i++) deck[i] = i;
    player = decktop = 0;
    deckbottom = k-1;
    for (i = 0; i < k; i++) {        /* k cards to be dealt */
        /*printf ("deal %d to player %d\n", deck[decktop]+1, player);*/
        if (player == n-1) betsy[nbetsy++] = deck[decktop]+1;
        player = (player + 1) % n;
        decktop++;              /* that card is gone */
        for (j = 0; j < p; j++)
            deck[++deckbottom] = deck[decktop++];   /* card to bottom */
    }
    qsort(betsy, k/n, sizeof (int), cmp);
    for (i = 0; i < k/n; i++) fprintf (fout, "%d\n", betsy[i]);
    exit (0);
}