CSES - Datatähti 2017 alku - Results
Submission details
Task:Järjestys
Sender:comp
Submission time:2016-10-08 14:04:58 +0300
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'std::vector<int> sort(int, std::vector<int>)':
input/code.cpp:11:9: error: 'reverse' is not a member of 'std'
         std::reverse(values.begin(), values.begin() + index + 1);
         ^
input/code.cpp:14:5: error: 'reverse' is not a member of 'std'
     std::reverse(values.begin(), values.end() - sorted);
     ^

Code

#include <iostream>
#include <vector>

int n;
int sorted = 0;
std::vector<int> reverses_k;

std::vector<int> sort(int index, std::vector<int> values) {
    if (index != 0) {
        reverses_k.push_back(index + 1);
        std::reverse(values.begin(), values.begin() + index + 1);
    }
    reverses_k.push_back(n - sorted);
    std::reverse(values.begin(), values.end() - sorted);
    sorted += 1;
    
    return values;
}

int main() {
    std::cin >> n;
    
    std::vector<int> values;
    int x;
    for(int i = 0; i < n; ++i) {
        std::cin >> x;
        values.push_back(x);
    }
    
    for(int v = n; v > 0; --v) {
        for(int i = 0; i < n; ++i) {
            if (i == n - sorted - 1) {
                sorted += 1;
            } else if (v == values[i] && i != n - 1) {
                values = sort(i, values);
                break;
            }
        }
    }
    
    std::cout << reverses_k.size() << "\n";
    for(auto k: reverses_k) {
        std::cout << k << " ";
    }
    return 0;
}