# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
484777 | nhanbin03 | Watermelon (INOI20_watermelon) | C++14 | 29 ms | 19140 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
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 1e5 + 10;
int n, k;
int a[N], inDeg[N], ans[N];
vector<int> adj[N], pos[N];
bool buildDAG(int maxVal) {
for (int i = 1; i <= n; i++) {
pos[a[i]].push_back(i);
}
set<int> posSet;
for (int i = maxVal; i >= 1; i--) {
if (pos[i].empty()) return 0;
for (int j = pos[i].size() - 1; j >= 0; j--) {
int x = pos[i][j];
auto it = posSet.lower_bound(x);
if (it != posSet.end()) {
adj[x].push_back(*it);
inDeg[*it]++;
}
if (it != posSet.begin()) {
it--;
adj[x].push_back(*it);
inDeg[*it]++;
}
posSet.insert(x);
}
}
return 1;
}
void solve() {
priority_queue<int, vector<int>, greater<int>> pq;
for (int i = 1; i <= n; i++) {
if (inDeg[i] == 0) {
pq.push(i);
}
}
for (int i = 1; i <= n; i++) {
int u = pq.top(); pq.pop();
ans[u] = i;
for (int v : adj[u]) {
inDeg[v]--;
if (inDeg[v] == 0) pq.push(v);
}
}
for (int i = 1; i <= n; i++) {
assert(inDeg[i] == 0);
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cout.tie(0);
if (fopen(task ".inp","r")) {
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
if (a[n] != -1) {
cout << -1;
return 0;
}
int maxVal = 0;
for (int i = 1; i <= n; i++) {
maxVal = max(maxVal, a[i]);
}
for (int i = n; i >= 1; i--) {
if (a[i] == -1) a[i] = ++maxVal;
}
// for (int i = 1; i <= n; i++) {
// cout << a[i] << " ";
// }
// cout << "\n";
if (!buildDAG(maxVal)) {
cout << -1;
return 0;
}
// for (int i = 1; i <= n; i++) {
// cout << i << ": ";
// for (int j : adj[i]) {
// cout << j << " ";
// }
// cout << "\n";
// }
assert(0);
solve();
for (int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
for (int i = 1; i <= n; i++) {
for (int v : adj[i]) {
if (ans[i] >= ans[v]) assert(0);
}
}
}
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... |