CSES - Datatähti 2024 alku - Results
Submission details
Task:Käännöt
Sender:snowflake
Submission time:2023-11-09 02:52:14 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'void A(int, int, std::vector<int>&, std::set<std::vector<int> >&, bool&)':
input/code.cpp:69:13: error: 'a' was not declared in this scope
   69 |         if (a[i] == n) biggestpos = i;
      |             ^

Code

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

int nummerz[512];
vector<int> exes;

void Flipflop(int pos, int til)
{
    exes.push_back(pos);
    reverse(nummerz + pos, nummerz + til + 1);
}

bool BF(int n, int k, int operas, vector<int>& hoes, set<vector<int>>& already, bool& sorted)
{
    if (sorted) return true;

    vector<int> v;
    for (int o = 1; o <= n; o++) v.push_back(nummerz[o]);
    if (already.count(v)) return false;
    already.insert(v);

    bool ookoo = true;
    for (int oo = 1; oo <= n; oo++)
    {
        if (nummerz[oo] != oo)
        {
            ookoo = false;
            break;
        }
    }

    if (ookoo)
    {
        sorted = ookoo;
        return ookoo;
    }
    if (operas == 0) return false;

    for (int ooo = 1; ooo <= (n - k + 1); ooo++)
    {
        hoes.push_back(ooo);
        reverse(nummerz + ooo, nummerz + ooo + k);

        if (BF(n, k, operas - 1, hoes, already, sorted)) return true;

        reverse(nummerz + ooo, nummerz + ooo + k);
        hoes.pop_back();
    }

    return false;
}

void A(int n, int k, vector<int>& hoes, set<vector<int>>& already, bool& sorted)
{
    if (n <= 9)
    {
        if (BF(n, k, 128, hoes, already, sorted))
        {
            for (auto it : hoes) exes.push_back(it);
            return;
        }
    }

    int biggestpos = 0;
    for (int i = 1; i <= n; i++)
        if (a[i] == n) biggestpos = i;

    int off = k - 1;

    while (biggestpos <= n - k + 1)
    {
        Flipflop(biggestpos, biggestpos + off);
        biggestpos += off;
    }

    if (biggestpos == n)
    {
        A(n - 1, k, hoes, already, sorted); // next
    }
    else
    {
        biggestpos -= off;
        Flipflop(biggestpos, biggestpos + off);
        while (biggestpos <= n - k)
        {
            cout << biggestpos;
            if (k % 2 == 1)
            {
                Flipflop(biggestpos - (k / 2) + 1, biggestpos - (k / 2) + 1 + off);
                biggestpos += 2;
            }
            else
            {
                Flipflop(biggestpos - (k / 2) + 1, biggestpos - (k / 2) + 1 + off);
                biggestpos++;
            }
        }
        Flipflop(biggestpos, biggestpos + off);
        biggestpos = n;
        A(n - 1, k, hoes, already, sorted);
    }
}

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

    int n;
    int k;
    cin >> n >> k;

    for (int z = 1; z <= n; z++) cin >> nummerz[z];

    vector<int> hoes;
    set<vector<int>> already;
    bool sorted = false;

    A(n, k, hoes, already, sorted);

    for (int zz = 1; zz <= n; zz++)
    {
        if (zz != nummerz[zz])
        {
            cout << "NO";
            return 0;
        }
    }

    cout << "YES" << endl;

    int bcount = exes.size();
    cout << bcount << endl;

    for (int zzz = 0; zzz < bcount; zzz++)
    {
        cout << exes[zzz] << " ";
    }

    return 0;
}