Skip to content

Instantly share code, notes, and snippets.

@Noobgam
Created March 23, 2019 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noobgam/f0b6696a50bd255c0913f54d7243dc88 to your computer and use it in GitHub Desktop.
Save Noobgam/f0b6696a50bd255c0913f54d7243dc88 to your computer and use it in GitHub Desktop.
#pragma GCC optimize("Ofast")
#pragma comment(linker, "/stack:200000000")
#include <iostream>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <iomanip>
#include <ctime>
#include <cstring>
#include <map>
using namespace std;
const int N = 5100;
const int MOD = 1e9 + 7;
vector <int> now;
set <vector<int>> vis;
void cur(vector <int> stuff) {
if (stuff.empty()) {
reverse(now.begin(), now.end());
for (auto x : now) {
cout << x << "\n";
}
exit(0);
}
if (vis.count(stuff)) {
return;
}
vis.insert(stuff);
vector <int> check = stuff;
sort(check.begin(), check.end());
for (int i = 0; i < check.size(); ++i) {
if (check[i] > i + 1) {
return;
}
}
for (int i = 0; i < stuff.size(); ++i) {
if (stuff[i] > i + 1) {
return;
}
}
for (int i = stuff.size() - 1; i >= 0; --i) {
if (stuff[i] == i + 1) {
vector <int> stuff2 = stuff;
stuff2.erase(stuff2.begin() + i);
now.push_back(i + 1);
cur(stuff2);
now.pop_back();
}
}
}
int main() {
::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector <int> v(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
cur(v);
cout << -1 << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment