UVa : 637 (Booklet Printing)


testCases:


5
Printing order for 5 pages:
Sheet 1, front: Blank, 1
Sheet 1, back : 2, Blank
Sheet 2, front: Blank, 3
Sheet 2, back : 4, 5
100
Printing order for 100 pages:
Sheet 1, front: 100, 1
Sheet 1, back : 2, 99
Sheet 2, front: 98, 3
Sheet 2, back : 4, 97
Sheet 3, front: 96, 5
Sheet 3, back : 6, 95
Sheet 4, front: 94, 7
Sheet 4, back : 8, 93
Sheet 5, front: 92, 9
Sheet 5, back : 10, 91
Sheet 6, front: 90, 11
Sheet 6, back : 12, 89
Sheet 7, front: 88, 13
Sheet 7, back : 14, 87
Sheet 8, front: 86, 15
Sheet 8, back : 16, 85
Sheet 9, front: 84, 17
Sheet 9, back : 18, 83
Sheet 10, front: 82, 19
Sheet 10, back : 20, 81
Sheet 11, front: 80, 21
Sheet 11, back : 22, 79
Sheet 12, front: 78, 23
Sheet 12, back : 24, 77
Sheet 13, front: 76, 25
Sheet 13, back : 26, 75
Sheet 14, front: 74, 27
Sheet 14, back : 28, 73
Sheet 15, front: 72, 29
Sheet 15, back : 30, 71
Sheet 16, front: 70, 31
Sheet 16, back : 32, 69
Sheet 17, front: 68, 33
Sheet 17, back : 34, 67
Sheet 18, front: 66, 35
Sheet 18, back : 36, 65
Sheet 19, front: 64, 37
Sheet 19, back : 38, 63
Sheet 20, front: 62, 39
Sheet 20, back : 40, 61
Sheet 21, front: 60, 41
Sheet 21, back : 42, 59
Sheet 22, front: 58, 43
Sheet 22, back : 44, 57
Sheet 23, front: 56, 45
Sheet 23, back : 46, 55
Sheet 24, front: 54, 47
Sheet 24, back : 48, 53
Sheet 25, front: 52, 49
Sheet 25, back : 50, 51
99
Printing order for 99 pages:
Sheet 1, front: Blank, 1
Sheet 1, back : 2, 99
Sheet 2, front: 98, 3
Sheet 2, back : 4, 97
Sheet 3, front: 96, 5
Sheet 3, back : 6, 95
Sheet 4, front: 94, 7
Sheet 4, back : 8, 93
Sheet 5, front: 92, 9
Sheet 5, back : 10, 91
Sheet 6, front: 90, 11
Sheet 6, back : 12, 89
Sheet 7, front: 88, 13
Sheet 7, back : 14, 87
Sheet 8, front: 86, 15
Sheet 8, back : 16, 85
Sheet 9, front: 84, 17
Sheet 9, back : 18, 83
Sheet 10, front: 82, 19
Sheet 10, back : 20, 81
Sheet 11, front: 80, 21
Sheet 11, back : 22, 79
Sheet 12, front: 78, 23
Sheet 12, back : 24, 77
Sheet 13, front: 76, 25
Sheet 13, back : 26, 75
Sheet 14, front: 74, 27
Sheet 14, back : 28, 73
Sheet 15, front: 72, 29
Sheet 15, back : 30, 71
Sheet 16, front: 70, 31
Sheet 16, back : 32, 69
Sheet 17, front: 68, 33
Sheet 17, back : 34, 67
Sheet 18, front: 66, 35
Sheet 18, back : 36, 65
Sheet 19, front: 64, 37
Sheet 19, back : 38, 63
Sheet 20, front: 62, 39
Sheet 20, back : 40, 61
Sheet 21, front: 60, 41
Sheet 21, back : 42, 59
Sheet 22, front: 58, 43
Sheet 22, back : 44, 57
Sheet 23, front: 56, 45
Sheet 23, back : 46, 55
Sheet 24, front: 54, 47
Sheet 24, back : 48, 53
Sheet 25, front: 52, 49
Sheet 25, back : 50, 51

trap

there is a space after “back”

solution:


// http://uva.onlinejudge.org/external/6/637.html
// Runtime: 0.008s
// Tag: presentation, adhoc

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>

#define Inf 2147483647
#define Pi acos(-1.0)
#define N 1000000
#define LL long long

inline LL Power(int b, int p) { LL ret = 1; for ( int i = 1; i <= p; i++ ) ret *= b; return ret; }

#define F(i, a, b) for( int i = (a); i < (b); i++ )
#define Fs(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fe(i, x) for(typeof (x.begin()) i = x.begin(); i != x.end (); i++)
#define Set(a, s) memset(a, s, sizeof (a))
#define max(a, b)  (a < b ? b : a)
#define min(a, b)  (a > b ? b : a)

using namespace std;

struct sheet {
    int pageNumber [4];
} arr [25 + 10];

void reset()
{
    for ( int i = 0; i < 35; i++ ) {
        for ( int j = 0; j < 4; j++ ) {
            arr [i].pageNumber [j] = 0;
        }
    }
}

void print (int sheetNumber, string order, int first, int second)
{
    cout << "Sheet " << sheetNumber << ", " << order << ": ";

    if ( first ) printf ("%d", first);
    else printf ("Blank");

    printf (", ");

    if ( second ) printf ("%d", second);
    else printf ("Blank");

    printf ("\n");
}

int main ()
{
    int n;

    while ( scanf ("%d", &n) && n ) {

        int numberOfPages = (n / 4) + (n % 4 == 0 ? 0 : 1);

        reset();

        int save_n = n;
        int cntPageNumber = 0;

        for ( int i = 0; i < numberOfPages; i++ ) {
            if ( n ) {
                arr [i].pageNumber [1] = ++cntPageNumber;
                n--;
            }

            if ( n ) {
                arr [i].pageNumber [2] = ++cntPageNumber;
                n--;
            }
        }

        for ( int i = numberOfPages - 1; i >= 0; i-- ) {
            if ( n ) {
                arr [i].pageNumber [3] = ++cntPageNumber;
                n--;
            }

            if ( n ) {
                arr [i].pageNumber [0] = ++cntPageNumber;
                n--;
            }
        }

        printf ("Printing order for %d pages:\n", save_n);

        for ( int i = 0; i < numberOfPages; i++ ) {
            if ( arr [i].pageNumber [0] || arr [i].pageNumber [1] ) {
                print(i + 1, "front", arr [i].pageNumber [0], arr [i].pageNumber [1]);
            }

            if ( arr [i].pageNumber [2] || arr [i].pageNumber [3] ) {
                print(i + 1, "back ", arr [i].pageNumber [2], arr [i].pageNumber [3]);
            }
        }
    }

	return 0;
}

// @END_OF_SOURCE_CODE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s