Submission

Status:

PPPPP

Subtask/Task Score:

100/100

Score: 100

User: chokun

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

Language: cpp

Time: 0.023 second

Submitted On: 2026-10-07 21:02:29

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

// โครงสร้างข้อมูลสำหรับเก็บรายละเอียดของนิยายแต่ละเรื่อง
struct Book {
    int k;        // จำนวนเล่ม
    string name;  // ชื่อเรื่อง
    
    // กำหนดเงื่อนไขให้สามารถเรียงลำดับ (Sort) ตามชื่อเรื่อง A-Z ได้
    bool operator<(const Book& other) const {
        return name < other.name;
    }
};

int main() {
    // ปิดการ sync เพื่อประสิทธิภาพในการแสดงผลข้อมูลจำนวนมาก
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int L, N;
    if (!(cin >> L >> N)) return 0;

    vector<Book> books(N);
    for (int i = 0; i < N; ++i) {
        cin >> books[i].k >> books[i].name;
    }

    // 1. เรียงลำดับหนังสือตามชื่อเรื่องอักษร A-Z
    sort(books.begin(), books.end());

    // 2. กระจายหนังสือแต่ละเล่มลงใน vector สันหนังสือทั้งหมด เพื่อให้จัดการง่ายขึ้น
    // ดัชนีคู่-คี่ จะถูกนับรวมต่อเนื่องไปตลอดทั้งชั้นหนังสือ
    vector<string> all_spines;
    for (const auto& b : books) {
        for (int i = 0; i < b.k; ++i) {
            all_spines.push_back(b.name);
        }
    }

    int total_books = all_spines.size();

    // 3. เตรียมอาเรย์ 2 มิติ (ตารางข้อมูล) ขนาด L แถว x total_books คอลัมน์
    vector<vector<char>> grid(L, vector<char>(total_books));

    for (int col = 0; col < total_books; ++col) {
        string current_name = all_spines[col];
        int name_len = current_name.length();
        
        // ตรวจสอบว่าเป็นเล่มลำดับคี่ (แนวตั้ง) หรือ เล่มลำดับคู่ (กลับหัว)
        // หมายเหตุ: col เริ่มจาก 0 ดังนั้น col % 2 == 0 คือเล่มที่ 1, 3, 5 (แนวตั้ง)
        if (col % 2 == 0) {
            // สันหนังสือแนวตั้ง: ตัวอักษรอยู่บน จุดอยู่ล่าง
            for (int row = 0; row < L; ++row) {
                if (row < name_len) {
                    grid[row][col] = current_name[row];
                } else {
                    grid[row][col] = '.';
                }
            }
        } else {
            // สันหนังสือกลับหัว: จุดอยู่บน ตัวอักษรอยู่ล่าง (อ่านย้อนกลับจากท้ายขึ้นไป)
            int dots_count = L - name_len;
            for (int row = 0; row < L; ++row) {
                if (row < dots_count) {
                    grid[row][col] = '.';
                } else {
                    // ดึงตัวอักษรจากท้ายย้อนขึ้นมา
                    grid[row][col] = current_name[name_len - 1 - (row - dots_count)];
                }
            }
        }
    }

    // 4. พิมพ์ขอบบนสุดของชั้นหนังสือ
    for (int col = 0; col < total_books; ++col) {
        cout << "+-";
    }
    cout << "+\n";

    // 5. พิมพ์เนื้อหาและเส้นคั่นแต่ละแถวของตู้หนังสือ
    for (int row = 0; row < L; ++row) {
        for (int col = 0; col < total_books; ++col) {
            cout << "|" << grid[row][col];
        }
        cout << "|\n";
    }

    // 6. พิมพ์ขอบล่างสุดของชั้นหนังสือ
    for (int col = 0; col < total_books; ++col) {
        cout << "+-";
    }
    cout << "+\n";

    return 0;
}