This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int n, k;
vector<int> simulate(vector<int> v) {
vector<int> b(n+1, -1);
vector<int> inv(n+1, -1);
for(int i = 0;i<n;i++)inv[v[i]]=i+1;
for(int i = 0;i<n;i++) {
vector<int> er;
for(int j = 0;j<(int)v.size()-1;j++) {
if(v[j] < v[j+1]) {
er.push_back(j);
}
}
reverse(er.begin(), er.end());
if(er.size() == 0)break;
for(int j:er) {
b[inv[v[j]]] = i+1;
v.erase(v.begin()+j);
}
}
return b;
}
bool cmp(const vector<int> &A, const vector<int> &B) {
for(int i = 1;i<=n;i++) {
bool check=1;
for(int j = 1;j<i;j++) {
for(int x = 0;x<n;x++) {
if(A[x] == j and B[x] != j)check=false;
}
}
int y = 0, x = 0;
for(y = 0;y<n;y++) {
if(A[y] == i)break;
}
for(x = 0;x<n;x++) {
if(B[x] == i)break;
}
if(y >= x)check=false;
if(check)return 1;
}
return false;
}
int main () {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
vector<int> b(n+1, -1);
for(int i = 1;i<=n;i++)cin >> b[i];
vector<int> ans(n);
vector<vector<int>> res;
iota(ans.begin(), ans.end(), 1);
do {
vector<int> bb = simulate(ans);
if(bb == b){
res.push_back(ans);
}
}while(next_permutation(ans.begin(), ans.end()));
sort(res.begin(), res.end(), cmp);
if(res.size() >= k) {
for(int i:res[k-1])cout << i << " ";
cout << "\n";
}
else cout << "-1\n";
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:60:17: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
60 | if(res.size() >= k) {
| ~~~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |