Submission

Status:

PP---

Subtask/Task Score:

40/100

Score: 40

User: cyblox_boi

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

Language: cpp

Time: 0.022 second

Submitted On: 2025-10-22 20:08:20

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

void printBanner(int totalBooks)
{
    for (int i = 0; i < (totalBooks * 2) + 1; i++)
    {
        if (i % 2 == 0)
        {
            cout << '+';
        }
        else
        {
            cout << '-';
        }
    }

    cout << '\n';
}

void printBookshelf(int totalBooks, vector<string> &bookshelf)
{
    printBanner(totalBooks);

    for (const string &i : bookshelf)
    {
        cout << '|';

        for (const char &j : i)
        {
            cout << j << '|';
        }

        cout << '\n';
    }

    printBanner(totalBooks);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int l, n;
    cin >> l >> n;

    map<string, int> books;

    int totalBooks = 0;
    int maxBookLength = 0;

    for (int i = 0; i < n; i++)
    {
        int bookAmount;
        string bookName;
        cin >> bookAmount >> bookName;

        books[bookName] = bookAmount;
        totalBooks += bookAmount;
        maxBookLength = max(maxBookLength, (int)bookName.length());
    }

    if (maxBookLength > l)
    {
        l = maxBookLength;
    }

    vector<string> bookshelf(l, string(totalBooks, '.'));
    int count = 0;
    int currentColumn = 0;

    for (const auto &book : books)
    {
        for (int i = 0; i < book.second; i++)
        {
            int currentRow;

            if (count % 2 == 0)
            {
                currentRow = 0;
            }
            else
            {
                currentRow = l - 1;
            }

            for (int j = 0; j < book.first.length(); j++)
            {
                bookshelf[currentRow][currentColumn] = book.first[j];

                if (count % 2 == 0)
                {
                    currentRow++;
                }
                else
                {
                    currentRow--;
                }
            }

            count++;
            currentColumn++;
        }
    }

    printBookshelf(totalBooks, bookshelf);

    return 0;
}