Translate

Views

Sunday, June 4, 2023

Solution UVA: 637 - Booklet Printing

 Click on table to read problem, discuss, debug!

 Last Submissions
  Problem  VerdictLangTimeBestRankSubmit Time
 | discuss637 - Booklet Printing AcceptedC++110.0800.000327238 mins ago


Suggest:

<1>

The pages are arranged in the following order:

Sheet 1: Front: Last page, First Page

                 Back: Next First Page, Prev Last Page

Sheet 2: Front: Perv of Perv Last page, Next of Next First Page

                 Back: Next of Next of Next First Page, Prev of Prev of Prev Last Page

....

=> In general, it is not necessary to understand the problem, it is important that you know how the output will be generated!


<2>

If a blank page is needed, it will be set as follows:

Sheet 1: Front: Blank, First Page (if blank = 1)

                 Back: Next First Page, Blank (if blank =2)

Sheet 2: Front: Blank, First Page (if blank=3)

                 Back: Next of Next First Page, Blank (if blank =4)

=> Generally you try to put blank pages on the first sheets (these blank pages will be the last pages) !


* After knowing the rules of generating output, you can easily code or refer to my code implementation!


Code:

#include<bits/stdc++.h>
using namespace std;

typedef pair<string, string> ii;
const int N= 1000111;
ii sheet[N][2];

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);

    int n;
    while(cin >> n && n){
        cout<<"Printing order for "<<n<<" pages:\n";
        int max_sheet= n/4+(n%4?1:0);
        int r=n, l=1, blank=max_sheet*4-n, cnt=0;
        while(cnt<max_sheet){
            ++cnt;
            if (blank > 0){
                sheet[cnt][0] = ii(blank > 0 ? "Blank" : to_string(r), to_string(l));
                l++; blank--;
            }
            else{
                sheet[cnt][0] = ii(to_string(r), to_string(l));
                r--; l++;
            }
           
            if (blank > 0){
                sheet[cnt][1] = ii(to_string(l), blank > 0 ? "Blank" : to_string(r));
                l++; blank--;  
            }
            else{
                sheet[cnt][1] = ii(to_string(l), to_string(r));
                r--; l++;  
            }
           
        }
        for(int i=1; i<=max_sheet; i++){
            cout<<"Sheet "<<i<<", front: "<<sheet[i][0].first<<", "<<sheet[i][0].second<<"\n";
            if (n>1) cout<<"Sheet "<<i<<", back : "<<sheet[i][1].first<<", "<<sheet[i][1].second<<"\n";
        }
       
    }
}

No comments: