Submission

Status:

PP---

Subtask/Task Score:

40/100

Score: 40

User: MaYangPhanTe

Problemset: ชั้นหนังสือ

Language: cpp

Time: 0.027 second

Submitted On: 2026-03-19 15:55:43

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

void edge(int sumN) {
    for (int i = 0; i < sumN*2+1; i++) {
        if(i%2==0) cout << "+";
        else cout << "-";
    }
    cout << "\n";
}

struct item {
    int amount;
    string name;
};

void bookshelf() {
    int l,n;
    cin >> l >> n;
    vector<item> book(n);

    int x;
    int sumN = 0;
    string y;

    for(int i = 0; i < n; i++) {
        cin >> x >> y;
        book[i].amount = x;
        book[i].name = y;
        sumN += x;
    }

    sort ( book.begin(), book.end(), [](item &a, item &b){

        if(a.amount == b.amount) return a.name[0] < b.name[0];

        return a.amount < b.amount;
    });

    

    vector<string> rbook;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < book[i].amount; j++){
            rbook.push_back(book[i].name);
        }
    }

    edge(sumN);

    bool invert = 0;

    for (int i = 0; i < l; i++) {
        invert = false;

        for (int j = 0; j < sumN*2+1; j++) {
            if(j%2==0) {
                cout << "|";
                continue;
            }
            int idx = (j+1)/2-1;
            int ns = rbook[idx].length();
            
            if(invert == false) {
                if(i<ns) {
                    cout << rbook[idx][i];
                }
                else cout << ".";

                invert = true;
            }

            else {
                int k = l-ns-1;
                if(i>k) {
                    cout << rbook[idx][ns-(i-k)];
                }
                else cout << ".";
                
                invert = false;
            }

        }
        cout << "\n";

    }

    edge(sumN);
}

int main() {
    bookshelf();
}