# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
485076 | nhanbin03 | Watermelon (INOI20_watermelon) | C++14 | 2091 ms | 1684 KiB |
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>
#define task "INOI20_watermelon"
#define X first
#define Y second
#define left ___left
#define right ___right
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 0 + 10;
int main() {
// cin.tie(0)->sync_with_stdio(0);
// cout.tie(0);
if (fopen(task ".inp","r")) {
freopen(task ".inp","r",stdin);
freopen(task ".ans","w",stdout);
}
int n, k;
cin >> n >> k;
assert(k == 1);
vector<int> a(n), v(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
v[i] = i;
}
vector<vector<int>> lst;
do {
vector<int> ans(n + 1);
for (int i = 0; i < n; i++) {
ans[v[i]] = i;
}
vector<int> left(n + 2), right(n + 2);
for (int i = 0; i <= n; i++) {
left[i + 1] = i;
right[i] = i + 1;
}
vector<int> cmp(n, -1);
int cnt = 0;
while (++cnt) {
bool changed = 0;
for (int i = right[0]; i <= n; i = right[i]) {
// cout << i << endl;
if (ans[i - 1] < ans[right[i] - 1]) {
right[left[i]] = right[i];
left[right[i]] = left[i];
changed = 1;
cmp[i - 1] = cnt;
}
}
if (changed == 0) break;
}
bool ok = 1;
for (int i = 0; i < n; i++) {
if (cmp[i] != a[i]) ok = 0;
}
if (ok) lst.push_back(v);
// cout << endl;
// for (int i = 0; i < n; i++) cout << ans[i] << " "; cout << endl;
// for (int i = 0; i < n; i++) cout << cmp[i] << " "; cout << endl;
} while (next_permutation(v.begin(), v.end()));
sort(lst.begin(), lst.end());
if (k > lst.size()) {
cout << -1;
return 0;
}
vector<int> ans(n);
for (int i = 0; i < n; i++) {
ans[lst[k - 1][i]] = i + 1;
}
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
}
Compilation message (stderr)
# | 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... |