Submission details
Task:Sorting books
Sender:aalto26dw_009
Submission time:2026-09-23 16:55:02 +0300
Language:C++ (C++23)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main(int, char**)':
input/code.cpp:18:31: error: 'LLONG_MAX' was not declared in this scope
   18 |     long long best_op_found = LLONG_MAX;
      |                               ^~~~~~~~~
input/code.cpp:5:1: note: 'LLONG_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
    4 | #include <algorithm>
  +++ |+#include <climits>
    5 | 
input/code.cpp:19:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |     for (int cur_i=0; cur_i<original_books.size(); cur_i++) {
      |                       ~~~~~^~~~~~~~~~~~~~~~~~~~~~
input/code.cpp:28:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |         for (int m=0; m<books.size(); m++) {
      |                       ~^~~~~~~~~~~~~
input/code.cpp:30:28: warning...

Code

#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>


int main(int argc, char **argv) {
    int n;
    std::cin >> n;

    std::vector<int> original_books(n, 0);
    for (int i=0; i<n; i++) {
        std::cin >> original_books.at(i);
    }

    //std::cout << std::format("{}", original_books) << std::endl;

    long long best_op_found = LLONG_MAX;
    for (int cur_i=0; cur_i<original_books.size(); cur_i++) {
        long long op = 0;
        std::vector<int> books(original_books);

        //std::cout << "Starting" << std::endl;
        //std::cout << std::format("{}", books) << std::endl;

        bool leave = false;

        for (int m=0; m<books.size(); m++) {

            for (int i=0; i<books.size()-1; i++) {
                if (books.at(i) > books.at(i+1)) {
                    int aux = books.at(i);
                    books.at(i) = books.at(i+1);
                    books.at(i+1) = aux;
                    //std::cout << std::format("{}", books) << std::endl;
                    op++;
                    i=-1;
                }

                if (op >= best_op_found) {
                    leave = true;
                    break;
                }

                if (i == books.size() - 1) {
                    //std::cout << "llegado" << std::endl;
                    if (op < best_op_found) {
                        best_op_found = op;
                        break;
                    }
                }
            }

            if (leave) {
                break;
            }
        }

        //std::cout << op << std::endl;

        if (op < best_op_found) {
            best_op_found = op;
            break;
        }
    }

    std::cout << best_op_found << std::endl;

    return 0;
}