CSES - Datatähti 2024 alku - Results
Submission details
Task:Käännöt
Sender:stpn129
Submission time:2023-11-09 10:50:31 +0200
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'void solve(int, int, std::vector<int>&)':
input/code.cpp:130:17: error: cannot convert 'std::vector<int>' to 'int' in initialization
  130 |         int j = pos
      |                 ^~~
      |                 |
      |                 std::vector<int>
input/code.cpp:130:13: warning: unused variable 'j' [-Wunused-variable]
  130 |         int j = pos
      |             ^

Code

#include <bits/stdc++.h>
 
using namespace std;
 
void init_code() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
 
bool is_ok(int j, int i, int n, int k) {
    if (j < 1 || (j + (k - 1)) > n || j < i) {
        return false;
    }
 
    return true;
}
 
void rev(int i, int k, vector<int>& a, vector<int>& pos, vector<int>& res) {
    reverse(a.begin() + i, a.begin() + i + k);
 
    for (int ik = 0; ik <= k - 1; ++ik) {
        pos[a[i + ik]] = i + ik;
    }
 
    res.push_back(i);
}
 
vector<int> compress(vector<int>& a) {
    vector<int> b = a;
 
    sort(b.begin(), b.end());
    b.erase(unique(b.begin(), b.end()), b.end());
 
    for (int &x : a)
        x = int(lower_bound(b.begin(), b.end(), x) - b.begin()) + 1;
 
    return a;
}
 
 
vector<int> solve2(int n, int k, vector<int>& a) {
    queue<vector<int>>  q;
    q.push(a);
 
    map<vector<int>, int> d;
    d[a] = 0;
 
    map<vector<int>, vector<int>> prev;
    
    map<vector<int>, int> prev_op;
    prev_op[a] = -1;
 
    while (!q.empty()) {
        vector<int> v = q.front();
        q.pop();
 
        if (is_sorted(v.begin(), v.end())) {
            vector<int> ops;
            while (prev_op[v] != -1) {
                ops.push_back(prev_op[v] + 1);
                v = prev[v];
            }
            reverse(ops.begin(), ops.end());
            return ops;
        }
 
        for (int i = 0; i < n - (k - 1); ++i) {
            vector<int> next = v;
            reverse(next.begin() + i, next.begin() + i + k);
            if (!d.count(next) || d[next] > d[v] + 1) {
                q.push(next);
                
                prev[next] = v;
                prev_op[next] = i;
 
                d[next] = d[v] + 1;
            }
        }
    }
 
    return vector<int> ();
}

 
vector<int> solve_i(int n, int k, int i) {
    vector<int> res;
    return res;
}
 

void solve(int n, int k, vector<int>& a) {
    a.insert(a.begin(), -1);

    vector<int> pos(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        pos[a[i]] = i;
    }
 
    vector<int> res;
 
    if (n <= 8) {
        a.erase(a.begin());
 
        res = solve2(n, k, a);
 
        if (res.size() > 0 || is_sorted(a.begin(), a.end())) {
            cout << "YES" << '\n';
            cout << res.size() << '\n';
            for (auto i : res) {
                cout << i << ' ';
            }
            cout << '\n';
        } else {
            cout << "NO" << '\n';
        }
        
        return;
    }
 
    for (int i = 1; i <= n; ++i) {
        if (a[i] == i) {
            continue;
        }

        int j = pos
    }
 
    if (is_sorted(a.begin() + 1, a.end())) {
        cout << "YES" << '\n';
        cout << res.size() << '\n';
        for (auto i : res) {
            cout << i << ' ';
        }
        cout << '\n';
        return;
    }

    /*
    map<vector<int>, vector<int>> mp;
    
    vector<int> lst;
    for (int i = n - 8; i <= n; ++i) {
        lst.push_back(a[i]);
    }
 
    lst = compress(lst);
 
    for (auto i : mp[lst]) {
        rev(i + (n - 9), k, a, pos, res);
    }*/
    
    if (is_sorted(a.begin() + 1, a.end())) {
        cout << "YES" << '\n';
        cout << res.size() << '\n';
        for (auto i : res) {
            cout << i << ' ';
        }
        cout << '\n';
        return;
    }
 
    cout << "NO" << '\n';
}
 
signed main() {
    init_code();
    int n, k;
    cin >> n >> k;

    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    solve(n, k, a);
    return 0;
}