QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#447654 | #8561. Restoring a Permutation | suomynonA | WA | 1ms | 7968kb | C++20 | 1.4kb | 2024-06-18 17:43:05 | 2024-06-18 17:43:06 |
Judging History
answer
#include <bits/stdc++.h>
const int N = 2e5;
int n, a[N + 5], b[N + 5], las[N + 5], ans[N + 5], in[N + 5];
std::vector<int> v[N + 5];
std::stack<int> sta;
std::vector<int> seq;
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
std::cin >> n;
for (int i = 1; i <= n; ++i) std::cin >> a[i];
for (int i = 1; i <= n; ++i) std::cin >> b[i];
for (int i = 1; i <= n; ++i) {
while (sta.size() and a[sta.top()] >= a[i]) v[i].push_back(sta.top()), sta.pop();
sta.push(i);
if (a[i] > 1) v[las[a[i] - 1]].push_back(i);
las[a[i]] = i;
}
while (sta.size()) sta.pop();
for (int i = 1; i <= n; ++i) las[i] = 0;
for (int i = n; i; --i) {
while (sta.size() and b[sta.top()] >= b[i]) v[i].push_back(sta.top()), sta.pop();
sta.push(i);
if (b[i] > 1) v[las[b[i] - 1]].push_back(i);
las[b[i]] = i;
}
for (int i = 1; i <= n; ++i) for (auto j : v[i]) ++in[j];
seq.push_back(0);
for (int i = 1; i <= n; ++i) if (!in[i]) seq.push_back(i);
for (int i = 1; i <= n; ++i) {
int p = seq[i];
for (auto j : v[p]) {
--in[j];
if (!in[j]) seq.push_back(j);
}
}
for (int i = 1; i <= n; ++i) ans[seq[i]] = i;
for (int i = 1; i <= n; ++i) std::cout << ans[i] << " \n"[i == n];
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7968kb
input:
5 1 1 1 2 3 3 2 1 1 1
output:
5 3 1 2 4
result:
ok
Test #2:
score: 0
Accepted
time: 0ms
memory: 7728kb
input:
6 1 1 2 2 3 3 2 1 2 1 2 1
output:
3 1 5 2 6 4
result:
ok
Test #3:
score: 0
Accepted
time: 1ms
memory: 5616kb
input:
1 1 1
output:
1
result:
ok
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 7664kb
input:
10 1 2 3 1 4 2 4 2 2 5 3 4 4 1 4 3 3 2 1 1
output:
5 6 7 1 8 4 9 3 2 10
result:
wrong answer GIS endinig at position 7 is 4, but 5 expected.