Missing Pages: Difference between revisions

From programming_contest
Jump to navigation Jump to search
imported>Hz115
imported>Hz115
mNo edit summary
Line 3: Line 3:
Use the symmetry of the structure: the first two pages are bundled with the last two and so on.
Use the symmetry of the structure: the first two pages are bundled with the last two and so on.


Given an odd P, the other three numbers are P + 1, N - P and N - P + 1. For an even P, the other three pages are P - 1, N - P + 1 and N - P + 2
Given an odd P, the other three pages are P + 1, N - P and N - P + 1. For an even P, the other three pages are P - 1, N - P + 1 and N - P + 2.


== Code ==  
== Code ==  

Revision as of 05:32, 4 December 2015

Original Description

Idea

Use the symmetry of the structure: the first two pages are bundled with the last two and so on.

Given an odd P, the other three pages are P + 1, N - P and N - P + 1. For an even P, the other three pages are P - 1, N - P + 1 and N - P + 2.

Code

/*
 * =====================================================================================
 * Filename: MCPC2013-C.cpp
 * Description: Missing Pages
 * Compiler: g++
 * =====================================================================================
 */

#include <iostream>
#include <set>

using namespace std;

int main () {
    int N;
    cin >> N;
    while (N) {
        int P;
        cin >> P;
        set<int> ps;
        if (!(P % 2)) {
            ps.insert(P - 1);
            ps.insert(N - P + 1);
            ps.insert(N - P + 2);
        } else {
            ps.insert(P + 1);
            ps.insert(N - P);
            ps.insert(N - P + 1);
        }
        for (set<int>::iterator i = ps.begin(); i != ps.end(); i++) {
            if (i != ps.begin()) cout << " ";
            cout << *i;
        }
        cout << endl;
        cin >> N;
    }
    return 0;
}