CSES - Datatähti 2024 qualification mirror - Results
Submission details
Task:Käännöt
Sender:Levente
Submission time:2023-11-12 22:13:53 +0200
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'std::vector<int> findOperations(std::vector<int>&, int)':
input/code.cpp:16:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |         for (int i = 0; i < a.size(); ++i)
      |                         ~~^~~~~~~~~~
input/code.cpp:18:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |             for (int j = 0; j < a.size() - 1; ++j)
      |                             ~~^~~~~~~~~~~~~~
input/code.cpp:31:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |         for (int i = 0; i < a.size(); ++i)
      |                         ~~^~~~~~~~~~
input/code.cpp:35:31: error: no matching function for call to 'find(std::vector<int>::iter...

Code

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

bool canSort(int n, int k)
{
    return k == 2 || k % 2 == 1;
}

vector<int> findOperations(vector<int> &a, int k)
{
    vector<int> ops;
    if (k == 2)
    {
        // Bubble sort logic for k = 2
        for (int i = 0; i < a.size(); ++i)
        {
            for (int j = 0; j < a.size() - 1; ++j)
            {
                if (a[j] > a[j + 1])
                {
                    swap(a[j], a[j + 1]);
                    ops.push_back(j + 1);
                }
            }
        }
    }
    else
    {
        // Logic for k > 2 and odd
        for (int i = 0; i < a.size(); ++i)
        {
            while (a[i] != i + 1)
            {
                int idx = find(a.begin(), a.end(), i + 1) - a.begin();
                int start = max(0, idx - k + 1);
                reverse(a.begin() + start, a.begin() + start + k);
                ops.push_back(start + 1);
            }
        }
    }
    return ops;
}

int main()
{
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int &x : a)
        cin >> x;

    if (!canSort(n, k))
    {
        cout << "NO\n";
        return 0;
    }

    vector<int> ops = findOperations(a, k);
    cout << "YES\n";
    cout << ops.size() << "\n";
    for (int op : ops)
        cout << op << " ";
    cout << "\n";

    return 0;
}