Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
|---|---|---|---|---|---|---|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int N, per[1 << 18];
struct SEG {
pair<long long, int> node[2 * 1 << 18];
long long lazy[2 * 1 << 18];
void lz(int nn) {
if (lazy[nn] != 0) {
node[nn].first -= lazy[nn];
lazy[nn * 2] += lazy[nn], lazy[nn * 2 + 1] += lazy[nn];
lazy[nn] = 0;
}
}
void sup(int p, long long val, int nn = 1, int ns = 1, int ne = N) {
if (ns > p or ne < p) return;
if (ns == ne) {
node[nn] = pair<long long, int>(val, p);
return;
}
lz(nn);
int m = (ns + ne) >> 1;
sup(p, val, nn * 2, ns, m);
sup(p, val, nn * 2 + 1, m + 1, ne);
if (node[nn * 2].first < 0)
node[nn] = node[nn * 2 + 1];
else if (node[nn * 2 + 1].first < 0)
node[nn] = node[nn * 2];
else if (node[nn * 2].first < node[nn * 2 + 1].first)
node[nn] = node[nn * 2];
else
node[nn] = node[nn * 2 + 1];
}
void rup(int s, int e, int val, int nn = 1, int ns = 1, int ne = N) {
if (ns > e or ne < s) return;
if (ns == ne) {
lazy[nn] += val;
node[nn].first -= lazy[nn];
lazy[nn] = 0;
return;
}
if (ns <= s and e <= ne) {
lazy[nn] += val;
lz(nn);
return;
}
lz(nn);
int m = (ns + ne) >> 1;
rup(s, e, val, nn * 2, ns, m);
rup(s, e, val, nn * 2 + 1, m + 1, ne);
if (node[nn * 2].first < 0)
node[nn] = node[nn * 2 + 1];
else if (node[nn * 2 + 1].first < 0)
node[nn] = node[nn * 2];
else if (node[nn * 2].first < node[nn * 2 + 1].first)
node[nn] = node[nn * 2];
else
node[nn] = node[nn * 2 + 1];
}
int ret() { return node[1].second; }
} st;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> N;
for (int i = 1; i <= N; ++i) {
long long s;
cin >> s;
st.sup(i, s);
}
for (int i = 1; i <= N; ++i) {
int place = st.ret();
per[place] = i;
st.sup(place, -1);
if (place == N) continue;
st.rup(place + 1, N, i);
}
for (int i = 1; i <= N; ++i) cout << per[i] << ' ';
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <class V, class U>
struct SegTree {
int n;
vector<V> st;
vector<U> lazy;
vector<int> leaves;
SegTree(const int n) : n(n) {
leaves.resize(n);
init(1, 0, n - 1);
lazy.resize(st.size());
}
void init(const int si, const int lo, const int hi) {
if (lo == hi) {
if (si >= (int)st.size()) st.resize(si + 1);
st[si] = V(0, lo);
leaves[lo] = si;
} else {
const int mid = (lo + hi) >> 1;
init(si << 1, lo, mid);
init(si << 1 | 1, mid + 1, hi);
st[si] = V(st[si << 1], st[si << 1 | 1]);
}
}
void updateLazy(const int si, const int lo, const int hi) {
lazy[si].apply(st[si], lo, hi);
if (lo != hi) {
lazy[si << 1] = U(lazy[si << 1], lazy[si]);
lazy[si << 1 | 1] = U(lazy[si << 1 | 1], lazy[si]);
}
lazy[si] = U();
}
V query(const int l, const int r) {
return (l <= r && l < n && r >= 0) ? query(l, r, 1, 0, n - 1) : V();
}
V query(const int l, const int r, const int si, const int lo, const int hi) {
updateLazy(si, lo, hi);
if (l <= lo && hi <= r) return st[si];
const int mid = (lo + hi) >> 1;
if (r <= mid) return query(l, r, si << 1, lo, mid);
if (mid < l) return query(l, r, si << 1 | 1, mid + 1, hi);
return V(query(l, r, si << 1, lo, mid),
query(l, r, si << 1 | 1, mid + 1, hi));
}
void update(const int l, const int r, const U u) {
if (l <= r) update(l, r, u, 1, 0, n - 1);
}
void update(const int l, const int r, const U &u, const int si, const int lo,
const int hi) {
if (l <= lo && hi <= r) {
lazy[si] = U(lazy[si], u);
updateLazy(si, lo, hi);
} else {
updateLazy(si, lo, hi);
if (hi < l || r < lo) return;
const int mid = (lo + hi) >> 1;
update(l, r, u, si << 1, lo, mid);
update(l, r, u, si << 1 | 1, mid + 1, hi);
st[si] = V(st[si << 1], st[si << 1 | 1]);
}
}
vector<int> remAll(const int l, const int r) {
vector<int> res;
remAll(l, r, 1, 0, n - 1, res);
return res;
}
void remAll(const int l, const int r, const int si, const int lo,
const int hi, vector<int> &res) {
updateLazy(si, lo, hi);
if (!st[si] || hi < l || r < lo) return;
if (lo == hi) {
res.push_back(lo);
st[si] = 0;
} else {
const int mid = (lo + hi) >> 1;
remAll(l, r, si << 1, lo, mid, res);
remAll(l, r, si << 1 | 1, mid + 1, hi, res);
st[si] = V(st[si << 1], st[si << 1 | 1]);
}
}
void updateAllLazy() { updateAllLazy(1, 0, n - 1); }
void updateAllLazy(const int si, const int lo, const int hi) {
updateLazy(si, lo, hi);
if (lo != hi) {
const int mid = (lo + hi) >> 1;
updateAllLazy(si << 1, lo, mid);
updateAllLazy(si << 1 | 1, mid + 1, hi);
}
}
vector<V> retrieve() {
updateAllLazy();
vector<V> res(n);
for (int i = 0; i < n; ++i) res[i] = st[leaves[i]];
return res;
}
};
struct StVal {
pair<long long, long long> v;
StVal() {}
StVal(const int i, const int j) {
v.first = i;
v.second = j;
}
StVal(const StVal &v1, const StVal &v2) {
v = (v1.v.first >= v2.v.first) ? v2.v : v1.v;
}
};
struct StUpdate {
long long v = 0;
StUpdate() {}
StUpdate(const long long v) : v(v) {}
StUpdate(const StUpdate &u1, const StUpdate &u2) { v = u1 + u2; }
operator long long() const { return v; }
void apply(StVal &v, const int lo, const int hi) { v.v.first += this->v; }
};
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
SegTree<StVal, StUpdate> st(n);
for (int i = 0; i < n; i++) {
int a;
cin >> a;
st.update(i, i, a);
}
vector<int> ans(n);
int k = 1;
for (int i = 0; i < n; i++) {
StVal p = st.query(0, n - 1);
ans[p.v.second] = k;
st.update(p.v.second, n - 1, -k);
st.update(p.v.second, p.v.second, (1 << 30));
k++;
}
for (int i = 0; i < n; i++) cout << ans[i] << " ";
cout << endl;
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
set<pair<long long, long long>> ss;
long long tot = 0;
for (int i = 1; i <= n; i++) {
tot += i;
ss.insert({tot, i});
}
int arr[n], res[n];
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = n - 1; i >= 0; i--) {
if (arr[i] == 0) {
res[i] = (*ss.begin()).second;
ss.erase(ss.begin());
} else {
set<pair<long long, long long>>::iterator it =
ss.lower_bound({arr[i] + 1, -1});
res[i] = (*it).second;
ss.erase(it);
}
}
for (int i = 0; i < n; i++) cout << res[i] << ' ';
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
/*
Code for task B by detestmaths
*/
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Solver {
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
// FastScanner in = new FastScanner("search.in");
//PrintWriter out = new PrintWriter("search.out");
int n = in.nextInt();
long s[] = new long[n];
for (int i = 0; i < n; i++) {
s[i] = in.nextLong();
}
segment_tree st = new segment_tree(s);
int ans[] = new int[n];
for (int i = 1; i < n + 1; i++) {
pair p = st.get(0, n - 1);
int ind = p.ind;
ans[ind] = i;
st.add(ind, ind, 1010101010);
st.add(ind + 1, n - 1, -i);
}
for (int i = 0; i < n; i++) {
out.print(ans[i] + " ");
}
out.close();
}
}
class segment_tree {
long min[];
int id[];
long a[];
long sum[];
segment_tree(long a[]) {
this.a = a;
int n = a.length;
min = new long[4 * n];
sum = new long[4 * n];
id = new int[4 * n];
build(1, 0, n - 1);
}
void build(int v, int l, int r) {
if (l == r) {
min[v] = a[l];
id[v] = l;
return;
}
int m = ((l + r) >> 1);
build(v * 2, l, m);
build(v * 2 + 1, m + 1, r);
if (min[v * 2] < min[v * 2 + 1]){
min[v] = min[v*2];
id[v] = id[v * 2];
}else if(min[v*2+1] < min[v*2]){
min[v] = min[v*2+1];
id[v] = id[v*2+1];
}else{
min[v] = min[v*2];
id[v] = max(id[v*2],id[v*2+1]);
}
}
void push(int v) {
min[v] += sum[v];
sum[v * 2] += sum[v];
sum[v * 2 + 1] += sum[v];
sum[v] = 0;
}
void upd(int v) {
long p1 = get_el(v * 2);
long p2 = get_el(v * 2 + 1);
if (p1 < p2) {
min[v] = p1;
id[v] = id[v * 2];
} else if (p1 > p2) {
min[v] = p2;
id[v] = id[v * 2 + 1];
} else {
min[v] = p1;
id[v] = max(id[v * 2], id[v * 2 + 1]);
}
}
long get_el(int v) {
return min[v] + sum[v];
}
void add(int l, int r, long x) {
add(1, 0, a.length - 1, l, r, x);
}
void add(int v, int l, int r, int left, int right, long x) {
if (l > right || r < left) return;
if (l >= left && r <= right) {
sum[v] += x;
return;
}
push(v);
int m = ((l + r) >> 1);
add(v * 2, l, m, left, right, x);
add(v * 2 + 1, m + 1, r, left, right, x);
upd(v);
}
pair get(int l, int r) {
return get(1, 0, a.length - 1, l, r);
}
pair combine(pair a, pair b) {
if (a.min < b.min) return a;
if (b.min < a.min) return b;
return new pair(a.min, max(a.ind, b.ind));
}
pair get(int v, int l, int r, int left, int right) {
if (l > right || r < left) return new pair((long) 1e17, -1);
if (l >= left && r <= right) {
return new pair(get_el(v), id[v]);
}
push(v);
int m = ((l + r) >> 1);
pair p1 = get(v * 2, l, m, left, right);
pair p2 = get(v * 2 + 1, m + 1, r, left, right);
pair q = combine(p1, p2);
upd(v);
return q;
}
}
class pair {
long min;
int ind;
public pair(long min, int ind) {
this.min = min;
this.ind = ind;
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
public FastScanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public FastScanner(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
public String next() throws IOException {
if (!st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
import math
import bisect
from collections import defaultdict;
def solve(s):
n = len(s)
result = [0] * n
xx = defaultdict(lambda: [])
for ind, x in enumerate(s):
xx[x].append(ind)
p = [-1]
sum = 0
for i in range(1, n + 1):
left = sum
for start in reversed(p):
if left in xx and xx[left][-1] > start:
result[xx[left][-1]] = i
p.append(xx[left][-1])
xx[left].pop(-1)
if len(xx[left]) == 0:
del xx[left]
sum += i
break
left -= result[start]
return result
def main():
n = int(input())
s = [int(x) for x in input().split()]
print(' '.join([str(x) for x in solve(s)]))
if __name__ == '__main__':
main()
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool mini(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool maxi(T &a, T b) {
return a < b ? (a = b, true) : false;
}
const int N = 2e5 + 5;
int n, p[N], ans[N];
long long bit[N];
void up(int i, int val) {
assert(i > 0);
for (; i <= n; i += i & -i) bit[i] += val;
}
long long get(int i) {
long long res = 0;
for (; i; i -= i & -i) res += bit[i];
return res;
}
inline void sol() {
cin >> n;
for (int i = 0, _ = (n); i < _; i++) cin >> p[i], up(i + 1, i + 1);
for (int i = (int)(n)-1; i >= 0; --i) {
long long l = 1, r = n, mid;
while (l < r) {
mid = (l + r + 1) >> 1;
if (get(mid - 1) <= p[i])
l = mid;
else
r = mid - 1;
}
up(l, -l);
ans[i] = l;
}
for (int i = 0, _ = (n); i < _; i++) cout << ans[i] << ' ';
}
signed main() {
{ ios_base::sync_with_stdio(false), cin.tie(NULL); };
cout.precision(10);
cout << fixed;
sol();
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 10;
struct node {
int val, wz;
} t[maxn];
int n;
int a[maxn];
bool cmp(node a, node b) {
if (a.val != b.val) return a.val < b.val;
return a.wz > b.wz;
}
int ans[maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
t[i].val = a[i], t[i].wz = i;
}
sort(t + 1, t + n + 1, cmp);
for (int i = 1; i <= n; ++i) ans[t[i].wz] = i;
for (int i = 1; i <= n; ++i) cout << ans[i] << " ";
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
#include <iomanip>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DECIMAL(n) std::cout << std::fixed;std::cout << std::setprecision(n);
#define hell 1000000007
#define narak 998244353
#define PI 3.14159265358979323844
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define maxc(v) *max_element(ALL(v))
#define minc(v) *min_element(ALL(v))
#define sqr(a) (a)*(a)
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) m*(n/GCD(m,n))
#define inputarr(a,n) for(int xxx=0;xxx<n;++xxx) cin>>a[xxx]
#define initarr(a,n,x) for (int xxx=0;xxx<n;++xxx) a[xxx]=x
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repA(i, a, n) for(int i = a; i <= (n); ++i)
#define repD(i, a, n) for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define invect(data,n,commands) for(int xxx = 0;xxx<n;xxx++){int tmp;cin>>tmp;data.pb(tmp);commands}
#define inset(data,n,commands) for(int xxx = 0;xxx<n;xxx++){int tmp;cin>>tmp;data.insert(tmp);commands}
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl
#define debug cerr<<"bhau"<<endl
#define between(n,a,b) (a<=n&&n<=b)
#define clamp(n,a,b) (((n)<(a))?(a):(((n)>(b))?(b):(n)))
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
template<typename T, typename U> static inline void amin(T &x, U y)
{
if (y < x)
x = y;
}
template<typename T, typename U> static inline void amax(T &x, U y)
{
if (x < y)
x = y;
}
std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vvi vector<vi >
#define vii vector<pii >
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
//const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
//const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
/*------------------------------------------------*/
//primes for hashing 937,991,1013,1409,1741
pii operator+(pii a, pii b)
{
return {a.fi+b.fi,a.se+b.se};
}
pll operator+(pll a, pll b)
{
return {a.fi+b.fi,a.se+b.se};
}
std::ostream& operator<<(std::ostream& out,pii a)
{
out<<a.fi<<" "<<a.se<<endl;
return out;
}
std::ostream& operator<<(std::ostream& out,pll a)
{
out<<a.fi<<" "<<a.se<<endl;
return out;
}
std::istream& operator>>(std::istream& in,pii &a)
{
in>>a.fi>>a.se;
return in;
}
std::istream& operator>>(std::istream& in,pll &a)
{
in>>a.fi>>a.se;
return in;
}
using namespace std;
using namespace __gnu_pbds;
const int maxn=1000005;
class FenwickTree{
public:
ll bit[maxn];
FenwickTree(){
for (int i = 0; i < maxn; ++i)
{
bit[i]=0;
}
}
ll sum(int x)
{
ll ans=0;
for(int j=x;j>=0;j=(j&(j+1))-1)
ans+=bit[j];
return ans;
}
ll sum(int a,int b)
{
return sum(b)-sum(a-1);
}
void add(int x,ll val)
{
for(int j=x;j<maxn;j=j|(j+1))
bit[j]+=val;
}
};
void solver()
{
ll n;
cin>>n;
ll s[n];
inputarr(s,n);
FenwickTree ft;
ll p[n];
for (int i = n-1; i >=0; --i)
{
ll k=s[i];
ll lo=1,hi=n;
while(hi-lo>0){
ll mid=(hi+lo+1)/2;
ll x=mid*(mid-1)/2;
x-=ft.sum(1,mid);
if(x>k){
hi=mid-1;
}else{
lo=mid;
}
}
p[i]=lo;
ft.add(lo,lo);
}
display(p);
}
int main()
{
FAST;
int testcases=1;
//cin>>testcases;
for(int i=0;i<testcases;++i)
{
solver();
}
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long a[N], seg[4 * N], n;
long long ans[N];
long long lazy[4 * N];
void minimum(long long node) {
seg[node] = min(seg[2 * node], seg[2 * node + 1]);
}
void fun(long long l, long long r, long long node, long long val) {
seg[node] += val;
if (l != r) {
if (2 * node <= 4 * N) {
lazy[2 * node] += val;
lazy[2 * node + 1] += val;
}
}
lazy[node] = 0;
}
void build(long long l, long long r, long long node) {
if (l == r) {
seg[node] = a[l];
return;
}
long long mid = (l + r) / 2;
build(l, mid, 2 * node);
build(mid + 1, r, 2 * node + 1);
minimum(node);
}
void update(long long l, long long r, long long node, long long ql,
long long qr, long long val) {
if (lazy[node]) fun(l, r, node, lazy[node]);
if (l > qr or r < ql) return;
if (l >= ql and r <= qr) {
fun(l, r, node, val);
return;
}
long long mid = (l + r) / 2;
update(l, mid, 2 * node, ql, qr, val);
update(mid + 1, r, 2 * node + 1, ql, qr, val);
minimum(node);
}
long long find(long long l, long long r, long long node) {
if (l == r) return l;
if (lazy[node]) fun(l, r, node, lazy[node]);
if (l != r) {
fun(l, r, 2 * node, lazy[2 * node]);
fun(l, r, 2 * node + 1, lazy[2 * node + 1]);
}
long long mid = (l + r) / 2;
if (seg[2 * node + 1] == 0)
return find(mid + 1, r, 2 * node + 1);
else
return find(l, mid, 2 * node);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) cin >> a[i];
build(1, n, 1);
for (long long i = 1; i <= n; i++) {
long long p = find(1, n, 1);
ans[p] = i;
update(1, n, 1, p + 1, n, -i);
update(1, n, 1, p, p, INT_MAX);
}
for (long long i = 1; i <= n; i++) cout << ans[i] << " ";
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long int n;
long long int tree[200001 * 10];
long int a[200002];
void build(long int node, long int start, long int end) {
if (start == end) {
tree[node] = a[start];
} else {
long int mid = (start + end) / 2;
build(2 * node + 1, start, mid);
build(2 * node + 2, mid + 1, end);
tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
}
}
void update(long int node, long int start, long int end, long int ind) {
if (start == end)
tree[node] = a[ind];
else {
long int mid = (start + end) / 2;
if (start <= ind && ind <= mid) {
update(2 * node + 1, start, mid, ind);
} else {
update(2 * node + 2, mid + 1, end, ind);
}
tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
}
}
long long int query(long int node, long int start, long int end, long int l,
long int r) {
if (r < start || end < l) {
return 0;
}
if (l <= start && end <= r) {
return tree[node];
}
long int mid = (start + end) / 2;
long long int p1 = query(2 * node + 1, start, mid, l, r);
long long int p2 = query(2 * node + 2, mid + 1, end, l, r);
return (p1 + p2);
}
long int idx[200002];
long int root(long int x) {
while (idx[x] != x) {
idx[x] = idx[idx[x]];
x = idx[x];
}
return x;
}
int main() {
cin >> n;
long int ans = 0;
for (long int i = 0; i <= n + 1; i++) a[i] = i, idx[i] = i;
build(0, 0, n);
long long int b[n];
for (long int i = 0; i < n; i++) cin >> b[i];
vector<long int> x;
for (long int i = n - 1; i >= 0; i--) {
long int l = 0, r = n;
long int ans = -1;
while (l <= r) {
long int mid = (l + r) / 2;
long long int temp1 = query(0, 0, n, 0, mid);
if (temp1 == b[i]) {
ans = mid + 1;
break;
} else if (temp1 < b[i]) {
l = mid + 1;
} else
r = mid - 1;
}
x.push_back(root(ans));
a[ans] = 0;
update(0, 0, n, ans);
idx[root(ans)] = root(ans + 1);
}
for (long int i = 0; i < x.size(); i++) cout << x[x.size() - 1 - i] << " ";
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const long long inf = 1e11;
struct Node {
int l, r, num;
long long tag;
long long val;
} node[4 * maxn];
long long a[maxn];
int ans[maxn];
void up(int no) {
if (node[no << 1].val < node[no << 1 | 1].val) {
node[no].val = node[no << 1].val;
node[no].num = node[no << 1].num;
} else {
node[no].val = node[no << 1 | 1].val;
node[no].num = node[no << 1 | 1].num;
}
}
void build(int l, int r, int no) {
node[no].l = l;
node[no].r = r;
node[no].tag = 0;
if (l == r) {
node[no].val = a[l];
node[no].num = l;
return;
}
int mid = l + r >> 1;
build(l, mid, no << 1);
build(mid + 1, r, no << 1 | 1);
up(no);
}
void pushdown(int no) {
node[no << 1].tag += node[no].tag;
node[no << 1].val += node[no].tag;
node[no << 1 | 1].tag += node[no].tag;
node[no << 1 | 1].val += node[no << 1 | 1].tag;
node[no].tag = 0;
}
void change(int l, int r, int no, long long num) {
if (node[no].r < l || node[no].l > r) return;
if (node[no].l >= l && node[no].r <= r) {
node[no].tag += num;
node[no].val += num;
return;
}
if (node[no].tag != 0) {
pushdown(no);
}
change(l, r, no << 1, num);
change(l, r, no << 1 | 1, num);
up(no);
}
int main() {
int n, i;
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
build(1, n, 1);
for (i = 1; i <= n; i++) {
int x = node[1].num;
ans[x] = i;
change(x, x, 1, inf);
change(x + 1, n, 1, -i);
}
for (i = 1; i <= n; i++) {
printf("%d ", ans[i]);
}
printf("\n");
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(1, sc, out);
out.close();
}
static class Task {
public long[] C;
public int lowBit(int x) {
return x&(-x);
}
public void add(int x,int y) {
for(int i=x;i<C.length;i+=lowBit(i))
C[i]+=y;
}
public long ask(int x) {
long res=0;
for(int i=x;i>0;i-=lowBit(i))
res+=C[i];
return res;
}
public void solve(int testNumber, InputReader sc, PrintWriter out) {
int n=sc.nextInt();
C=new long[n+1];
long[] s=new long[n+1];
long[] res=new long[n+1];
boolean[] jud=new boolean[n+1];
for(int i=1;i<=n;i++) {
s[i]=sc.nextLong();
add(i,i);
}
for(int i=1;i<=n;i++)
System.out.println(ask(i));
for(int i=n;i>=1;i--) {
int l=1;
int r=n;
int ans=0;
while(l<=r) {
int mid=(l+r)>>1;
long sum=ask(mid-1);
if(s[i]==sum) {
if(jud[mid]) {
l=mid+1;
continue;
}
ans=mid;
jud[ans]=true;
add(mid,-mid);
break;
}
if(s[i]<sum)
r=mid-1;
else
l=mid+1;
}
res[i]=ans;
}
for(int i=1;i<=n;i++)
out.print(res[i]+" ");
out.println();
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
private static int KvoElem = 131072 * 2;
private static int KvoForSum = 262144 * 2;
private static int tree[] = new int[KvoForSum];
private static long t[] = new long[KvoForSum * 2];
private static int arr[] = new int[KvoElem + 1];
private static int Result = 0;
public static PrintWriter out;
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
Main user = new Main();
for (int i = KvoElem; i < KvoForSum; i++)
user.update(i - KvoElem, 1);
int n = sc.nextInt();
for (int i = 0; i < KvoElem; i++) {
arr[i] = 0;
}
build(1, 0, KvoElem);
long[] value = new long[n];
for (int i = 0; i < n; i++) {
value[i] = sc.nextLong();
}
List <Integer> list = new ArrayList <>();
for (int i = n - 1; i >= 0; i--) {
long x = user.find_need(value[i]);
user.searchNext((int) x);
list.add(Result);
update(1, 0, n, Result, Result);
}
for (int i = n - 1; i >= 0; i--) {
System.out.print(list.get(i) + " ");
}
}
private long find_need(long x) {
int l = 0;
int r = 200005;
while (r > l) {
long mid = (l + r) / 2;
long val = get_sum(mid);
if (val == x) {
return mid + 1;
} else if (val > x) {
r = (int) mid;
} else {
l = (int) (mid + 1);
}
}
return l + 1;
}
private static long get_sum(long x) {
return x * (x + 1) / 2 - sum1(1, 0, KvoElem, 0, (int) x);
}
private static int sum(int index) {
index += KvoElem;
int res = tree[index];
while (index > 1) {
if (index % 2 == 1)
res += tree[index - 1];
index /= 2;
}
return res;
}
private void searchNext(int index) {
index += KvoElem;
if (tree[index] == 1) {
update(index - KvoElem, 0);
Result = index - KvoElem;
} else {
int SearcInd = sum(index - KvoElem) + 1;
index = 1;
while (Main.tree[index] != SearcInd && (index * 2 + 1) < Main.KvoForSum) {
if (Main.tree[index] > SearcInd) {
index *= 2;
} else if (Main.tree[index] < SearcInd) {
SearcInd -= Main.tree[index];
index++;
}
}
UpdateIfRavno(index);
}
}
private void UpdateIfRavno(int index) {
while ((index * 2 + 1) < Main.KvoForSum) {
if (Main.tree[index * 2 + 1] == 0)
index *= 2;
else
index = index * 2 + 1;
}
Result = index - KvoElem;
update(index - Main.KvoElem, 0);
}
private void update(int index, int value) {
index += Main.KvoElem;
int a = value - Main.tree[index];
while (index >= 1) {
Main.tree[index] += a;
index /= 2;
}
}
public static void build(int v, int tl, int tr) {
if (tl == tr)
t[v] = arr[tl];
else {
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm + 1, tr);
t[v] = t[v * 2] + t[v * 2 + 1];
}
}
public static long sum1(int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
if (l == tl && r == tr)
return t[v];
int tm = (tl + tr) / 2;
return sum1(v * 2, tl, tm, l, Math.min(r, tm))
+ sum1(v * 2 + 1, tm + 1, tr, Math.max(l, tm + 1), r);
}
public static void update(int v, int tl, int tr, int pos, int new_val) {
if (tl == tr)
t[v] = new_val;
else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v * 2, tl, tm, pos, new_val);
else
update(v * 2 + 1, tm + 1, tr, pos, new_val);
t[v] = t[v * 2] + t[v * 2 + 1];
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct Node {
int l, r;
long long s;
} t[800005];
int n, ans[200005];
long long s[200005];
void pushup(int rt) {
int lc = rt << 1, rc = lc | 1;
t[rt].s = t[lc].s + t[rc].s;
}
void build(int rt, int l, int r) {
t[rt].l = l;
t[rt].r = r;
t[rt].s = 0;
if (l == r) {
t[rt].s = l;
return;
}
int mid = (l + r) / 2;
int lc = rt << 1, rc = lc | 1;
build(lc, l, mid);
build(rc, mid + 1, r);
pushup(rt);
}
int query(int rt, long long k) {
if (t[rt].l == t[rt].r) {
t[rt].s = 0;
return t[rt].l;
}
int mid = (t[rt].l + t[rt].r) / 2;
int lc = rt << 1, rc = lc | 1;
int res = 0;
if (t[lc].s > k) {
res = query(lc, k);
t[rt].s -= res;
} else {
res = query(rc, k - t[lc].s);
t[rt].s -= res;
}
return res;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) scanf("%d", &s[i]);
build(1, 1, n);
for (int i = n; i >= 1; i--) {
ans[i] = query(1, s[i]);
}
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e11;
const double EPS = 1e-9;
const long long MOD = (long long)(1e9 + 7);
const long long MAXV = (long long)(2e5 + 10);
const long long MAXE = (long long)(1e6 + 10);
long long seg[MAXV * 4];
long long lazy[MAXV * 4];
long long A[MAXV * 4];
void init(long long n, long long s, long long e, long long p) {
if (s + 1 == e) {
seg[n] = s;
} else {
long long m = (s + e) >> 1;
if (p < m)
init(n * 2, s, m, p);
else
init(n * 2 + 1, m, e, p);
long long l = seg[n * 2];
long long r = seg[n * 2 + 1];
if (A[l] < A[r])
seg[n] = l;
else
seg[n] = r;
}
}
void add_lazy(long long n, long long s, long long e, long long x) {
lazy[n] = x;
A[seg[n]] += x;
}
void push(long long n, long long s, long long e, long long x) {
if (s + 1 == e) return;
long long m = (s + e) >> 1;
add_lazy(n * 2, s, m, lazy[n]);
add_lazy(n * 2 + 1, m, e, lazy[n]);
lazy[n] = 0;
}
void update(long long n, long long s, long long e, long long a, long long b,
long long x) {
if (s >= b || e <= a) return;
if (s == a && e == b)
add_lazy(n, s, e, x);
else {
if (lazy[n]) push(n, s, e, x);
int m = (s + e) >> 1;
if (b <= m)
update(n * 2, s, m, a, b, x);
else if (a >= m)
update(n * 2 + 1, m, e, a, b, x);
else {
update(n * 2, s, m, a, m, x);
update(n * 2 + 1, m, e, m, b, x);
}
long long l = seg[n * 2];
long long r = seg[n * 2 + 1];
if (A[l] < A[r])
seg[n] = l;
else
seg[n] = r;
}
}
long long n;
long long ans[MAXV];
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (long long i = 0; i < n; i++) cin >> A[i];
for (long long i = 0; i < n; i++) init(1, 0, n, i);
long long curr = 1;
for (int i = 0; i < n; i++) {
long long p = seg[1];
ans[p] = curr;
update(1, 0, n, p + 1, n, -curr);
update(1, 0, n, p, p + 1, INF);
curr++;
}
for (int i = 0; i < n; i++) cout << ans[i] << " ";
cout << "\n";
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class Main
{
boolean multiple = false;
long MOD = 998244353;
@SuppressWarnings("Duplicates")
void solve() throws Exception
{
int n = sc.nextInt();
Node[] arr = new Node[n];
int[] ans = new int[n];
ArrayList<Node> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(new Node(i, sc.nextInt()));
Collections.sort(list);
int ptr = 0;
for (int i = 0; i < n; i++)
ans[list.get(i).idx] = ++ptr;
printarr(ans);
}
class Node implements Comparable<Node>
{
int idx, val;
Node(int I, int V)
{
idx = I;
val = V;
}
@Override
public int compareTo(Node o)
{
if (val != o.val) return Integer.compare(val, o.val);
return Integer.compare(o.idx, idx);
}
}
long inv(long a, long b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
long gcd(long a, long b) { return a == 0 ? b : gcd(b % a, a); }
long powmod(long x, long pow) { if (pow == 0) return 1L;long temp = powmod(x,pow / 2);if (pow % 2 == 0) return (temp * temp) % MOD;return (((x * temp) % MOD) * temp) % MOD; }
long lcm(long a, long b) { return (a * b) / gcd(a , b); }
void printarr(int[] arr) { for (int t : arr) System.out.print(t + " ");System.out.println(); }
TreeMap<Integer, TreeSet<Integer>> buildGraph(int n, int m) throws Exception{TreeMap<Integer, TreeSet<Integer>> graph = new TreeMap<>();for (int i = 1; i <= n; i++) graph.put(i, new TreeSet<>());for (int i = 0; i < m; i++) { int u = sc.nextInt();int v = sc.nextInt();graph.get(u).add(v);graph.get(v).add(u); }return graph; }
TreeMap<Integer, TreeSet<Integer>> buildDirGraph(int n, int m) throws Exception { TreeMap<Integer, TreeSet<Integer>> graph = new TreeMap<>();for (int i = 1; i <= n; i++) graph.put(i, new TreeSet<>());for (int i = 0; i < m; i++) graph.get(sc.nextInt()).add(sc.nextInt());return graph; }
TreeMap<Integer, TreeSet<Integer>> revGraph(TreeMap<Integer, TreeSet<Integer>> graph) { TreeMap<Integer, TreeSet<Integer>> rev = new TreeMap<>();for (Integer u : graph.keySet()) rev.put(u, new TreeSet<>());for (Integer u : graph.keySet()) for (Integer v : graph.get(u)) rev.get(v).add(u);return rev; }
int[] iarr(int n) throws Exception { int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = sc.nextInt();return a; }
Integer[] Iarr(int n) throws Exception { Integer[] a = new Integer[n];for (int i = 0; i < n; i++) a[i] = sc.nextInt();return a; }
long[] larr(int n) throws Exception { long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = sc.nextLong();return a; }
TreeMap<Long, Integer> counts(long[] arr) { TreeMap<Long, Integer> counts = new TreeMap<>(); for (long a : arr) { if (!counts.containsKey(a)) counts.put(a, 0); counts.put(a, counts.get(a) + 1);} return counts; }
String reverse(String s, int i, int j) /*[i, j] closed*/ { return (i > j) ? reverse(s, j, i) : s.substring(0, i) + (new StringBuilder(s.substring(i, j + 1))).reverse() + ((j + 1 <= s.length() - 1) ? s.substring(j + 1) : "");}
Reader sc = new Reader(); public static void main(String[] args) throws Exception{new Main();}
static class Reader{
final private int BUFFER_SIZE = 1 << 22;
private DataInputStream din;private byte[] buffer;
private int bufferPointer, bytesRead;public Reader(){
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];bufferPointer = bytesRead = 0;}
public Reader(String file_name) throws IOException
{din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];bufferPointer = bytesRead = 0;}
public String readLine() throws IOException{byte[] buf = new byte[1<<18];
int cnt = 0, c;while ((c = read()) != -1)
{if (c == '\n')break;buf[cnt++] = (byte) c;}
return new String(buf, 0, cnt);}
public int nextInt() throws IOException{ int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret;}public long nextLong() throws IOException{ long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret;}public double nextDouble() throws IOException{ double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret;}private void fillBuffer() throws IOException{ bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1;}private byte read() throws IOException{ if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++];}public void close() throws IOException{ if (din == null) return; din.close();}} Main() throws Exception{if (multiple){ int q = sc.nextInt();for (int i = 0; i < q; i++) solve();} else solve(); out.close();} PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
class DualBIT():
def __init__(self, n):
self.n = n
self.bit = [0] * (n + 1)
def get(self, i):
'''i番目の要素を取得'''
i = i + 1
s = 0
while i <= self.n:
s += self.bit[i]
i += i & -i
return s
def _add(self, i, x):
while i > 0:
self.bit[i] += x
i -= i & -i
def add(self, i, j, x):
'''[i, j)の要素にxを加算する'''
self._add(j, x)
self._add(i, -x)
n = int(input())
a = list(map(int, input().split()))
bit = DualBIT(n+3)
for i in range(1, n+1):
bit.add(i+1, n+1, i)
li = []
flag = False
while True:
if not a:
break
ok = n + 1
ng = 0
num = a[-1]
if num == 0 and not flag:
flag = True
bit.add(1, n + 2, -1)
li.append(1)
del a[-1]
continue
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if bit.get(mid) > num:
ok = mid
else:
ng = mid
tmp = ok - 1
bit.add(tmp + 1, n + 2, -ok)
li.append(tmp)
del a[-1]
print(*li[::-1])
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
/*
Code for task B by detestmaths
*/
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Solver {
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
// FastScanner in = new FastScanner("search.in");
//PrintWriter out = new PrintWriter("search.out");
int n = in.nextInt();
long s[] = new long[n];
for (int i = 0; i < n; i++) {
s[i] = in.nextLong();
}
segment_tree st = new segment_tree(s);
int ans[] = new int[n];
for (int i = 1; i < n + 1; i++) {
pair p = st.get(0, n - 1);
int ind = p.ind;
ans[ind] = i;
st.add(ind, ind, 1010101);
st.add(ind + 1, n - 1, -i);
}
for (int i = 0; i < n; i++) {
out.print(ans[i] + " ");
}
out.close();
}
}
class segment_tree {
long min[];
int id[];
long a[];
long sum[];
segment_tree(long a[]) {
this.a = a;
int n = a.length;
min = new long[4 * n];
sum = new long[4 * n];
id = new int[4 * n];
build(1, 0, n - 1);
}
void build(int v, int l, int r) {
if (l == r) {
min[v] = a[l];
id[v] = l;
return;
}
int m = ((l + r) >> 1);
build(v * 2, l, m);
build(v * 2 + 1, m + 1, r);
if (min[v * 2] < min[v * 2 + 1]){
min[v] = min[v*2];
id[v] = id[v * 2];
}else if(min[v*2+1] < min[v*2]){
min[v] = min[v*2+1];
id[v] = id[v*2+1];
}else{
min[v] = min[v*2];
id[v] = max(id[v*2],id[v*2+1]);
}
}
void push(int v) {
min[v] += sum[v];
sum[v * 2] += sum[v];
sum[v * 2 + 1] += sum[v];
sum[v] = 0;
}
void upd(int v) {
long p1 = get_el(v * 2);
long p2 = get_el(v * 2 + 1);
if (p1 < p2) {
min[v] = p1;
id[v] = id[v * 2];
} else if (p1 > p2) {
min[v] = p2;
id[v] = id[v * 2 + 1];
} else {
min[v] = p1;
id[v] = max(id[v * 2], id[v * 2 + 1]);
}
}
long get_el(int v) {
return min[v] + sum[v];
}
void add(int l, int r, long x) {
add(1, 0, a.length - 1, l, r, x);
}
void add(int v, int l, int r, int left, int right, long x) {
if (l > right || r < left) return;
if (l >= left && r <= right) {
sum[v] += x;
return;
}
push(v);
int m = ((l + r) >> 1);
add(v * 2, l, m, left, right, x);
add(v * 2 + 1, m + 1, r, left, right, x);
upd(v);
}
pair get(int l, int r) {
return get(1, 0, a.length - 1, l, r);
}
pair combine(pair a, pair b) {
if (a.min < b.min) return a;
if (b.min < a.min) return b;
return new pair(a.min, max(a.ind, b.ind));
}
pair get(int v, int l, int r, int left, int right) {
if (l > right || r < left) return new pair((long) 1e17, -1);
if (l >= left && r <= right) {
return new pair(get_el(v), id[v]);
}
push(v);
int m = ((l + r) >> 1);
pair p1 = get(v * 2, l, m, left, right);
pair p2 = get(v * 2 + 1, m + 1, r, left, right);
pair q = combine(p1, p2);
upd(v);
return q;
}
}
class pair {
long min;
int ind;
public pair(long min, int ind) {
this.min = min;
this.ind = ind;
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
public FastScanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public FastScanner(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
public String next() throws IOException {
if (!st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class Test {
static int readInt() {
int ans = 0;
boolean neg = false;
try {
boolean start = false;
for (int c = 0; (c = System.in.read()) != -1; ) {
if (c == '-') {
start = true;
neg = true;
continue;
} else if (c >= '0' && c <= '9') {
start = true;
ans = ans * 10 + c - '0';
} else if (start) break;
}
} catch (IOException e) {
}
return neg ? -ans : ans;
}
static long readLong() {
long ans = 0;
boolean neg = false;
try {
boolean start = false;
for (int c = 0; (c = System.in.read()) != -1; ) {
if (c == '-') {
start = true;
neg = true;
continue;
} else if (c >= '0' && c <= '9') {
start = true;
ans = ans * 10 + c - '0';
} else if (start) break;
}
} catch (IOException e) {
}
return neg ? -ans : ans;
}
static String readLine() {
StringBuilder b = new StringBuilder();
try {
boolean start = false;
for (int c = 0; (c = System.in.read()) != -1; ) {
if (Character.isLetterOrDigit(c)) {
start = true;
b.append((char)c);
} else if (start) break;
}
} catch (IOException e) {
}
return b.toString();
}
static PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
void start() {
int n = readInt();
int[] s = new int[n+1];
Map<Integer, Stack<Integer>> can = new HashMap<>();
for (int i = 1; i <= n; i++) {
s[i] = readInt();
can.computeIfAbsent(s[i], x -> new Stack<>()).push(i);
}
int[] ans = new int[n+1];
TreeMap<Integer, Integer> pos = new TreeMap<>();
int sum = 0;
loop:
for (int i = 1; i <= n; i++) {
int c = sum;
for (int k : pos.keySet()) {
Stack<Integer> stk = can.get(c);
if (stk != null && !stk.isEmpty() && stk.peek() > -k) {
int p = stk.pop();
ans[p] = i;
pos.put(-p, i);
sum += i;
continue loop;
}
c -= pos.get(k);
}
if (can.get(0).isEmpty()) {
writer.println(i + " ");
return;
}
int p = can.get(c).pop();
ans[p] = i;
pos.put(-p, i);
sum += i;
}
for (int i = 1; i <= n; i++) writer.print(ans[i] + " ");
}
public static void main(String[] args) {
Test te = new Test();
te.start();
writer.flush();
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
//package manthan19;
import java.io.*;
import java.util.*;
public class D {
static TreeMap<Long, Long> map = new TreeMap<Long, Long>();
static TreeSet<Long> avail = new TreeSet<Long>();
public static void main(String[] args) {
MyScanner sc = new MyScanner();
int n = sc.nextInt();
for(long i = 1; i <= n; i++)
avail.add(i);
long[] s = new long[n];
for(int i = 0; i < n; i++)
s[i] = sc.nextLong();
long[] p = new long[n];
for(int i = n - 1; i >= 0; i--) {
p[i] = avail.ceiling(desum(s[i], 1, n));
avail.remove(p[i]);
if(map.floorKey(p[i]) != null)
map.put(p[i], p[i] + map.floorKey(p[i]));
else
map.put(p[i], p[i]);
//System.out.println(map);
}
for(long i : p)
out.print(i + " ");
out.println();
out.close();
}
public static long desum(long s, long l, long r) {
if(r == l) return l;
long m = (l + r) / 2;
long sum = m * (m - 1) / 2 - (map.floorKey(m - 1) != null ? map.floorKey(m - 1) : 0);
//System.out.println(m + " "+ sum + " "+ s);
if(sum < s)
return desum(s, m + 1, r);
return desum(s, l, m);
}
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static class MyScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
String next() {
while (st == null || !st.hasMoreElements())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
int ans[maxn], s[maxn], T[maxn * 8], n, lazy[maxn * 4], dif;
void pass(int cur) {
T[cur] -= lazy[cur];
lazy[cur << 1] += lazy[cur];
lazy[(cur << 1) + 1] += lazy[cur];
lazy[cur] = 0;
}
void build(int cur, int l, int r) {
if (l == r) {
T[cur] = s[l];
return;
}
int mid = (l + r) >> 1, ls = cur << 1, rs = ls + 1;
build(ls, l, mid);
build(rs, mid + 1, r);
T[cur] = min(T[ls], T[rs]);
}
void modify(int cur, int l, int r, int ll, int rr) {
pass(cur);
if (rr < l || ll > r) {
return;
}
if (l >= ll && rr >= r) {
lazy[cur] += dif;
pass(cur);
return;
}
int mid = (l + r) >> 1;
modify(cur << 1, l, mid, ll, rr);
modify((cur << 1) + 1, mid + 1, r, ll, rr);
T[cur] = min(T[cur << 1], T[1 + (cur << 1)]);
}
int query(int cur, int l, int r) {
pass(cur);
if (l == r) return l;
int mid = (l + r) >> 1;
int ls = cur << 1, rs = ls + 1, t1 = 0, t2 = 0;
pass(ls);
pass(rs);
if (!T[ls]) t1 = query(ls, l, mid);
if (!T[rs]) t2 = query(rs, mid + 1, r);
return max(t1, t2);
}
int main() {
scanf("%d\n", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &s[i]);
}
build(1, 1, n);
int ss;
for (int i = 1; i <= n; i++) {
int s = query(1, 1, n);
ans[s] = i;
dif = i;
modify(1, 1, n, s + 1, n);
dif = -0x3f3f3f3f;
modify(1, 1, n, s, s);
}
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
/* Rajkin Hossain */
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class D{
FastInput k = new FastInput(System.in);
//FastInput k = new FastInput("/home/rajkin/Desktop/input.txt");
FastOutput z = new FastOutput();
Pair [] y;
int [] ans;
void startProgram() {
while(k.hasNext()) {
int n = k.nextInt();
y = new Pair[n];
ans = new int[n];
for(int i = 0; i<n; i++) {
y[i] = new Pair(i, k.nextInt());
}
Arrays.sort(y);
int start = 1;
for(int i = 0; i<n; i++) {
Pair p = y[i];
ans[p.index] = start++;
}
for(int a : ans) {
z.print(a+" ");
}
z.println();
}
z.flush();
System.exit(0);
}
class Pair implements Comparable<Pair> {
Integer index;
Integer value;
public Pair(int index, int value) {
super();
this.index = index;
this.value = value;
}
@Override
public int compareTo(Pair p) {
if(p.value == this.value) {
return p.index - this.index;
}
return this.value - p.value;
}
}
public static void main(String [] args) throws IOException {
new Thread(null, new Runnable(){
public void run(){
try{
new D().startProgram();
}
catch(Exception e){
e.printStackTrace();
}
}
},"Main",1<<28).start();
}
static class Template {
static <E> HashSet<E> generateSet(E [] y) {
HashSet<E> set = new HashSet<E>();
for(E key : y) {
set.add(key);
}
return set;
}
static <E> HashMap<E, Integer> generateCountMap(E [] y) {
HashMap<E, Integer> map = new HashMap<E, Integer>();
for(E key : y) {
map.putIfAbsent(key, 0);
map.put(key, map.get(key) + 1);
}
return map;
}
}
/* MARK: FastInput and FastOutput */
class FastInput {
BufferedReader reader;
StringTokenizer tokenizer;
FastInput(InputStream stream){
reader = new BufferedReader(new InputStreamReader(stream));
}
FastInput(String path){
try {
reader = new BufferedReader(new FileReader(path));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tokenizer = null;
}
String next() {
return nextToken();
}
String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
boolean hasNext(){
try {
return reader.ready();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
String nextToken() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
String line = null;
try {
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (line == null) {
return null;
}
tokenizer = new StringTokenizer(line);
}
return tokenizer.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.valueOf(nextToken());
}
int [] getInputIntArray(int n) {
int [] input = new int[n];
for(int i = 0; i<n; i++) {
input[i] = nextInt();
}
return input;
}
long [] getInputLongArray(int n) {
long [] input = new long[n];
for(int i = 0; i<n; i++) {
input[i] = nextLong();
}
return input;
}
int [] getInputIntArrayOneBasedIndex(int n) {
int [] input = new int[n+1];
for(int i = 1; i<=n; i++) {
input[i] = nextInt();
}
return input;
}
long [] getInputLongArrayOneBasedIndex(int n) {
long [] input = new long[n+1];
for(int i = 1; i<=n; i++) {
input[i] = nextLong();
}
return input;
}
<E> void fill2DArray(E [][] array, E value) {
for(int i = 0; i<array.length; i++) {
for(int j = 0; j<array[0].length; j++) {
array[i][j] = value;
}
}
}
<E> void fill3DArray(E [][][] array, E value) {
for(int i = 0; i<array.length; i++) {
for(int j = 0; j<array[0].length; j++) {
for(int k = 0; k<array[0][0].length; k++) {
array[i][j][k] = value;
}
}
}
}
<E> void fill4DArray(E [][][][] array, E value) {
for(int i = 0; i<array.length; i++) {
for(int j = 0; j<array[0].length; j++) {
for(int k = 0; k<array[0][0].length; k++) {
for(int l = 0; l<array[0][0][0].length; l++) {
array[i][j][k][l] = value;
}
}
}
}
}
}
class FastOutput extends PrintWriter {
FastOutput() {
super(new BufferedOutputStream(System.out));
}
public void debug(Object...obj) {
System.err.println(Arrays.deepToString(obj));
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.io.IOException;
import java.util.*;
//import Library1.InputReader;
import java.text.DecimalFormat;
public class Codeforces {
public static long mod = (long)Math.pow(10, 9)+7 ;
public static double epsilon=0.00000000008854;//value of epsilon
public static InputReader sc = new InputReader(System.in);
public static PrintWriter pw = new PrintWriter(System.out);
public static long gcd(long a,long b){
while(b>0){
long t=b;
b=a%b;
a=t;
}
return a;
}
public static int firstNode(int a,int p[]){
if(a!=p[a]){
p[a]=firstNode(p[a], p);
}
return p[a];
}
public static void Union(int a,int b,int p[]){
//int a=firstNode(a1, p);
//int b=firstNode(b1, p);
/*if(a!=b){
if(r[a]<r[b]){
p[a]=b;
}
else if(r[a]>r[b]){
p[b]=a;
}
else{
r[b]++;
p[a]=b;
}
}*/
//if(a!=b)
p[firstNode(a,p)]=firstNode(b,p);//if no rank than
}
public static void main(String[] args) {
// code starts..
int n=sc.nextInt();
Integer a[][]=new Integer[n][3];
for(int i=0;i<n;i++){
a[i][0]=sc.nextInt();
a[i][1]=i;
}
Arrays.sort(a,pair());
int c=1;
for(int i=0;i<n;i++){
int t=i+1;
while(t<n&&a[i][0]-a[t][0]==0){
t++;
}
for(int j=t-1;j>=i;j--){
a[j][2]=c++;
}
i=t-1;
}
Arrays.sort(a,column(1));
for(int i=0;i<n;i++)
pw.print(a[i][2]+" ");
// Code ends...
pw.flush();
pw.close();
}
static class tripletL implements Comparable<tripletL> {
Long x, y, z;
tripletL(long x, long y, long z) {
this.x = x;
this.y = y;
this.z = z;
}
public int compareTo(tripletL o) {
int result = x.compareTo(o.x);
if (result == 0)
result = y.compareTo(o.y);
if (result == 0)
result = z.compareTo(o.z);
return result;
}
public boolean equlas(Object o) {
if (o instanceof tripletL) {
tripletL p = (tripletL) o;
return (x - p.x == 0) && (y - p.y ==0 ) && (z - p.z == 0);
}
return false;
}
public String toString() {
return x + " " + y + " " + z;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode() + new Long(z).hashCode();
}
}
public static String Doubleformate(double a,int n){
String s="";
while(n-->0){
s+='0';
}
DecimalFormat f =new DecimalFormat("#0."+s);
return f.format(a);
}
public static Comparator<Integer[]> column(int i){
return
new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
return o1[i].compareTo(o2[i]);//for ascending
//return o2[i].compareTo(o1[i]);//for descending
}
};
}
public static Comparator<Integer[]> pair(){
return
new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
int result=o1[0].compareTo(o2[0]);
if(result==0)
result=o1[1].compareTo(o2[1]);
return result;
}
};
}
public static Comparator<Integer[]> Triplet(){
return
new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
for(int i=0;i<3;i++){
for(int j=i+1;j<3;j++){
for(int k=0;k<3;k++){
for(int p=k+1;p<3;p++){
if((o1[i]==o2[k]&&o1[j]==o2[p])||(o1[j]==o2[k]&&o1[i]==o2[p])){
}
}
}
}
}
int result=o1[0].compareTo(o2[0]);
if(result==0)
result=o1[1].compareTo(o2[1]);
if(result==0)
result=o1[2].compareTo(o2[2]);
return result;
}
};
}
public static String reverseString(String s){
StringBuilder input1 = new StringBuilder();
input1.append(s);
input1 = input1.reverse();
return input1.toString();
}
public static int[] scanArray(int n){
int a[]=new int [n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
return a;
}
public static long[] scanLongArray(int n){
long a[]=new long [n];
for(int i=0;i<n;i++)
a[i]=sc.nextLong();
return a;
}
public static String [] scanStrings(int n){
String a[]=new String [n];
for(int i=0;i<n;i++)
a[i]=sc.nextLine();
return a;
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
class Bit{//1...n
int a[];
Bit(int n){
a=new int[n+1];
}
void update(int i,int delta){
while(i<a.length){
a[i]+=delta;
i+=i&(-i);
}
}
int query(int i){
int sum=0;
while(i>0){
sum+=a[i];
i-=i&(-i);
}
return sum;
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
#define ll long long int
#define mod 998244353
#define pb push_back
#define INF 1000000000000001
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
using namespace std;
void build(ll tree[],ll s[],ll l,ll r,ll node)
{
if(l==r)
{
tree[node] = s[l];
return;
}
ll mid = (l+r)>>1;
build(tree,s,l,mid,2*node);
build(tree,s,mid+1,r,2*node+1);
tree[node] = min(tree[2*node],tree[2*node+1]);
}
void update(ll tree[],ll start,ll end,ll x,ll l,ll r,ll node,ll lazy[])
{
if(lazy[node])
{
tree[node] += lazy[node];
if(start!=end && start<=end)
{
lazy[2*node] += lazy[node];
lazy[2*node+1] += lazy[node];
}
lazy[node] = 0;
}
if(start>r || l>end || l>r || start > end)
return;
if(start>=l && end<=r)
{
tree[node] += x;
if(start!=end && start<=end)
{
lazy[2*node] += x;
lazy[2*node+1] += x;
}
return;
}
ll mid = (start+end)>>1;
update(tree,start,mid,x,l,r,2*node,lazy);
update(tree,mid+1,end,x,l,r,2*node+1,lazy);
tree[node] = min(tree[2*node],tree[2*node+1]);
}
ll query(ll tree[],ll start,ll end,ll l,ll r,ll node,ll lazy[])
{
if(lazy[node])
{
tree[node] += lazy[node];
if(start!=end && start<=end)
{
lazy[2*node] += lazy[node];
lazy[2*node+1] += lazy[node];
}
lazy[node] = 0;
}
if(start>r || l>end || l>r || start > end)
return 1;
if(start>=l && r>=end)
return tree[node];
ll mid = (start+end)>>1;
return min(query(tree,start,mid,l,r,2*node,lazy),query(tree,mid+1,end,l,r,2*node+1,lazy));
}
int main()
{
//fast;
ll n;
cin>>n;
ll s[n],ans[n],tree[4*n+10],lazy[4*n+10]={},l,r,mid,val,cnt=0;
for(int i = 0; i<n; ++i) cin>>s[i];
build(tree,s,0,n-1,1);
for(ll k = 0; k < n; ++k)
{
l = 0, r = n - 1;
while(l<=r)
{
mid = (l+r)>>1;
if(query(tree,0,n-1,mid,r,1,lazy)==0)
{
l = mid + 1;
val = mid;
}
else
r = mid - 1;
cnt++;
if(cnt>19) cout<<"ERROR";
}
if(val>n-1) cout<<"ERROR";
ans[val] = k + 1;
update(tree,0,n-1,-k-1,val+1,n-1,1,lazy);
update(tree,0,n-1,INF,val,val,1,lazy);
}
for(int i = 0; i < n; ++i) cout << ans[i] << " ";
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <class V, class U>
struct SegTree {
int n;
vector<V> st;
vector<U> lazy;
vector<int> leaves;
SegTree(const int n) : n(n) {
leaves.resize(n);
init(1, 0, n - 1);
lazy.resize(st.size());
}
void init(const int si, const int lo, const int hi) {
if (lo == hi) {
if (si >= (int)st.size()) st.resize(si + 1);
st[si] = V(0, lo);
leaves[lo] = si;
} else {
const int mid = (lo + hi) >> 1;
init(si << 1, lo, mid);
init(si << 1 | 1, mid + 1, hi);
st[si] = V(st[si << 1], st[si << 1 | 1]);
}
}
void updateLazy(const int si, const int lo, const int hi) {
lazy[si].apply(st[si], lo, hi);
if (lo != hi) {
lazy[si << 1] = U(lazy[si << 1], lazy[si]);
lazy[si << 1 | 1] = U(lazy[si << 1 | 1], lazy[si]);
}
lazy[si] = U();
}
V query(const int l, const int r) {
return (l <= r && l < n && r >= 0) ? query(l, r, 1, 0, n - 1) : V();
}
V query(const int l, const int r, const int si, const int lo, const int hi) {
updateLazy(si, lo, hi);
if (l <= lo && hi <= r) return st[si];
const int mid = (lo + hi) >> 1;
if (r <= mid) return query(l, r, si << 1, lo, mid);
if (mid < l) return query(l, r, si << 1 | 1, mid + 1, hi);
return V(query(l, r, si << 1, lo, mid),
query(l, r, si << 1 | 1, mid + 1, hi));
}
void update(const int l, const int r, const U u) {
if (l <= r) update(l, r, u, 1, 0, n - 1);
}
void update(const int l, const int r, const U &u, const int si, const int lo,
const int hi) {
if (l <= lo && hi <= r) {
lazy[si] = U(lazy[si], u);
updateLazy(si, lo, hi);
} else {
updateLazy(si, lo, hi);
if (hi < l || r < lo) return;
const int mid = (lo + hi) >> 1;
update(l, r, u, si << 1, lo, mid);
update(l, r, u, si << 1 | 1, mid + 1, hi);
st[si] = V(st[si << 1], st[si << 1 | 1]);
}
}
vector<int> remAll(const int l, const int r) {
vector<int> res;
remAll(l, r, 1, 0, n - 1, res);
return res;
}
void remAll(const int l, const int r, const int si, const int lo,
const int hi, vector<int> &res) {
updateLazy(si, lo, hi);
if (!st[si] || hi < l || r < lo) return;
if (lo == hi) {
res.push_back(lo);
st[si] = 0;
} else {
const int mid = (lo + hi) >> 1;
remAll(l, r, si << 1, lo, mid, res);
remAll(l, r, si << 1 | 1, mid + 1, hi, res);
st[si] = V(st[si << 1], st[si << 1 | 1]);
}
}
void updateAllLazy() { updateAllLazy(1, 0, n - 1); }
void updateAllLazy(const int si, const int lo, const int hi) {
updateLazy(si, lo, hi);
if (lo != hi) {
const int mid = (lo + hi) >> 1;
updateAllLazy(si << 1, lo, mid);
updateAllLazy(si << 1 | 1, mid + 1, hi);
}
}
vector<V> retrieve() {
updateAllLazy();
vector<V> res(n);
for (int i = 0; i < n; ++i) res[i] = st[leaves[i]];
return res;
}
};
struct StVal {
pair<long long, long long> v;
StVal() {}
StVal(const int i, const int j) {
v.first = i;
v.second = j;
}
StVal(const StVal &v1, const StVal &v2) {
v = (v1.v.first >= v2.v.first) ? v2.v : v1.v;
}
};
struct StUpdate {
long long v = 0;
StUpdate() {}
StUpdate(const long long v) : v(v) {}
StUpdate(const StUpdate &u1, const StUpdate &u2) { v = u1 + u2; }
operator long long() const { return v; }
void apply(StVal &v, const int lo, const int hi) { v.v.first += this->v; }
};
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
SegTree<StVal, StUpdate> st(n);
for (int i = 0; i < n; i++) {
long long a;
cin >> a;
st.update(i, i, a);
}
vector<int> ans(n);
int k = 1;
for (int i = 0; i < n; i++) {
StVal p = st.query(0, n - 1);
ans[p.v.second] = k;
st.update(p.v.second, n - 1, -k);
st.update(p.v.second, p.v.second, (1 << 30));
k++;
}
for (int i = 0; i < n; i++) cout << ans[i] << " ";
cout << endl;
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.Map.Entry;
public class gym{
static class pair implements Comparable<pair>{
long n;int idx;
pair(long x,int y){
n=x;idx=y;
}
@Override
public int compareTo(pair o) {
int me=Arrays.binarySearch(nums, n);
int oo=Arrays.binarySearch(nums, o.n);
if(me!=oo) {
if(n>o.n)return 1;
return -1;
}
return idx-o.idx;
}
}
static long []nums;
public static void main(String[] args) throws Exception{
MScanner sc = new MScanner(System.in);
PrintWriter pw=new PrintWriter(System.out);
nums=new long[(int)2e5+1];
for(int i=1;i<nums.length;i++) {
nums[i]=(i*1l*((i-1)/2));
}
int n=sc.nextInt();
TreeSet<pair>t=new TreeSet<pair>();
for(int i=0;i<n;i++) {
t.add(new pair(sc.nextLong(),i));
}
int[]ans=new int[n];
for(int i=1;i<=n;i++) {
pair p=t.floor(new pair((i*1l*(i-1)/2),n));
t.remove(p);
ans[p.idx]=i;
}
for(int i:ans)pw.print(i+" ");
pw.flush();
}
static class MScanner {
StringTokenizer st;
BufferedReader br;
public MScanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public MScanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.util.Scanner;
public class ResP
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long ar1[]=new long[n];
long ar2[]=new long[n];
for(int x=0;x<n;x++)
{
ar1[x]=Long.parseLong(sc.next());
}
if(allZ(ar1))
{
for(int i=n;i>0;i--)
{
System.out.print(i);
if(i!=1)
System.out.print(" ");
}
}
else
{
for(int x=n-1;x>=0;x--)
{
if(x!=n-1 && ar1[x]==ar1[x+1])
{
ar2[x]=ar2[x+1]+1;
}
else
ar2[x]=(long)((1+(Math.sqrt(1+8*ar1[x])))/2.0);
}
for(int i=0;i<n;i++)
{
System.out.print(ar2[i]);
if(i!=n-1)
System.out.print(" ");
}
}
}
public static boolean allZ(long[] a)
{
int i=0;
while(i!=a.length)
{
if(a[i]!=0)
return false;
i++;
}
return true;
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.geom.*;
import static java.lang.Math.*;
public class Main implements Runnable
{
boolean multiiple = false;
class Pair implements Comparable<Pair>
{
long val;
int pos;
Pair(long a, int b)
{
val = a;
pos = b;
}
@Override
public int compareTo(Pair o)
{
if (val == o.val)
return o.pos - pos;
if (val - o.val > 0)
return 1;
if (val - o.val < 0)
return -1;
return 0;
}
}
void solve() throws Exception
{
int n = sc.nextInt();
PriorityQueue<Pair> pq = new PriorityQueue<>();
for (int i = 0; i < n; i++)
pq.add(new Pair(sc.nextLong(), i));
int count = 1;
int[] ans = new int[n];
while (!pq.isEmpty())
{
Pair next = pq.poll();
ans[next.pos] = count;
count++;
}
for (int an : ans)
{
System.out.print(an + " ");
}
}
@Override
public void run()
{
try
{
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
sc = new FastScanner(in);
if (multiiple)
{
int q = sc.nextInt();
for (int i = 0; i < q; i++)
solve();
}
else
solve();
}
catch (Throwable uncaught)
{
Main.uncaught = uncaught;
}
finally
{
out.close();
}
}
public static void main(String[] args) throws Throwable {
Thread thread = new Thread(null, new Main(), "", (1 << 26));
thread.start();
thread.join();
if (Main.uncaught != null) {
throw Main.uncaught;
}
}
static Throwable uncaught;
BufferedReader in;
FastScanner sc;
PrintWriter out;
}
class FastScanner
{
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in)
{
this.in = in;
}
public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import javafx.util.Pair;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
long s[];
int p[];
n = scanner.nextInt();
s = new long[n];
p = new int[n];
for(int i = 0; i < n; i++)
s[i] = scanner.nextLong();
SegmentTree segmentTree = new SegmentTree(s, n);
for(int i = 1; i <= n; i++){
int index = segmentTree.get(0, n-1).getKey();
p[index] =i;
segmentTree.delete_(index);
segmentTree.update_(index + 1, n-1, -1);
}
for(int x : p){
System.out.print(x + " ");
}
System.out.println();
}
}
class SegmentTree{
Pair<Integer, Long> t[];
long lazy[];
int size;
SegmentTree(long a[], int n){
t = new Pair[4*n];
size = n;
lazy = new long[4*n];
build(a, 1, 0, n-1);
}
void update_(int l, int r, long add){
update(1, 0, size-1, l, r, add);
}
void delete_(int n){
delete(1, 0, size-1, n);
}
private void delete(int v, int tl, int tr, int n){
if(tl == tr){
if(tl == n){
t[v] = new Pair<Integer, Long>(t[v].getKey(), Long.MAX_VALUE) ;
}else{
return;
}
}else{
push(v);
int tm = (tl + tr)/2;
delete(v*2, tl, tm, n);
delete(v*2 + 1, tm+1, tr, n);
if(t[v*2 + 1].getValue() == 0){
t[v] = t[v*2+1];
}else if(t[v*2].getValue() < t[v*2+1].getValue()){
t[v] = t[v*2];
}else{
t[v] = t[v*2 + 1];
}
}
}
Pair<Integer, Long> get(int l, int r){
return query(1, 0, size-1, l, r);
}
private void build(long a[], int v, int tl, int tr){
if (tl == tr) {
t[v] = new Pair<Integer, Long>(tl, a[tl]);
} else {
int tm = (tl + tr) / 2;
build(a, v*2, tl, tm);
build(a, v*2+1, tm+1, tr);
if(t[v*2 + 1].getValue() == 0){
t[v] = t[v*2+1];
}else if(t[v*2].getValue() < t[v*2+1].getValue()){
t[v] = t[v*2];
}else{
t[v] = t[v*2 + 1];
}
}
}
private void push(int v){
t[v*2] = new Pair<Integer, Long>(t[v*2].getKey() , t[v*2].getValue() + lazy[v]);
lazy[v*2] += lazy[v];
t[v*2 + 1] = new Pair<Integer, Long>(t[v*2 + 1].getKey() , t[v*2 + 1].getValue() + lazy[v]);
lazy[v*2+1] += lazy[v];
lazy[v] = 0;
}
private void update(int v, int tl, int tr, int l, int r, long addend){
if (l > r)
return;
if(l == tl && tr == r){
t[v] = new Pair<Integer, Long>(t[v].getKey(), t[v].getValue() + addend) ;
lazy[v] += addend;
}else{
push(v);
int tm = (tl + tr)/2;
update(v*2, tl, tm, l, Math.min(r, tm), addend);
update(v*2+1, tm+1, tr, Math.max(l, tm+1), r, addend);
if(t[v*2 + 1].getValue() == 0){
t[v] = t[v*2+1];
}else if(t[v*2].getValue() < t[v*2+1].getValue()){
t[v] = t[v*2];
}else{
t[v] = t[v*2 + 1];
}
}
}
private Pair<Integer, Long> query(int v, int tl, int tr, int l, int r){
if (l > r)
return new Pair<Integer, Long>(-1, Long.MAX_VALUE);
if (l <= tl && tr <= r)
return t[v];
push(v);
int tm = (tl + tr) / 2;
Pair<Integer, Long> t1 = query(v*2, tl, tm, l, Math.min(r, tm));
Pair<Integer, Long> t2 = query(v*2+1, tm+1, tr, Math.max(l, tm+1), r);
if(t2.getValue() <= t1.getValue()){
return t2;
}
return t1;
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class d {
public static void main(String[] args) throws IOException {
// Scanner s = new Scanner(System.in);
BufferedReader s=new BufferedReader(new InputStreamReader(System.in));
// String[] st=s.readLine().trim().split("\\s+");
// a[i]=Integer.parseInt(st[i]);
// Integer.parseInt(s.readLine().trim().split("\\s+");
StringBuilder sb = new StringBuilder();
int n=Integer.parseInt(s.readLine().trim().split("\\s+")[0]);
long[] a=new long[n];
String[] st=s.readLine().trim().split("\\s+");
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(st[i]);
}int j=n-1;long[] ans=new long[n];
for(int i=n-2;i>=0;i--){
if(a[i]==a[i+1]) continue;
long x=(long)(Math.sqrt(1+8*a[i+1])-1)/2+1;
while(j>i){
ans[j--]=x++;
}
}
long x=(long)(Math.sqrt(1+8*a[0])-1)/2+1;
while(j>-1){
ans[j--]=x++;
}
for(int i=0;i<n;i++) sb.append(ans[i]+" ");
System.out.println(sb.toString()
);
}
static String lexographicallysmallest(String s) {
if (s.length() % 2 == 1) return s;
String s1 =lexographicallysmallest(s.substring(0, s.length()/2));
String s2 = lexographicallysmallest(s.substring(s.length()/2, s.length()));
if (s1.compareTo(s2)<0) return s1 + s2;
else return s2 + s1;
}
public static int countSetBits(int n)
{
return (BitsSetTable256[n & 0xff]
+ BitsSetTable256[(n >> 8) & 0xff]
+ BitsSetTable256[(n >> 16) & 0xff]
+ BitsSetTable256[n >> 24]);
}
static int[] BitsSetTable256 ;
public static void initialize(int n)
{
BitsSetTable256[0] = 0;
for (int i = 0; i <=Math.pow(2,n); i++) {
BitsSetTable256[i] = (i & 1) + BitsSetTable256[i / 2];
}
}
static void dfs(int i,int val,ArrayList<Integer>[] adj){
}
static void computeLPSArray(String pat, int M, int lps[]) {
int len = 0;
int i = 1;
lps[0] = 0;
while (i < M) {
if (pat.charAt(i) == pat.charAt(len)) {
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0) {
len = lps[len - 1];
}
else
{
lps[i] = len;
i++;
}
}
}
}
static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static long powerwithmod(long x, long y, int p) {
long res = 1;
x = x % p;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long powerwithoutmod(long x, int y) {
long temp;
if( y == 0)
return 1;
temp = powerwithoutmod(x, y/2);
if (y%2 == 0)
return temp*temp;
else
{
if(y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
static void fracion(double x) {
String a = "" + x;
String spilts[] = a.split("\\."); // split using decimal
int b = spilts[1].length(); // find the decimal length
int denominator = (int) Math.pow(10, b); // calculate the denominator
int numerator = (int) (x * denominator); // calculate the nerumrator Ex
// 1.2*10 = 12
int gcd = (int) gcd((long) numerator, denominator); // Find the greatest common
// divisor bw them
String fraction = "" + numerator / gcd + "/" + denominator / gcd;
// System.out.println((denominator/gcd));
long x1 = modInverse(denominator / gcd, 998244353);
// System.out.println(x1);
System.out.println((((numerator / gcd) % 998244353 * (x1 % 998244353)) % 998244353));
}
static int bfs(int i1, ArrayList<Integer>[] h, int[] vis, int n,int val1) {
Queue<Integer> q = new LinkedList<Integer>();
q.add(i1);Queue<Integer> aq=new LinkedList<Integer>();
aq.add(0);
while(!q.isEmpty()){
int i=q.poll();
int val=aq.poll();
if(i==n){
return val;
}
if(h[i]!=null){
for(Integer j:h[i]){
if(vis[j]==0){
q.add(j);vis[j]=1;
aq.add(val+1);}
}
}
}return -1;
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long modInverse(long a, int m)
{
return (powerwithmod(a, m - 2, m));
}
static int MAXN;
static int[] spf;
static void sieve() {
spf[1] = 1;
for (int i=2; i<MAXN; i++)
spf[i] = i;
for (int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (int i=3; i*i<MAXN; i++)
{
if (spf[i] == i)
{
for (int j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}
static ArrayList<Integer> getFactorizationUsingSeive(int x) {
ArrayList<Integer> ret = new ArrayList<Integer>();
while (x != 1)
{
ret.add(spf[x]);
x = x / spf[x];
}
return ret;
}
static long[] fac ;
static void calculatefac(int mod){
fac[0]=1;
for (int i = 1 ;i <= MAXN; i++)
fac[i] = fac[i-1] * i % mod;
}
static long nCrModPFermat(int n, int r, int mod) {
if (r == 0)
return 1;
fac[0] = 1;
return (fac[n]* modInverse(fac[r], mod)
% mod * modInverse(fac[n-r], mod)
% mod) % mod;
} }
class Student {
long l;long r;int x;
public Student(long l, long r) {
this.l = l;
this.r = r;this.x=x;
}
public String toString()
{
return this.l+" ";
}
}
class Sortbyroll implements Comparator<Student>
{
public int compare(Student a, Student b){
if(a.l<b.l) return 1;
else if(a.l==b.l){
if(a.r==b.r){
return 0;
}
if(a.r<b.r) return -1;
return 1;}
return -1; }
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
import sys
input=sys.stdin.buffer.readline
def updates(pos,seg_tree,usg):
seg_tree[usg+par[pos-usg+1]-1]=0
pos=pos>>1
while(pos>0):
seg_tree[pos]=seg_tree[pos<<1]+seg_tree[(pos<<1)+1]
pos=pos>>1
def queries(value,usg):
i=1
j=usg
k=1
while(True):
if(value==seg_tree[k]):
return j+1
elif(value<seg_tree[k]):
j=(i+j)//2
else:
k+=1
swap=j
j=2*j-i+1
i=swap+1
value=value-seg_tree[k-1]
j=(i+j)//2
k=k<<1
def find(x):
if(par[x]==x):
return x
par[x]=find(par[x])
return par[x]
def union(a,b):
xa=find(a)
xb=find(b)
if(xa!=xb):
par[xa]=par[xb]
n=int(input())
a=list(map(int,input().split()))
par=[i for i in range(n+2)]
i=1
while((1<<i)<n):
i+=1
usg=1<<i
seg_tree=[0 for i in range(1<<(i+1))]
count=1
for i in range(usg,usg+n):
seg_tree[i]=count
count+=1
for i in range(usg-1,0,-1):
seg_tree[i]=seg_tree[i<<1]+seg_tree[(i<<1)+1]
stack=[]
for i in range(n-1,-1,-1):
if(a[i]==0):
stack.append(find(par[1]))
union(1,par[1]+1)
continue
r=queries(a[i],usg)
if(a[i]==15):
print(r,'h',r+usg-1)
r=find(r)
updates(r+usg-1, seg_tree,usg)
stack.append(find(par[r]))
union(r,par[r]+1)
print(*stack[::-1])
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize "O3"
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 5;
const int M = 1 << 18;
pair<long long, int> tree[M * 2 + 5];
long long lazy[M * 2 + 5];
int n;
void push(int v) {
tree[v * 2].first += lazy[v];
tree[v * 2 + 1].first += lazy[v];
lazy[v * 2] += lazy[v];
lazy[v * 2 + 1] += lazy[v];
lazy[v] = 0;
}
void add(int l, int r, long long val, int v = 1, int le = 1, int re = M) {
if (l > r || l > re || r < le) return;
if (le >= l && re <= r) {
tree[v].first += val;
lazy[v] += val;
return;
}
int mid = (le + re) / 2;
push(v);
add(l, r, val, v * 2, le, mid);
add(l, r, val, v * 2 + 1, mid + 1, re);
tree[v] = min(tree[v * 2], tree[v * 2 + 1]);
}
pair<long long, int> query(int l, int r, int v = 1, int le = 1, int re = M) {
if (l > r || l > re || r < le) return {INF, INF};
if (le >= l && re <= r) return tree[v];
int mid = (le + re) / 2;
push(v);
auto ret =
min(query(l, r, v * 2, le, mid), query(l, r, v * 2 + 1, mid + 1, re));
tree[v] = min(tree[v * 2], tree[v * 2 + 1]);
return ret;
}
void solve() {
int pom;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> tree[i + M - 1].first;
tree[i + M - 1].second = {-i};
}
for (int i = M - 1; i >= 1; i--) tree[i] = min(tree[i * 2], tree[i * 2 + 1]);
for (int i = 1; i <= n; i++) {
pom = -query(1, n).second;
add(pom, pom, (long long)n * n);
add(pom + 1, n, -pom);
cout << pom << ' ';
}
cout << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.TreeSet;
import java.util.ArrayList;
import java.util.List;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
static class TaskD {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.i();
int[] a = in.ia(n);
TreeSet<Integer> set = new TreeSet<>();
Fenwick f = new Fenwick(n);
for (int i = 1; i <= n; i++) {
set.add(i);
f.add(i, i);
}
List<Integer> list = new ArrayList<>();
for (int i = n - 1; i >= 0; i--) {
int ind = f.indexWithGivenCumFreq(a[i]);
Integer u = set.ceiling(ind);
f.add(u, -u);
list.add(u);
set.remove(u);
}
Collections.reverse(list);
list.forEach(e -> out.print(e + " "));
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void close() {
writer.close();
}
}
static class InputReader {
private InputStream is;
private byte[] inbuf = new byte[1024];
private int lenbuf = 0;
private int ptrbuf = 0;
public InputReader(InputStream is) {
this.is = is;
}
private int readByte() {
if (lenbuf == -1) throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) return -1;
}
return inbuf[ptrbuf++];
}
public int[] ia(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = i();
return a;
}
public int i() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
}
static class Fenwick {
public final long[] bit;
public final int size;
public Fenwick(int[] a) {
this(a.length);
for (int i = 0; i < a.length; i++) {
this.add(i, a[i]);
}
}
public Fenwick(long[] a) {
this(a.length);
for (int i = 0; i < a.length; i++)
this.add(i, a[i]);
}
public Fenwick(int size) {
bit = new long[size + 1];
this.size = size + 1;
}
public void add(int i, long delta) {
for (++i; i < size; i += (i & -i)) {
bit[i] += delta;
}
}
public int indexWithGivenCumFreq(long v) {
int i = 0, n = size;
for (int b = Integer.highestOneBit(n); b != 0; b >>= 1) {
if ((i | b) < n && bit[i | b] <= v) {
i |= b;
v -= bit[i];
}
}
return i;
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DRestorePermutation solver = new DRestorePermutation();
solver.solve(1, in, out);
out.close();
}
static class DRestorePermutation {
public void solve(int testNumber, FastReader s, PrintWriter out) {
int n = s.nextInt();
int[] arr = s.nextIntArray(n);
int[] ans = new int[n];
DRestorePermutation.SegmentTree st = new DRestorePermutation.SegmentTree(n);
for (int i = 0; i < n; i++) {
st.increment(i, i, arr[i]);
}
for (int i = 1; i <= n; i++) {
st.minimum(0, n - 1);
int ans1 = (int) st.ans[1];
ans[ans1] = i;
st.increment(ans1, n - 1, i * -1);
st.increment(ans1, ans1, Long.MAX_VALUE - 1);
}
out.println(DRestorePermutation.arrays.printArr(ans));
}
private static class arrays {
static StringBuilder printArr(int[] arr) {
StringBuilder ans = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
ans.append(arr[i] + " ");
}
return ans;
}
}
private static class SegmentTree {
long[] lo;
long[] hi;
long[] min;
long[] delta;
long[] ans;
int n;
public SegmentTree(int n) {
this.n = n;
this.hi = new long[4 * n + 1];
this.lo = new long[4 * n + 1];
this.min = new long[4 * n + 1];
this.delta = new long[4 * n + 1];
this.ans = new long[4 * n + 1];
init(1, 0, n - 1);
}
void prop(int i) {
delta[2 * i] += delta[i];
delta[2 * i + 1] += delta[i];
delta[i] = 0;
}
void update(int i) {
if (min[2 * i + 1] + delta[2 * i + 1] <= min[2 * i] + delta[2 * i]) {
min[i] = min[2 * i + 1] + delta[2 * i + 1];
ans[i] = ans[2 * i + 1];
} else {
min[i] = min[2 * i] + delta[2 * i];
ans[i] = ans[2 * i];
}
// min[i] = Math.min(min[2 * i + 1] + delta[2 * i + 1], min[2 * i] + delta[2 * i]);
}
void increment(int a, int b, long val) {
increment(1, a, b, val);
}
private void increment(int i, int a, int b, long val) {
if (a > hi[i] || b < lo[i]) {
return;
}
//Fully overlap case.
if (a <= lo[i] && hi[i] <= b) {
delta[i] += val;
return;
}
//Partial overlap case.
prop(i);
increment(2 * i, a, b, val);
increment(2 * i + 1, a, b, val);
update(i);
}
private long minimum(int a, int b) {
return minimum(1, a, b);
}
private long minimum(int i, int a, int b) {
if (a > hi[i] || b < lo[i]) {
return Long.MAX_VALUE;
}
if (a <= lo[i] && hi[i] <= b) {
return min[i] + delta[i];
}
prop(i);
long minLeft = minimum(2 * i, a, b);
long minRight = minimum(2 * i + 1, a, b);
update(i);
return Math.min(minLeft, minRight);
}
private void init(int i, int from, int to) {
lo[i] = from;
hi[i] = to;
if (from == to) {
ans[i] = from;
return;
}
int mid = (from + to) / 2;
init(2 * i, from, mid);
init(2 * i + 1, mid + 1, to);
}
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Codeforce {
static final long INF = (int) 1e9;
static final int MAXN = (int) (2e6 + 10);
static long[] TREE = new long[MAXN * 4];
static long[] LAZY = new long[MAXN * 4];
static int[] ANS = new int[MAXN];
public static void main(String[] args) {
solve();
}
static void update1(int left, int right, int idx, long val, int pos) {
if (left > idx || right < idx) {
return;
}
if (left == right) {
TREE[pos] = val;
return;
}
int mid = (left + right) / 2;
update1(left, mid, idx, val, 2 * pos + 1);
update1(mid + 1, right, idx, val, 2 * pos + 2);
TREE[pos] = Math.min(TREE[2 * pos + 1], TREE[2 * pos + 2]);
}
static void update2(int left, int right, int qleft, int qright, long val, int pos) {
if (left > qright || right < qleft) {
return;
}
if (left >= qleft && right <= qright) {
TREE[pos] += val;
LAZY[pos] += val;
return;
}
int mid = (left + right) / 2;
update2(left, mid, qleft, qright, val, 2 * pos + 1);
update2(mid + 1, right, qleft, qright, val, 2 * pos + 2);
TREE[pos] = Math.min(TREE[2 * pos + 1], TREE[2 * pos + 2]);
}
static int query(int left, int right, int pos) {
if (left == right) {
return left;
}
TREE[2 * pos + 1] += LAZY[pos];
TREE[2 * pos + 2] += LAZY[pos];
LAZY[2 * pos + 1] += LAZY[pos];
LAZY[2 * pos + 2] += LAZY[pos];
LAZY[pos] = 0;
int mid = (left + right) / 2;
if (TREE[2 * pos + 2] == 0) {
return query(mid + 1, right, 2 * pos + 2);
} else {
return query(left, mid, 2 * pos + 1);
}
}
static void solve() {
FastScanner sc = new FastScanner();
int n = sc.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
}
for (int i = 0; i < n; i++) {
update1(0, n - 1, i, arr[i], 0);
}
for (int i = 1; i <= n; i++) {
int zero_pos = query(0, n - 1, 0);
ANS[zero_pos] = i;
update1(0, n - 1, zero_pos, INF, 0);
update2(0, n - 1, zero_pos + 1, n - 1, -i, 0);
}
for (int i = 0; i < n-1; i++) {
System.out.print(ANS[i]+" ");
}
System.out.print(ANS[n-1]);
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
if (System.getProperty("ONLINE_JUDGE") == null) {
// Input is a file
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("input.txt");
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
} else {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
n = int(input())
s = [*map(int, input().split())]
ans = [-1] * n
from collections import defaultdict
idx = defaultdict(list)
for i in range(n):
idx[s[i]].append(i)
looking = 0
toadd = 1
while toadd <= n:
updatelooking = 0
for i in idx[looking][::-1]:
ans[i] = toadd
updatelooking += toadd
toadd += 1
looking += updatelooking
print(ans)
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Codeforce {
static final long INF = (int) 1e15;
static final int MAXN = (int) (2e6 + 10);
static long[] TREE = new long[MAXN * 4];
static long[] LAZY = new long[MAXN * 4];
static int[] ANS = new int[MAXN];
public static void main(String[] args) {
solve();
}
static void update1(int left, int right, int idx, long val, int pos) {
if (left > idx || right < idx) {
return;
}
if (left == right) {
TREE[pos] = val;
return;
}
int mid = (left + right) / 2;
update1(left, mid, idx, val, 2 * pos + 1);
update1(mid + 1, right, idx, val, 2 * pos + 2);
TREE[pos] = Math.min(TREE[2 * pos + 1], TREE[2 * pos + 2]);
}
static void update2(int left, int right, int qleft, int qright, long val, int pos) {
if (left > qright || right < qleft) {
return;
}
if (left >= qleft && right <= qright) {
TREE[pos] += val;
LAZY[pos] += val;
return;
}
int mid = (left + right) / 2;
update2(left, mid, qleft, qright, val, 2 * pos + 1);
update2(mid + 1, right, qleft, qright, val, 2 * pos + 2);
TREE[pos] = Math.min(TREE[2 * pos + 1], TREE[2 * pos + 2]);
}
static int query(int left, int right, int pos) {
if (left == right) {
return left;
}
TREE[2 * pos + 1] += LAZY[pos];
TREE[2 * pos + 2] += LAZY[pos];
LAZY[2 * pos + 1] += LAZY[pos];
LAZY[2 * pos + 2] += LAZY[pos];
LAZY[pos] = 0;
int mid = (left + right) / 2;
if (TREE[2 * pos + 2] == 0) {
return query(mid + 1, right, 2 * pos + 2);
} else {
return query(left, mid, 2 * pos + 1);
}
}
static void solve() {
// FastScanner sc = new FastScanner();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
}
for (int i = 0; i < n; i++) {
update1(0, n - 1, i, arr[i], 0);
}
for (int i = 1; i <= n; i++) {
int zero_pos = query(0, n - 1, 0);
ANS[zero_pos] = i;
update1(0, n - 1, zero_pos, INF, 0);
update2(0, n - 1, zero_pos + 1, n - 1, -i, 0);
}
for (int i = 0; i < n-1; i++) {
System.out.print(ANS[i]+" ");
}
System.out.print(ANS[n-1]);
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
if (System.getProperty("ONLINE_JUDGE") == null) {
// Input is a file
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("input.txt");
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
} else {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long sum(long long n) { return n * (n + 1) / 2; }
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
long long S[n];
long long A[n];
for (long long i = 0; i < n; i++) {
cin >> S[i];
}
long long cnt = 0;
for (long long i = n - 1; i > -1; i--) {
if (S[i] == sum(cnt)) {
cnt++;
A[i] = cnt;
while ((i > 0) && (S[i - 1] == S[i])) {
cnt++;
A[i - 1] = cnt;
i--;
}
i = n;
}
if (cnt == n) break;
}
for (long long i = 0; i < n; i++) cout << A[i] << " ";
cout << "\n";
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author JENSIH
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DRestorePermutation solver = new DRestorePermutation();
solver.solve(1, in, out);
out.close();
}
static class DRestorePermutation {
public void solve(int testNumber, ScanReader in, PrintWriter out) {
int n = in.scanInt();
int ar[] = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = in.scanInt();
}
SegLazy ss = new SegLazy(n);
int ans[] = new int[n];
for (int i = 0; i < n; i++) ss.update(i, i, +ar[i]);
for (int i = 1; i <= n; i++) {
int pos = ss.getlastZERO();
ans[pos] = i;
ss.update(pos, pos, Integer.MAX_VALUE / 3);
ss.update(pos + 1, n - 1, -i);
}
for (int i = 0; i < n; i++) {
out.print(ans[i] + " ");
}
}
public class SegLazy {
int n;
long[] tree;
long[] lazy;
public SegLazy(int n) {
this.n = n;
this.tree = new long[(4 * n) + 5];
this.lazy = new long[(4 * n) + 5];
}
public void update(int l, int r, long val) {
update(1, 0, n - 1, l, r, val);
}
public int getlastZERO() {
return qu(1, 0, n - 1);
}
private int qu(int node, int start, int end) {
if (start == end) return start;
else {
LAZY(node, start, end);
int mid = (start + end) / 2;
int res = -1;
LAZY(node, start, end);
LAZY(node * 2, start, mid);
LAZY((node * 2) + 1, mid + 1, end);
if (tree[(node * 2) + 1] == 0) {
res = qu((node * 2) + 1, mid + 1, end);
} else {
res = qu((node * 2), start, mid);
}
LAZY(node, start, end);
LAZY(node * 2, start, mid);
LAZY((node * 2) + 1, mid + 1, end);
tree[node] = Math.min(tree[node * 2], tree[(node * 2) + 1]);
return res;
}
}
private void update(int node, int start, int end, int l, int r, long val) {
LAZY(node, start, end);
if (start > end || start > r || end < l) return;
if (start >= l && end <= r) {
tree[node] += val;
LAZY(node, start, end);
if (start != end) {
lazy[node * 2] += val;
lazy[(node * 2) + 1] += val;
}
return;
}
int mid = (start + end) / 2;
update(node * 2, start, mid, l, r, val);
update((node * 2) + 1, mid + 1, end, l, r, val);
LAZY(node, start, end);
LAZY(node * 2, start, mid);
LAZY((node * 2) + 1, mid + 1, end);
tree[node] = Math.min(tree[node * 2], tree[(node * 2) + 1]);
}
private void LAZY(int node, int start, int end) {
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (start != end) {
lazy[node * 2] += lazy[node];
lazy[(node * 2) + 1] += lazy[node];
}
lazy[node] = 0;
}
}
}
}
static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int INDEX;
private BufferedInputStream in;
private int TOTAL;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() {
if (INDEX >= TOTAL) {
INDEX = 0;
try {
TOTAL = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (TOTAL <= 0) return -1;
}
return buf[INDEX++];
}
public int scanInt() {
int I = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
I *= 10;
I += n - '0';
n = scan();
}
}
return neg * I;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
from functools import cmp_to_key
def cmd(l, r):
if l[1] < r[1]:
return -1
elif l[1] > r[1]:
return 1
else:
if l[0] > r[0]:
return -1
elif l[0] < r[0]:
return 1
else:
return 0
n = int(input())
s = map(int, input().split())
s = list(enumerate(s))
s = sorted(s, key=cmp_to_key(cmd))
output = [0] * n
for i in range(n):
output[s[i][0]] = i + 1
print(' '.join(map(str, output)))
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool mini(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool maxi(T &a, T b) {
return a < b ? (a = b, true) : false;
}
const int N = 2e5 + 5;
int n, p[N], ans[N];
long long bit[N];
void up(int i) {
assert(i > 0);
for (long long val = i; i <= n; i += i & -i) bit[i] += val;
}
long long get(int i) {
long long res = 0;
for (; i > 0; i -= i & -i) res += bit[i];
return res;
}
inline void sol() {
cin >> n;
for (int i = 0, _ = (n); i < _; i++) cin >> p[i];
for (int i = (int)(n)-1; i >= 0; --i) {
long long l = 0, r = n - 1, mid;
while (l < r) {
mid = (l + r + 1) >> 1;
if (mid * (mid + 1) / 2 > get(mid) + p[i])
r = mid - 1;
else
l = mid;
}
up(ans[i] = l + 1);
}
for (int i = 0, _ = (n); i < _; i++) cout << ans[i] << ' ';
cout << "\n";
}
signed main() {
{ ios_base::sync_with_stdio(false), cin.tie(NULL); };
cout.precision(10);
cout << fixed;
sol();
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 200005;
int n;
long long s[Maxn];
long long BIT[Maxn];
int res[Maxn];
void Insert(int ind, long long val) {
for (int i = ind; i <= n; i += i & -i) BIT[i] += val;
}
long long Get(int ind) {
long long res = 0;
for (int i = ind; i > 0; i -= i & -i) res += BIT[i];
return res;
}
int main() {
scanf("%d", &n);
set<int> S;
for (int i = 1; i <= n; i++) {
Insert(i, i);
S.insert(i);
}
for (int i = 0; i < n; i++) scanf("%I64d", &s[i]);
for (int i = n - 1; i >= 0; i--) {
int lef = 1, rig = n;
while (lef <= rig) {
int mid = lef + rig >> 1;
if (Get(mid - 1) >= s[i])
rig = mid - 1;
else
lef = mid + 1;
}
set<int>::iterator it = S.lower_bound(rig + 1);
res[i] = *it;
Insert(*it, res[i]);
S.erase(it);
}
for (int i = 0; i < n; i++) printf("%d%c", res[i], i + 1 < n ? ' ' : '\n');
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
class DualBIT():
def __init__(self, n):
self.n = n
self.bit = [0] * (n + 1)
def get(self, i):
'''i番目の要素を取得'''
i = i + 1
s = 0
while i <= self.n:
s += self.bit[i]
i += i & -i
return s
def _add(self, i, x):
while i > 0:
self.bit[i] += x
i -= i & -i
def add(self, i, j, x):
'''[i, j)の要素にxを加算する'''
self._add(j, x)
self._add(i, -x)
n = int(input())
a = list(map(int, input().split()))
bit = DualBIT(n+1)
for i in range(1, n):
bit.add(i+1, n+1, i)
li = []
while True:
if not a:
break
ok = n
ng = 0
num = a[-1]
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if bit.get(mid) >= num:
ok = mid
else:
ng = mid
tmp = ok
bit.add(ok + 1, n+1, -ok)
bit.add(ok, ok + 1, -0.01)
li.append(tmp)
del a[-1]
print(*li[::-1])
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
long long n;
cin >> n;
vector<long long> v(n), ans(n);
set<pair<long long, long long> > st;
long long pos1 = 0;
for (long long i = 0; i < n; i++) {
cin >> v[i];
if (v[i] == 0) {
pos1 = max(pos1, i);
}
st.insert({v[i], i});
}
long long ipos = pos1;
for (long long i = 1; i <= n; i++) {
st.erase({v[ipos], ipos});
ans[ipos] = i;
if (i == n) break;
long long nxt = v[ipos] + i;
nxt = max(nxt, st.begin()->first);
auto it = st.upper_bound({nxt + 1, -1});
it--;
ipos = it->second;
}
for (auto u : ans) {
cout << u << " ";
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace __gnu_pbds;
using namespace std;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
tree_order_statistics_node_update>
new_data_set;
//defines
typedef long long ll;
typedef long double ld;
#define TIME clock() * 1.0 // CLOCKS_PER_SEC
#define prev _prev
#define y0 _y0
#define kill _kill
#define MAX 10000
#define pie 3.1415926535898
// Computes the parity of the number n (64-Bit) i.e. parity is 0 if the number of set-bits is even otherwise odd.
#define COMPUTE_PARITY(number) __builtin_parity(number)
// Computes the number of leading zeroes in the binary representation of n (64-Bit). The value will be 64 - (number of bits required to represent n).
#define COMPUTE_LEADING_ZEROES(number) __builtin_clzll(number)
// Computes the number of trailing zeroes in the binary representation of n (64-Bit). The value will always be zero if n is odd.
#define COMPUTE_TRAILING_ZEROES(number) __builtin_ctzll(number)
//permanent constants
const ld pi = acos(-1.0);
const int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int digarr[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int dxo[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dyo[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
const int alf = 26;
const int dig = 10;
const int two = 2;
const int th = 3;
const ll prost = 239;
const ll btc = 30;
const ld eps = 1e-10;
const ll INF = (ll)(1e18 + 239);
const int BIG = (int)(1e9 + 239);
const long long MOD = (long long)(1e9 + 7); //(int)(1e9 + 7); //998244353;
const ll MOD2 = (ll)MOD * (ll)MOD;
//random
mt19937 rnd(239); //(chrono::high_resolution_clock::now().time_since_epoch().count());
//constants
const int M = (int)(2e5 + 239);
const int N = (int)(2e3 + 239);
const int L = 20;
const int T = (1 << 20);
const int B = 500;
const int X = 1e4 + 239;
//for finding trailing zeros in factorial of a number like zero in 5!(120) is 1
ll findTrailingZeros(ll n)
{
ll count = 0;
for (ll i = 5; n / i >= 1; i *= 5)
count += n / i;
return count;
}
ll power(ll aa, ll nn)
{
ll res = 1; // Initialize result
aa = aa % MOD; // Update 'a' if 'a' >= p
while (nn > 0)
{
// If n is odd, multiply 'a' with result
if (nn & 1)
res=(res*aa) % MOD;
// n must be even now
nn = nn>>1; // n = n/2
aa= (aa*aa)%MOD;
}
return res;
}
ll gcd(ll a, ll b)
{
if(a < b)
return gcd(b, a);
else if(a%b == 0)
return b;
else return gcd(b, a%b);
}
ll fact(ll n)
{
if (n <= 1)
return 1;
return n * fact(n - 1);
}
ll nPr(ll n, ll r)
{
return fact(n) / fact(n - r);
}
ll nCr(ll n, ll r)
{
return fact(n) / (fact(r) * fact(n - r));
}
ll nCrModp(ll n, ll r, ll p)
{
long long C[r+1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (long long i = 1; i <= n; i++)
{
for (long long j = min(i, r); j > 0; j--)
C[j] = (C[j] + C[j-1])%p;
}
return C[r];
}
/*// If n is prime, then always returns true, If n is
// composite than returns false with high probability
// Higher value of k increases probability of correct
// result.
int isPrime2(long long n, long long k)
{
// Corner cases
if (n <= 1 || n == 4) return 0;
if (n <= 3) return 1;
// Try k times
while (k>0)
{
// Pick a random number in [2..n-2]
// Above corner cases make sure that n > 4
long long a = 2 + rand()%(n-4);
// Checking if a and n are co-prime
if (gcd(n, a) != 1)
return 0;
// Fermat's little theorem
if (power(a, n-1, n) != 1)
return 0;
k--;
}
return 1;
for(long long i=2;i<=sqrt(n);i++)
{
if(n%i==0)return 0;
}return 1;
} */
ll isPrime1(ll n)
{
if (n <= 1)
return 0;
if (n==2 || n==3)
return 1;
if (n % 2 == 0 || n % 3 == 0)
return 0;
for (ll i = 5; i * i<=n; i+=6)
if (n % i == 0 || n % (i + 2) == 0)
return 0;
return 1;
}
// An interval has start time and end time
struct Interval {
long roll,start, end;
};
/*// Compares two intervals according to staring times.
bool compareInterval(Interval a, Interval b) {
if (a.end == b.end) return a.start < b.start;
return a.end < b.end;
}*/
// Compares numbers taken in pair (Final Sorting)
bool compareInterval(Interval a, Interval b)
{
if(a.end!=b.end)return a.end<b.end;
else if(a.end==b.end and a.start!=b.start)
return (a.start > b.start);
else if(a.start==b.start) return a.roll>b.roll;
// else return a.end<b.end;
}
/*
int n,matches=1;cin>>n;vector<Interval> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i].start >> v[i].end;
v[i].end+=v[i].start;
}
sort(v.begin(),v.end(),compareInterval);
long prev = v[0].end;
for (int i = 1; i < n; i++) {
if (v[i].start > prev) {
prev = v[i].end;
matches++ ;
}
}
*/
//countsquares between range or having odd number of divisors
ll countSquares(ll a,ll b)
{
return (floor(sqrt(b)) - ceil(sqrt(a)) + 1);
}
ll OR(ll v[], ll n)
{
ll ans =v[0];
for (ll i = 1; i < n; i++)
{ans=(ans|v[i])-v[i];
if(ans<0){
ans=0;break;
}
}
return ans;
}
//return powers of 2 (2^0+2^1+2^2+...... and this function returns 0+1+2+.....)
ll block(ll x)
{
vector<ll> v;
ll sum=0,count=0;
// Converting the decimal number
// into its binary equivalent.
while (x > 0)
{
v.push_back(x % 2);
//sum+=x%2;
if(x%2==1)count++;
x = x / 2;
}
/*// Displaying the output when
// the bit is '1' in binary
// equivalent of number.
for (long long i = 0; i < v.size(); i++)
{
if (v[i] == 1)
{
sum+=i;
// if (i != v.size() - 1)
// cout << ", ";
}
} */
//cout << endl;
return count;
}
//long int l=10000;
// return greatest possible number we can make placing all of them together
// [3,9,22,0] -> 93220
bool cmp(long long a,long long b)
{
return to_string(a)+to_string(b)>to_string(b)+to_string(a);
}
//LEFT SHIFT AND RIGHT SHIFT IN CASE OF STRING
string leftrotate(string s, ll d)
{
reverse(s.begin(), s.begin()+d);
reverse(s.begin()+d, s.end());
reverse(s.begin(), s.end());
return s;
}
string rightrotate(string s, ll d)
{
return leftrotate(s, s.length()-d);
}
//BACKSPACE STRING
string newString(string S)
{
deque<char> q;
ll h=0;
for (int i = S.size()-1; i >=0; i--) {
if (S[i] != '#' && h==0)
q.push_back(S[i]);
else if(S[i]=='#')h++;
else if(S[i]!='#' && h!=0)h--;
}
string ans = "";
while (!q.empty()) {
ans += q.front();
q.pop_front();
}
reverse(ans.begin(),ans.end());
return ans;
}
//Maximum Subarray Sum
ll maxSubArraySum(vector<ll>&a, ll size)
{
ll max_so_far = INT_MIN, max_ending_here = 0;
for (long long i = 0; i < size; i++)
{
max_ending_here=max(max_ending_here+a[i],a[i]);
max_so_far=max(max_so_far,max_ending_here);
}
return max_so_far;
}
ld eps1 = 1.0E-14;
//LONGEST COMMON SUBSEQUENCE LENGTH(NOT CONTIGUOUS)
ll longestCommonSubsequence(string text1, string text2) {
ll a=text1.size(),b=text2.size();
ll lcs[a+1][b+1];
for(ll i=0;i<=a;i++)
{
for(ll j=0;j<=b;j++)
{
if(i==0 or j==0)lcs[i][j]=0;
else if(text1[i-1]==text2[j-1])
{
lcs[i][j]=1+lcs[i-1][j-1];
}
else
{
lcs[i][j]=max(lcs[i-1][j],lcs[i][j-1]);
}
}
}
return lcs[a][b];
}
ll ispal(string a)
{
string b=a;
reverse(b.begin(),b.end());
return (b==a)?1:0;
}
ll a,b,c,n,m,k;
ll t=1;
bool isPower(ll x, ll y)
{
// logarithm function to calculate value
ll res1 = log(y) / log(x);
ld res2 = log(y) / log(x); // Note : this is double
// compare to the result1 or result2 both are equal
return (res1 == res2);
}
ll calc(ll x,ll y)
{
ll x1=(log2(x))+1LL,y1=(log2(y))+1LL;
ll xx=x,yy=y;
x=(x<<y1);
y=(y<<x1);
y+=xx;x+=yy;
return abs(y-x);
}
void solve()
{
cin>>n;
vector<pair<ll,ll>>v(n);
vector<ll>ans(n);
for(ll i=0;i<n;i++)cin>>v[i].first,v[i].second=i;
sort(v.begin(),v.end());
a=1;
for(ll i=0;i<n;i++)
{
ans[v[i].second]=a;a++;
}
for(auto p:ans)cout<<p<<" ";
if(t>0)cout<<"\n";
}
int32_t main()
{
#ifdef ONPC
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//cin>>t;
while(t--)
{
solve();
}
//cout<<sum;
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
// List<Integer> sr = Arrays.asList(0, 1, 1, 1, 10);
// int n = 5;
long[] arr = new long[n+1];
long[] fw = new long[n+1];
for(int i=1;i<=n;++i) {
arr[i] = s.nextLong();
}
for(int i=1;i<=n;++i) fw[i] = 0;
for(int i=1;i<=n;++i) {
fw[i] += i;
if((i + (i & (-i))) <= n)
fw[i + (i & (-i))] += fw[i];
}
for(int i=n; i>=1; --i) {
int l = 0, r = n;
while(l < r) {
int mid = (l + r )/2;
long tp = 0;
int j = mid;
while(j > 0) {
tp += fw[j];
j -= j&(-j);
}
System.out.println(l + " " + r + " " + mid + " " + tp);
if(tp <= arr[i]) l = mid + 1;
else r = mid ;
}
arr[i] = l;
addfw(l, -l, n, fw);
}
for(int i=1; i<=n; ++i)
System.out.print(arr[i] + " ");
}
private static void addfw(int a, int b, int n, long[] fw) {
while((a) <= n) {
fw[a] += b;
a += a&(-a);
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T>
bool chmax(T &a, const T &b) {
return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
return (b < a) ? (a = b, 1) : 0;
}
template <class C>
void print(const C &c, std::ostream &os = std::cout) {
std::copy(std::begin(c), std::end(c),
std::ostream_iterator<typename C::value_type>(os, " "));
os << std::endl;
}
template <typename T = int>
struct BinaryIndexedTree {
int n;
vector<T> bit;
BinaryIndexedTree(int n_, T init = 0) : n(n_), bit(n_ + 1, init) {}
BinaryIndexedTree(vector<T> init) : n(init.size() + 1), bit(init.size() + 1) {
for (int i = 1; i < init.size() + 1; ++i) {
bit[i] = init[i - 1];
}
}
T sum(int i) {
i++;
T s = bit[0];
for (int x = i; x > 0; x -= (x & -x)) s += bit[x];
return s;
}
void add(int i, T a) {
i++;
if (i == 0) return;
for (int x = i; x <= n; x += (x & -x)) bit[x] += a;
}
int lower_bound(int w) {
w++;
if (w <= 0) return 0;
int x = 0, r = 1;
while (r < n) r <<= 1;
for (int k = r; k > 0; k >>= 1) {
if (x + k <= n && bit[x + k] < w) {
w -= bit[x + k];
x += k;
}
}
return x + 1;
}
T query(int l, int r) { return sum(r - 1) - sum(l - 1); }
};
int main() {
ll n;
cin >> n;
vl s(n);
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
deque<int> ret;
BinaryIndexedTree<int> ints(n + 1);
for (int i = 0; i < n; ++i) {
ints.add(i, i + 1);
}
ll sum = n * (n + 1) / 2;
for (int i = n - 1; i >= 0; --i) {
int idx = ints.lower_bound(s[i]);
ret.push_front(idx);
ints.add(idx - 1, -(idx));
sum -= idx + 1;
}
print(ret);
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, p[200005], mnpos[1 << 19];
long long s[200005], mn[1 << 19], add[1 << 19];
void pushup(int u) {
mn[u] = mn[u << 1 | 1], mnpos[u] = mnpos[u << 1 | 1];
if (mn[u << 1] < mn[u]) mn[u] = mn[u << 1], mnpos[u] = mnpos[u << 1];
}
void build(int u, int l, int r) {
if (l == r) {
mn[u] = s[l];
mnpos[u] = l;
return;
}
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void upd(int u, long long v) { add[u] += v, mn[u] += v; }
void pushdown(int u) {
if (add[u]) {
upd(u << 1, add[u]);
upd(u << 1 | 1, add[u]);
add[u] = 0;
}
}
void update(int u, int l, int r, int ql, int qr, long long v) {
if (l >= ql && r <= qr) {
upd(u, v);
return;
}
int mid = l + r >> 1;
pushdown(u);
if (ql <= mid) update(u << 1, l, mid, ql, qr, v);
if (qr > mid) update(u << 1 | 1, mid + 1, r, ql, qr, v);
pushup(u);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%lld", s + i);
build(1, 1, n);
for (int i = 1; i <= n; ++i) {
int x = mnpos[1];
p[x] = i;
update(1, 1, n, x, x, 1LL << 40);
update(1, 1, n, x + 1, n, -1);
}
for (int i = 1; i <= n; ++i) printf("%d ", p[i]);
puts("");
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.util.*;
public final class code
{
static int n,ans[];
static long tree[],s[];
static void update(int idx,long x)
{
while(idx<=n)
{
tree[idx]+=x;
idx+=idx&(-idx);
}
}
static long read(int idx)
{
long res=0;
while(idx>0)
{
res+=tree[idx];
idx-=idx&(-idx);
}
return res;
}
static int bin_search(long x,int low,int high)
{
int ans=1;
while(low<=high)
{
int mid=(low+high)/2;
if(read(mid)==x)
{
ans=mid;
break;
}
else if(read(mid)<x)
low=mid+1;
else
high=mid-1;
}
return ans;
}
static void solve()throws IOException
{
n=nextInt();
ans=new int[n+1];
tree=new long[n+1];
s=new long[n+1];
for(int i=1;i<=n;i++)
{
update(i,i-1);
s[i]=nextLong();
}
for(int i=n;i>=1;i--)
{
int j=bin_search(s[i],1,n);
ans[i]=j;
//System.out.println(j);
update(j,-s[i]-1);
update(j+1,s[i]+1-j);
}
for(int i=1;i<=n;i++)
out.print(ans[i]+" ");
}
///////////////////////////////////////////////////////////
static BufferedReader br;
static StringTokenizer st;
static PrintWriter out;
static String nextToken()throws IOException
{
while(st==null || !st.hasMoreTokens())
st=new StringTokenizer(br.readLine());
return st.nextToken();
}
static String nextLine()throws IOException
{
return br.readLine();
}
static int nextInt()throws IOException
{
return Integer.parseInt(nextToken());
}
static long nextLong()throws IOException
{
return Long.parseLong(nextToken());
}
static double nextDouble()throws IOException
{
return Double.parseDouble(nextToken());
}
public static void main(String args[])throws IOException
{
br=new BufferedReader(new InputStreamReader(System.in));
out=new PrintWriter(new BufferedOutputStream(System.out));
solve();
out.close();
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long N = 300005;
struct segment_tree {
long long n;
vector<long long> it, lz, a;
segment_tree(vector<long long> _) {
a = _;
a.insert(a.begin(), 0);
n = _.size();
it.resize((n << 2) | 1);
lz.resize((n << 2) | 1);
build(1, 1, n);
}
long long build(long long u, long long l, long long r) {
if (l == r) return it[u] = a[l];
return it[u] = min(build(u << 1, l, ((l + r) >> 1)),
build((u << 1) | 1, ((l + r) >> 1) + 1, r));
}
void lazy(long long u, long long l, long long r) {
if (lz[u] == 0) return;
it[u] += lz[u];
if (l != r) {
lz[u << 1] += lz[u];
lz[(u << 1) | 1] += lz[u];
}
lz[u] = 0;
}
void update(long long u, long long l, long long r, long long L, long long R,
long long val) {
if (R < L) return;
lazy(u, l, r);
if (l > R || r < L) return;
if (L <= l && r <= R) {
lz[u] += val;
lazy(u, l, r);
return;
}
update(u << 1, l, ((l + r) >> 1), L, R, val);
update((u << 1) | 1, ((l + r) >> 1) + 1, r, L, R, val);
it[u] = min(it[u << 1], it[(u << 1) | 1]);
}
long long get(long long u, long long l, long long r, long long L,
long long R) {
if (l > R || r < L) return LONG_MAX;
lazy(u, l, r);
if (L <= l && r <= R) return it[u];
return min(get(u << 1, l, ((l + r) >> 1), L, R),
get((u << 1) | 1, ((l + r) >> 1) + 1, r, L, R));
}
};
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) cin >> a[i];
segment_tree ds(a);
vector<long long> ans(n + 1);
for (long long i = 1; i <= n; i++) {
long long L = 1, R = n, cur = -1;
while (L <= R) {
long long M = (L + R) >> 1;
long long z = ds.get(1, 1, n, M, n);
if (ds.get(1, 1, n, M, n) == 0)
cur = M, L = M + 1;
else
R = M - 1;
}
cout << cur << '\n';
ans[cur] = i;
ds.update(1, 1, n, cur + 1, n, -i);
ds.update(1, 1, n, cur, cur, LONG_MAX);
}
for (long long i = 1; i <= n; i++) cout << ans[i] << " ";
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author JENSIH
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DRestorePermutation solver = new DRestorePermutation();
solver.solve(1, in, out);
out.close();
}
static class DRestorePermutation {
public void solve(int testNumber, ScanReader in, PrintWriter out) {
int n = in.scanInt();
int ar[] = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = in.scanInt();
}
SegLazy ss = new SegLazy(n);
int ans[] = new int[n];
for (int i = 0; i < n; i++) ss.update(i, i, +ar[i]);
for (int i = 1; i <= n; i++) {
int pos = ss.getlastZERO();
ans[pos] = i;
ss.update(pos, pos, Long.MAX_VALUE / 2);
ss.update(pos + 1, n - 1, -i);
}
for (int i = 0; i < n; i++) {
out.print(ans[i] + " ");
}
}
public class SegLazy {
int n;
long[] tree;
long[] lazy;
public SegLazy(int n) {
this.n = n;
this.tree = new long[(4 * n) + 5];
this.lazy = new long[(4 * n) + 5];
}
public void update(int l, int r, long val) {
update(1, 0, n - 1, l, r, val);
}
public int getlastZERO() {
return qu(1, 0, n - 1);
}
private int qu(int node, int start, int end) {
if (start == end) return start;
else {
LAZY(node, start, end);
int mid = (start + end) / 2;
int res = -1;
LAZY(node, start, end);
LAZY(node * 2, start, mid);
LAZY((node * 2) + 1, mid + 1, end);
if (tree[(node * 2) + 1] == 0) {
res = qu((node * 2) + 1, mid + 1, end);
} else {
res = qu((node * 2), start, mid);
}
LAZY(node, start, end);
LAZY(node * 2, start, mid);
LAZY((node * 2) + 1, mid + 1, end);
tree[node] = Math.min(tree[node * 2], tree[(node * 2) + 1]);
return res;
}
}
private void update(int node, int start, int end, int l, int r, long val) {
LAZY(node, start, end);
if (start > end || start > r || end < l) return;
if (start >= l && end <= r) {
tree[node] += val;
LAZY(node, start, end);
if (start != end) {
lazy[node * 2] += val;
lazy[(node * 2) + 1] += val;
}
return;
}
int mid = (start + end) / 2;
update(node * 2, start, mid, l, r, val);
update((node * 2) + 1, mid + 1, end, l, r, val);
LAZY(node, start, end);
LAZY(node * 2, start, mid);
LAZY((node * 2) + 1, mid + 1, end);
tree[node] = Math.min(tree[node * 2], tree[(node * 2) + 1]);
}
private void LAZY(int node, int start, int end) {
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (start != end) {
lazy[node * 2] += lazy[node];
lazy[(node * 2) + 1] += lazy[node];
}
lazy[node] = 0;
}
}
}
}
static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int INDEX;
private BufferedInputStream in;
private int TOTAL;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() {
if (INDEX >= TOTAL) {
INDEX = 0;
try {
TOTAL = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (TOTAL <= 0) return -1;
}
return buf[INDEX++];
}
public int scanInt() {
int I = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
I *= 10;
I += n - '0';
n = scan();
}
}
return neg * I;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, a[1005000], ans[1005000], s[4 * 1005000], lazy[4 * 1005000];
void build(int id, int l, int r) {
if (l > r) return;
if (l == r) {
s[id] = a[l];
lazy[id] = a[l];
return;
}
int mid = (l + r) / 2;
build(2 * id, l, mid);
build(2 * id + 1, mid + 1, r);
s[id] = min(s[2 * id], s[2 * id + 1]);
}
void shift(int id) {
if (lazy[id]) {
s[2 * id] += lazy[id];
lazy[2 * id] += lazy[id];
s[2 * id + 1] += lazy[id];
lazy[2 * id + 1] += lazy[id];
lazy[id] = 0;
}
}
void update(int id, int l, int r, int L, int R, int val) {
if (l > R || r < L) return;
if (L <= l && r <= R) {
s[id] += val;
lazy[id] += val;
return;
}
shift(id);
int mid = (l + r) / 2;
update(2 * id, l, mid, L, R, val);
update(2 * id + 1, mid + 1, r, L, R, val);
s[id] = min(s[2 * id], s[2 * id + 1]);
}
int get_pos(int id, int l, int r) {
if (l == r) return l;
shift(id);
int mid = (l + r) / 2;
if (s[2 * id + 1] == 0)
get_pos(2 * id + 1, mid + 1, r);
else
get_pos(2 * id, l, mid);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
build(1, 1, n);
for (int i = 1; i <= n; i++) {
int pos = get_pos(1, 1, n);
update(1, 1, n, pos, pos, 1e9);
update(1, 1, n, pos + 1, n, -i);
ans[pos] = i;
}
for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
// Don't place your source in a package
import javax.swing.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.util.stream.Stream;
// Please name your class Main
public class Main {
static FastScanner fs=new FastScanner();
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int Int() {
return Integer.parseInt(next());
}
long Long() {
return Long.parseLong(next());
}
String Str(){
return next();
}
}
public static void main (String[] args) throws java.lang.Exception {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int T=1;
for(int t=0;t<T;t++){
int n=Int();
int A[]=new int[n];
for(int i=0;i<A.length;i++){
A[i]=Int();
}
Solution sol=new Solution(out);
sol.solution(A);
}
out.close();
}
public static int Int(){
return fs.Int();
}
public static long Long(){
return fs.Long();
}
public static String Str(){
return fs.Str();
}
}
class Solution{
PrintWriter out;
public Solution(PrintWriter out){
this.out=out;
}
public void solution(int A[]){
int n=A.length;
int arr[]=new int[n];
int res[]=new int[n];
TreeSet<Integer>tree=new TreeSet<>();
for(int i=0;i<arr.length;i++){
arr[i]=i+1;
tree.add(i);
}
FenWick fen=new FenWick(arr);
for(int i=n-1;i>=0;i--){
int l=0,r=A.length-1;
int pos=-1;
while(l<=r){
int mid=l+(r-l)/2;
int sum=fen.sumRange(0,mid);
if(sum>=A[i]){
pos=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
if(A[i]==0){
int first= tree.first();
res[i]=first+1;
tree.remove(first);
}
else{
Integer high=tree.higher(pos);
res[i]=high+1;
fen.update(high,high+1);
tree.remove(high);
}
}
for(int i:res){
out.print(i+" ");
}
}
class FenWick {
int tree[];//1-index based
int A[];
int arr[];
public FenWick(int[] A) {
this.A=A;
arr=new int[A.length];
tree=new int[A.length+1];
int sum=0;
for(int i=0;i<A.length;i++){
update(i,A[i]);
}
}
public void update(int i, int val) {
arr[i]=+val;
i++;
while(i<tree.length){
tree[i]+=val;
i+=(i&-i);
}
}
public int sumRange(int i, int j) {
return pre(j+1)-pre(i);
}
public int pre(int i){
int sum=0;
while(i>0){
sum+=tree[i];
i-=(i&-i);
}
return sum;
}
}
}
/*
;\
|' \
_ ; : ;
/ `-. /: : |
| ,-.`-. ,': : |
\ : `. `. ,'-. : |
\ ; ; `-.__,' `-.|
\ ; ; ::: ,::'`:. `.
\ `-. : ` :. `. \
\ \ , ; ,: (\
\ :., :. ,'o)): ` `-.
,/,' ;' ,::"'`.`---' `. `-._
,/ : ; '" `;' ,--`.
;/ :; ; ,:' ( ,:)
,.,:. ; ,:., ,-._ `. \""'/
'::' `:'` ,'( \`._____.-'"'
;, ; `. `. `._`-. \\
;:. ;: `-._`-.\ \`.
'`:. : |' `. `\ ) \
-hrr- ` ;: | `--\__,'
'` ,'
,-'
free bug dog
*/
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
/**
* @author derrick20
*/
import java.io.*;
import java.util.*;
public class RestorePermutations {
public static void main(String args[]) throws Exception {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int N = sc.nextInt();
long[] sums = new long[N];
for (int i = 0; i < sums.length; i++) {
sums[i] = sc.nextLong();
}
int[] arr = new int[N + 1];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
bit = new BIT(arr);
used = new HashSet<>();
int[] seq = new int[N];
for (int i = N - 1; i >= 0; i--) {
int val = binarySearch(1, N, sums[i]);
// Move up if it was used (only for 0 case)
while (used.contains(val) && bit.prefixSum(val + 1 - 1) == sums[i]) {
val++;
}
seq[i] = val;
bit.add(val, -val);
// System.out.println(bit);
used.add(val);
}
for (int i = 0; i < seq.length; i++) {
out.print(seq[i] + " ");
}
out.close();
}
static BIT bit;
static HashSet<Integer> used;
// Find the number such that the sum of things below it in the
// current sequence state equals the goal sum
// 0001111 Let's find the lowest one that's >= it. That's the same thing
static int binarySearch(int lo, int hi, long sum) {
while (lo < hi) {
int mid = (lo + hi) / 2; // go towards the 0's, so we crawl up
if (bit.prefixSum(mid - 1) >= sum) {
hi = mid;
}
else {
lo = mid + 1;
}
}
return lo;
}
static class BIT {
int[] arr;
int[] tree;
// todo ***This ASSUMES THAT ARR IS 1-INDEXED ALREADY***
public BIT(int[] arr) {
this.arr = arr;
this.tree = new int[arr.length];
// copy arr values into tree
for (int i = 1; i < tree.length; i++) {
tree[i] = arr[i];
}
constructBIT(arr, tree);
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Index: ");
for (int i = 1; i < tree.length; i++) {
sb.append(i + " ");
}
sb.append("\nValue: ");
for (int i = 1; i < tree.length; i++) {
sb.append(this.sum(i, i) + " ");
}
return sb.toString();
}
public int leastSignificantBit(int x) {
// by negating it, all
return x & (-x);
}
public void constructBIT(int[] arr, int[] tree) {
// propagate information up to the pieces above it that would be responsible for it
for (int i = 1; i < tree.length; i++) {
int j = i + leastSignificantBit(i);
// all of it's "parents" will need to possess its values,
// but, we can be clever and only propagate to its immediate parent.
// Since we are processing in order, that parent will propagate to its parent
// eventually, so we are fine. Add methods are log(N) because we can't rely on our
// parents eventually doing work for us.
if (j < arr.length) {
tree[j] += tree[i];
}
}
}
// return the sum
public int sum(int i, int j) {
return prefixSum(j) - prefixSum(i - 1);
// exclude the values under i
}
// returns sum from 1 to i of the array
// propagate downward! (decomposing the sum)
public int prefixSum(int i) {
int sum = 0;
while (i > 0) {
sum += tree[i];
i -= leastSignificantBit(i);
}
return sum;
}
// add a value of val at the ith value
// propagate upward!
public void add(int i, int val) {
while (i < tree.length) {
tree[i] += val;
i += leastSignificantBit(i);
}
}
// Change a value at an index (basically add the new value and subtract
// the original value
public void set(int i, int k) {
int val = sum(i, i);
add(i, k - val);
}
}
static class FastScanner {
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double cnt = 1;
BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
cnt=1;
boolean neg = false;
if(c==NC)c=getChar();
for(;(c<'0' || c>'9'); c = getChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=getChar()) {
res = (res<<3)+(res<<1)+c-'0';
cnt*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/cnt;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=getChar();
while(c>32) {
res.append(c);
c=getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=getChar();
while(c!='\n') {
res.append(c);
c=getChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=getChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.util.*;
public class Main {
public static final long INF = Long.MAX_VALUE;
public static class Pair {
long min;
int index;
public Pair(long min, int index) {
this.min = min;
this.index = index;
}
public void add(long x) {
if (this.min == INF || x == INF) {
this.min = INF;
} else {
this.min += x;
}
}
@Override
public String toString() {
return "(" + min + ", " + index + ")";
}
}
public static class SegmentTree {
public int n, next;
public Pair[] tree;
public long[] update;
public int next(int n) {
int i;
for (i = 1; i < n; i *= 2);
return i;
}
public SegmentTree(int[] arr) {
n = arr.length;
next = next(n);
tree = new Pair[next * 2];
update = new long[next * 2];
for (int i = 0; i < n; i++) {
tree[next + i] = new Pair(arr[i], i);
}
for (int i = n; i < next; i++) {
tree[next + i] = new Pair(INF, i);
}
for (int i = next - 1; i >= 1; i--) {
tree[i] = join(tree[i * 2], tree[i * 2 + 1]);
}
}
public long sum(long a, long b) {
if (a == INF || b == INF) {
return INF;
}
return a + b;
}
public void update(int i) {
if (update[i] != 0) {
tree[i].add(update[i]);
if (i < next) {
update[i * 2] = sum(update[i * 2], update[i]);
update[i * 2 + 1] = sum(update[i * 2 + 1], update[i]);
}
update[i] = 0;
}
}
public void add(long x, int l, int r, int i, int L, int R) {
if (l <= L && R <= r) {
update[i] = sum(update[i], x);
update(i);
i /= 2;
while (i >= 1 && i < next) {
tree[i] = join(tree[i * 2], tree[i * 2 + 1]);
i /= 2;
}
} else if (!(L > r || l > R)) {
int mid = (L + R) / 2;
add(x, l, r, i * 2, L, mid);
add(x, l, r, i * 2 + 1, mid + 1, R);
}
}
public Pair min(int l, int r, int i, int L, int R) {
update(i);
if (l <= L && R <= r) {
return tree[i];
} else if (!(L > r || l > R)) {
int mid = (L + R) / 2;
return join(min(l, r, i * 2, L, mid),
min(l, r, i * 2 + 1, mid + 1, R));
} else {
return null;
}
}
@Override
public String toString() {
String str = "";
str += "Tree: " + Arrays.toString(tree) + "\n";
str += "Update: " + Arrays.toString(update) + "\n";
return str;
}
}
public static Pair join(Pair a, Pair b) {
if (a == null) {
return new Pair(b.min, b.index);
} else if (b == null) {
return new Pair(a.min, a.index);
} else if (a.min == b.min) {
return new Pair(a.min, Math.max(a.index, b.index));
} else if (a.min < b.min) {
return new Pair(a.min, a.index);
} else {
return new Pair(b.min, b.index);
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
SegmentTree tree = new SegmentTree(arr);
int[] ans = new int[n];
int delta = 1;
for (int i = n - 1; i >= 0; i--) {
Pair pair = tree.min(0, n - 1, 1, 0, tree.next - 1);
ans[pair.index] = delta;
tree.add(-delta, pair.index + 1, n - 1, 1, 0, tree.next - 1);
tree.add(INF, pair.index, pair.index, 1, 0, tree.next - 1);
delta++;
}
for (int x : ans) {
System.out.print(x + " ");
}
System.out.println();
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool mini(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool maxi(T &a, T b) {
return a < b ? (a = b, true) : false;
}
const int N = 2e5 + 5;
int n, bit[N], p[N], ans[N];
void up(int i) {
assert(i > 0);
for (int val = i; i <= n; i += i & -i) bit[i] += val;
}
int get(int i) {
int res = 0;
for (; i > 0; i -= i & -i) res += bit[i];
return res;
}
inline void sol() {
cin >> n;
for (int i = 0, _ = (n); i < _; i++) cin >> p[i];
for (int i = (int)(n)-1; i >= 0; --i) {
int l = 0, r = n - 1, mid;
while (l < r) {
mid = (l + r + 1) >> 1;
if (mid * (mid + 1) / 2 > get(mid) + p[i])
r = mid - 1;
else
l = mid;
}
up(ans[i] = l + 1);
}
for (int i = 0, _ = (n); i < _; i++) cout << ans[i] << ' ';
cout << "\n";
}
signed main() {
{ ios_base::sync_with_stdio(false), cin.tie(NULL); };
cout.precision(10);
cout << fixed;
sol();
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-9, PI = acos(-1.);
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 5;
int n, ans[N];
long long mi[4 * N], pos[4 * N], v[N], lz[4 * N];
void build(int p, int l, int r) {
if (l == r) {
mi[p] = v[l], pos[p] = l;
return;
}
build(2 * p, l, (l + r) / 2);
build(2 * p + 1, (l + r) / 2 + 1, r);
mi[p] = min(mi[2 * p], mi[2 * p + 1]);
if (mi[p] == mi[2 * p + 1])
pos[p] = pos[2 * p + 1];
else
pos[p] = pos[2 * p];
}
void push(int p, int l, int r) {
if (lz[p]) {
mi[p] += lz[p];
if (l != r) {
lz[2 * p] += lz[p];
lz[2 * p + 1] += lz[p];
}
lz[p] = 0;
}
}
pair<long long, long long> query(int p, int l, int r, int i, int j) {
push(p, l, r);
if (r < i or l > j) return {LINF, 0};
if (l >= i and r <= j) return {mi[p], pos[p]};
pair<long long, long long> a = query(2 * p, l, (l + r) / 2, i, j);
pair<long long, long long> b = query(2 * p + 1, (l + r) / 2 + 1, r, i, j);
if (a.first < b.first)
return a;
else
return b;
}
void update(int p, int l, int r, int i, int j, int v) {
push(p, l, r);
if (r < i or l > j) return;
if (l >= i and r <= j) {
lz[p] = v;
push(p, l, r);
return;
}
update(2 * p, l, (l + r) / 2, i, j, v);
update(2 * p + 1, (l + r) / 2 + 1, r, i, j, v);
mi[p] = min(mi[2 * p], mi[2 * p + 1]);
if (mi[p] == mi[2 * p + 1])
pos[p] = pos[2 * p + 1];
else
pos[p] = pos[2 * p];
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &v[i]);
build(1, 1, n);
memset(ans, -1, sizeof(ans));
for (int i = 1; i <= n; i++) {
auto p = query(1, 1, n, 1, n);
ans[p.second] = i;
update(1, 1, n, p.second + 1, n, -i);
update(1, 1, n, p.second, p.second, LINF);
}
for (int i = 1; i <= n; i++) printf("%lld ", ans[i]);
printf("\n");
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
namespace myland{
using namespace std;
namespace _abbr{
#define fi first
#define se second
#define pb push_back
#define pr make_pair
#define lb lower_bound
#define ub upper_bound
#define str string
#define _1s __builtin_popcountll
#define nth(a,l,r,k) nth_element(a+l,a+l+k-1,a+r+1)
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
}using namespace _abbr;
namespace _constant{
const double EPS(1e-8);
const double PI(acos(-1.0));
const int INF(0x3f3f3f3f);
const ll INFL(0x3f3f3f3f3f3f3f3fll);
const int MOD(1e9+7);//998244353, 16341163, 999983, 131, 1e9 + 9
const int dx[]={1,0,0,-1,-1,1,1,-1}, dy[]={0,-1,1,0,1,1,-1,-1};
}using namespace _constant;
namespace _solve{
#define XOR int main()
#define GG exit(0)
#define rep(x,a,b) for(int x=a;x<=b;++x)
#define per(x,a,b) for(int x=b;x>=a;--x)
#define times(x) for(int cas=1,tms=(x);cas<=tms;cas++)
#define CaseT int T;cin >> T;times(T)
#define TCase int T;scanf("%d",&T);times(T)
#define continue(_) if(_) continue;
#define break(_) if(_) break;
}using namespace _solve;
namespace _calculate{
bool odd(ll x){return x&1;}
bool even(ll x){return (x&1)^1;}
bool posi(ll x){return x>0;}
bool nega(ll x){return x<0;}
bool zero(ll x){return x==0;}
bool prime(ll x){if(x<2) return 0;
for(ll i=2;1ll*i*i<=x;i++) if(x%i==0) return 0;
return 1;}
ll droot(ll x){return 1+(x-1)%9;}
ll upd(ll a, ll b){return a%b?a/b+1:a/b;};
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a/gcd(a, b)*b;}
mt19937_64 rand(time(0));
ll random(ll a, ll b){return a+rand()%(b-a+1);};
ll bitn(ll x){ll c=0;while(x)c++,x>>=1;return c;}
template<class T>T sqr(T x){return x*x;}
ll qpow(ll a, ll n, ll mod = MOD){
ll res(1);while(n){
if(n&1) (res*=a)%=mod;
(a*=a)%=mod;n>>=1;}
return res%mod;}
ll inv(ll a, ll mod = MOD){return qpow(a,mod-2, mod);}
template<class T>void tomin(T& a,T b){if(b<a) a=b;}
template<class T>void tomax(T& a,T b){if(b>a) a=b;}
}using namespace _calculate;
namespace _simple_algo{
#define rg(a,x,y) (a+x), (a+y+1)
#define vrg(a,x,y) (a.begin()+x),(a.begin()+y+1)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define clr(a,b) memset(a,b,sizeof(a))
#define fil(a,b) fill(all(a),b)
#define _sum(a) accumulate(a, 0ll)
#define sz(a) (ll(a.size()))
ll sol(const string& s){ll x=0;
for(char c:s)x=x*10+c-48;return x;}
string los(ll x){string s = "";
if(x==0) return "0";
while(x) s=char(x%10+48)+s,x/=10;
return s;}
bool pal(const string& s){int l = s.size();
for(int i=0,j=l-1;i<j;i++,j--)
if(s[i]!=s[j]) return 0;return 1;}
}using namespace _simple_algo;
namespace _io{
#define FAST_IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define bug cerr << "!!!" << endl;
#define bugc(_) cerr << (#_) << " = " << (_) << endl;
template<class T>void rd(T& x){cin>>x;}
str srd(){str s;cin>>s;return s;}
ll rd(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;}
void rd(int& x){x=rd();}
void rd(ll& x){x=rd();}
template<class A,class B>void rd(A& a,B& b){rd(a), rd(b);}
template<class A,class B>void rd(pair<A,B>& p){rd(p.fi, p.se);}
template<class A,class B,class C>void rd(A& a,B& b,C& c){rd(a,b);rd(c);}
template<class T>void wt(const T& x){cout << x << '\n';}
template<class T>void wt(const T& x, char c){cout << x << c;}
template<class T>void wt(const T& x, const str& s){cout << x << s;}
template<class T>void wt(const T& x, int rnd){
cout << fixed << setprecision(rnd) << x << '\n';}
template<class T>void wt(const vector<T>& v){
for(int i=0;i<sz(v);i++){cout << v[i];if(i<sz(v)-1) cout << ' ';};wt("");}
template<class A,class B>void wt(const pair<A,B>& pr){
cout << pr.fi << ' ' << pr.se << '\n';}
#define out(_) wt(_),GG
}using namespace _io;
}using namespace myland;
const int N = 2e5 + 5;
struct segt {
struct seg {
int l, r, p;
ll mn, lz;
void update(ll v) {
mn += v; lz += v;
}
};
#define lc x<<1
#define rc x<<1|1
#define mid ((t[x].l+t[x].r)/2)
ll * a;
seg t[N<<2];
void modify(ll * arr) {a = arr;}
void push_up(int x) {
if(t[lc].mn < t[rc].mn){
t[x].p = t[lc].p;
t[x].mn = t[lc].mn;
}
else{
t[x].p = t[rc].p;
t[x].mn = t[rc].mn;
}
}
void push_down(int x) {
ll lz = t[x].lz;
t[lc].update(lz); t[rc].update(lz);
t[x].lz = 0;
}
void build(int x, int l, int r) {
t[x] = seg({l, r, 0, INFL, 0ll});
if (l == r) {
t[x].p = l;
t[x].mn = a[l];
return;
}
build(lc, l, mid); build(rc, mid+1, r);
push_up(x);
}
void update(int x, int l, int r, ll v) {
int L(t[x].l), R(t[x].r);
if (l <= L && R <= r) {
t[x].update(v);
return;
}
push_down(x);
if (l <= mid) update(lc, l, r, v);
if (r > mid) update(rc, l, r, v);
push_up(x);
}
// ll query(int x, int l, int r) {
// int L(t[x].l), R(t[x].r);
// if (l <= L && R <= r) return t[x].mn;
// push_down(x);
// ll res(INFL);
// if (l <= mid) tomin(res, query(lc, l, r));
// if (r > mid) tomin(res, query(rc, l, r));
// return res;
// }
};
int n;
ll s[N], ans[N];
segt tree;
XOR{
rd(n);
rep(i,1,n) rd(s[i]);
tree.modify(s);
tree.build(1,1,n);
rep(i,1,n){
int p = tree.t[1].p;
ans[p] = i;
tree.update(1,p,p,INFL);
if(p+1<=n) tree.update(1,p+1,n,-i);
}
rep(i,1,n) printf("%d%c", ans[i], " \n"[i==n]);
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
n=int(input())
queue = list(map(int,input().split()))
queue = queue[::-1]
outf = [''] * n
for i in range(1, n+1):
a = queue.index(0)
outf[a]=str(i)
for j in range(i):
b= queue[j]-i
queue[j]=b
outf=outf[::-1]
print(' '.join(outf))
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int li[200001];
long long bit[200010];
long long query(int i) {
i += 2;
long long sum = 0;
while (i) {
sum += bit[i];
i -= i & (-i);
}
return sum;
}
void update(int i, int v) {
i += 2;
while (i < 200010) {
bit[i] += v;
i += i & (-i);
}
}
int n;
int main() {
scanf("%d", &n);
set<int> prox;
for (int i = 1; i <= n; i++) {
prox.insert(i);
update(i, i);
}
for (int i = 0; i < n; i++) {
scanf("%d", li + i);
}
stack<int> resps;
for (int i = n - 1; i >= 0; i--) {
set<int>::iterator it = prox.end();
int l = *prox.begin(), r = *prev(it);
int f = l;
while (l <= r) {
int m = (l + r) / 2;
int v = *prox.lower_bound(m);
int q = query(v - 1);
if (q == li[i]) {
f = v;
break;
} else if (q < li[i]) {
l = m + 1;
} else {
r = m - 1;
}
}
prox.erase(f);
resps.push(f);
update(f, -f);
}
int p = 0;
while (!resps.empty()) {
int r = resps.top();
resps.pop();
if (p) printf(" ");
printf("%d", r);
p = 1;
}
printf("\n");
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.util.*;
import java.io.*;
public class template {
final static int MOD = 1000000007;
final static int intMax = 1000000000;
final static int intMin = -1000000000;
final static int[] DX = { 0, 0, -1, 1 };
final static int[] DY = { -1, 1, 0, 0 };
static int T;
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[360]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public static void main(String[] args) throws Exception {
Reader in = new Reader();
int n = in.nextInt();
long s[] = new long[n];
for(int i = 0; i < n; ++i) {
s[i] = in.nextInt();
}
LazySegmentTree lst = new LazySegmentTree(n);
for(int i = 0; i < n; ++i) {
lst.update(i, i, s[i]);
}
int[] ans = new int[n];
for(int i = 1; i <= n; ++i) {
int l = 0, r = n - 1;
while(l < r) {
int mid = l + (r - l + 1) / 2;
if(lst.minQuery(mid, n - 1) == 0) {
l = mid;
}
else {
r = mid - 1;
}
}
lst.update(l, n - 1, -1);
lst.update(l, l, 1000000000);
ans[l] = i;
}
for(int i = 0; i < n; ++i) {
System.out.print(ans[i] + " ");
}
System.out.println();
in.close();
}
static class LazySegmentTree {
private long[] min;
private long[] lazy;
private long[] sum;
private long[] max;
private int size;
public LazySegmentTree(int size) {
this.size = size;
min = new long[4*size];
sum = new long[4*size];
max = new long[4*size];
lazy = new long[4*size];
}
public void update(int l, int r, long inc) {
update(1, 0, size-1, l, r, inc);
}
private void pushDown(int index, int l, int r) {
min[index] += lazy[index];
min[index] = Math.max(min[index], 0);
max[index] += lazy[index];
sum[index] += lazy[index] * (r-l+1);
if(l != r) {
lazy[2*index] += lazy[index];
lazy[2*index+1] += lazy[index];
}
lazy[index] = 0;
}
private void pullUp(int index, int l, int r) {
int m = (l+r)/2;
min[index] = Math.min(evaluateMin(2*index, l, m), evaluateMin(2*index+1, m+1, r));
min[index] = Math.max(min[index], 0);
max[index] = Math.max(evaluateMax(2*index, l, m), evaluateMax(2*index+1, m+1, r));
sum[index] = evaluateSum(2*index, l, m) + evaluateSum(2*index+1, m+1, r);
}
private long evaluateSum(int index, int l, int r) {
return sum[index] + (r-l+1)*lazy[index];
}
private long evaluateMin(int index, int l, int r) {
return Math.max(0, min[index] + lazy[index]);
}
private long evaluateMax(int index, int l, int r) {
return max[index] + lazy[index];
}
private void update(int index, int l, int r, int left, int right, long inc) {
if(r < left || l > right) return;
if(l >= left && r <= right) {
lazy[index] += inc;
return;
}
pushDown(index, l, r);
int m = (l+r)/2;
update(2*index, l, m, left, right, inc);
update(2*index+1, m+1, r, left, right, inc);
pullUp(index, l, r);
}
public long minQuery(int l, int r) {
return minQuery(1, 0, size-1, l, r);
}
private long minQuery(int index, int l, int r, int left, int right) {
if(r < left || l > right) return Long.MAX_VALUE;
if(l >= left && r <= right) {
return evaluateMin(index, l, r);
}
pushDown(index, l, r);
int m = (l+r)/2;
long ret = Long.MAX_VALUE;
ret = Math.min(ret, minQuery(2*index, l, m, left, right));
ret = Math.min(ret, minQuery(2*index+1, m+1, r, left, right));
pullUp(index, l, r);
return ret;
}
public long sumQuery(int l, int r) {
return sumQuery(1, 0, size-1, l, r);
}
private long sumQuery(int index, int l, int r, int left, int right) {
if(r < left || l > right) return 0;
if(l >= left && r <= right) {
return evaluateSum(index, l, r);
}
pushDown(index, l, r);
int m = (l+r)/2;
long ret = 0;
ret += sumQuery(2*index, l, m, left, right);
ret += sumQuery(2*index+1, m+1, r, left, right);
pullUp(index, l, r);
return ret;
}
public long maxQuery(int l, int r) {
return maxQuery(1, 0, size-1, l, r);
}
private long maxQuery(int index, int l, int r, int left, int right) {
if(r < left || l > right) return Long.MIN_VALUE;
if(l >= left && r <= right) {
return evaluateMax(index, l, r);
}
pushDown(index, l, r);
int m = (l+r)/2;
long ret = 0;
ret = Math.max(ret, maxQuery(2*index, l, m, left, right));
ret = Math.max(ret, maxQuery(2*index+1, m+1, r, left, right));
pullUp(index, l, r);
return ret;
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> t;
void build(int a[], int v, int tl, int tr) {
if (tl == tr) {
t[v] = {a[tl], tl};
} else {
int tm = (tl + tr) / 2;
build(a, v * 2 + 1, tl, tm);
build(a, v * 2 + 2, tm + 1, tr);
if (t[2 * v + 1].first < t[2 * v + 2].first) {
t[v] = t[2 * v + 1];
} else if (t[2 * v + 1].first > t[2 * v + 2].first) {
t[v] = t[2 * v + 2];
} else {
t[v] = {t[2 * v + 1].first,
max(t[2 * v + 1].second, t[2 * v + 2].second)};
}
}
}
void update(int v, int tl, int tr, int l, int r, int p) {
if (l > r) return;
if (l == tl && r == tr) {
t[v].first -= p;
} else {
int tm = (tl + tr) / 2;
update(v * 2 + 1, tl, tm, l, min(r, tm), p);
update(v * 2 + 2, tm + 1, tr, max(l, tm + 1), r, p);
if (t[2 * v + 1].first < t[2 * v + 2].first) {
t[v] = t[2 * v + 1];
} else if (t[2 * v + 1].first > t[2 * v + 2].first) {
t[v] = t[2 * v + 2];
} else {
t[v] = {t[2 * v + 1].first,
max(t[2 * v + 1].second, t[2 * v + 2].second)};
}
}
}
void change(int v, int tl, int tr, int pos, pair<int, int> new_val) {
if (tl == tr) {
t[v] = new_val;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
change(v * 2 + 1, tl, tm, pos, new_val);
else
change(v * 2 + 2, tm + 1, tr, pos, new_val);
if (t[2 * v + 1].first < t[2 * v + 2].first) {
t[v] = t[2 * v + 1];
} else if (t[2 * v + 1].first > t[2 * v + 2].first) {
t[v] = t[2 * v + 2];
} else {
t[v] = {t[2 * v + 1].first,
max(t[2 * v + 1].second, t[2 * v + 2].second)};
}
}
}
int main() {
int n;
cin >> n;
int s[n], p[n];
t.assign(n * 4, {INT_MAX, INT_MIN});
for (int i = 0; i < n; i++) {
cin >> s[i];
}
build(s, 0, 0, n - 1);
for (int i = 1; i <= n; i++) {
p[t[0].second] = i;
if (i == n) {
for (int j = 0; j < n; j++) {
cout << p[j] << ' ';
}
} else {
change(0, 0, n - 1, t[0].second, {INT_MAX, INT_MIN});
update(0, 0, n - 1, t[0].second + 1, n - 1, i);
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const long long inf = 1e11;
struct Node {
int l, r, num;
long long tag;
long long val;
} node[4 * maxn];
long long a[maxn];
int ans[maxn];
void up(int no) {
if (node[no << 1].val < node[no << 1 | 1].val) {
node[no].val = node[no << 1].val;
node[no].num = node[no << 1].num;
} else {
node[no].val = node[no << 1 | 1].val;
node[no].num = node[no << 1 | 1].num;
}
}
void build(int l, int r, int no) {
node[no].l = l;
node[no].r = r;
node[no].tag = 0;
if (l == r) {
node[no].val = a[l];
node[no].num = l;
return;
}
int mid = l + r >> 1;
build(l, mid, no << 1);
build(mid + 1, r, no << 1 | 1);
up(no);
}
void pushdown(int no) {
node[no << 1].tag += node[no].tag;
node[no << 1].val += node[no].tag;
node[no << 1 | 1].tag += node[no].tag;
node[no << 1 | 1].val += node[no << 1 | 1].tag;
node[no].tag = 0;
}
void change(int l, int r, int no, long long num) {
if (node[no].r < l || node[no].l > r) return;
if (node[no].l >= l && node[no].r <= r) {
node[no].tag += num;
node[no].val += node[no].tag;
return;
}
if (node[no].tag != 0) {
pushdown(no);
}
change(l, r, no << 1, num);
change(l, r, no << 1 | 1, num);
up(no);
}
int main() {
int n, i;
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
build(1, n, 1);
for (i = 1; i <= n; i++) {
int x = node[1].num;
ans[x] = i;
change(x, x, 1, inf);
change(x + 1, n, 1, -i);
}
for (i = 1; i <= n; i++) {
printf("%d ", ans[i]);
}
printf("\n");
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int arr[200005];
int ans[200005];
pair<long long, int> tree[4 * 200005];
long long lazy[4 * 200005];
pair<long long, int> init(int start, int end, int node) {
if (start == end) return tree[node] = {arr[start], start};
int mid = (start + end) / 2;
pair<long long, int> l = init(start, mid, node * 2);
pair<long long, int> r = init(mid + 1, end, node * 2 + 1);
if (l.first < r.first) {
return tree[node] = l;
} else if (l.first > r.first) {
return tree[node] = r;
} else {
if (r.second > l.second) {
return tree[node] = r;
} else {
return tree[node] = l;
}
}
}
void update_lazy(int node, int start, int end) {
if (lazy[node] != 0) {
tree[node].first += lazy[node];
if (start != end) {
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
}
pair<long long, int> getmin(int start, int end, int node, int left, int right) {
update_lazy(node, start, end);
if (left > end || right < start) return {2123456789000, -1};
if (left <= start && end <= right) return tree[node];
int mid = (start + end) / 2;
pair<long long, int> l = getmin(start, mid, node * 2, left, right);
pair<long long, int> r = getmin(mid + 1, end, node * 2 + 1, left, right);
if (l.first < r.first) {
return l;
} else if (l.first > r.first) {
return r;
} else {
if (r.second > l.second) {
return r;
} else {
return l;
}
}
}
void update(int start, int end, int node, int left, int right, long long dif) {
update_lazy(node, start, end);
if (left > end || right < start) return;
if (left <= start && end <= right) {
tree[node].first += dif;
if (start != end) {
lazy[node * 2] += dif;
lazy[node * 2 + 1] += dif;
}
return;
}
if (start == end) return;
int mid = (start + end) / 2;
update(start, mid, node * 2, left, right, dif);
update(mid + 1, end, node * 2 + 1, left, right, dif);
pair<long long, int> l = tree[node * 2];
pair<long long, int> r = tree[node * 2 + 1];
if (l.first < r.first) {
tree[node] = l;
} else if (l.first > r.first) {
tree[node] = r;
} else {
if (r.second > l.second) {
tree[node] = r;
} else {
tree[node] = l;
}
}
}
int main() {
long long n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
init(0, n - 1, 1);
for (int i = 1; i <= n; i++) {
pair<long long, int> p = getmin(0, n - 1, 1, 0, n - 1);
ans[p.second] = i;
update(0, n - 1, 1, p.second, p.second, (n * (n - 1)) / 2 + 1);
update(0, n - 1, 1, p.second + 1, n - 1, -i);
}
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
cout << endl;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const long long LLINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
int segtree[MAXN << 2];
int lazy[MAXN << 2];
int arr[MAXN];
int ans[MAXN];
void pushup(int rt) {
segtree[rt] = min(segtree[rt << 1], segtree[rt << 1 | 1]);
}
void pushdown(int rt) {
if (lazy[rt]) {
segtree[rt << 1] = max(0, segtree[rt << 1] - lazy[rt]);
segtree[rt << 1 | 1] = max(0, segtree[rt << 1 | 1] - lazy[rt]);
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
lazy[rt] = 0;
}
}
void build(int l, int r, int rt) {
lazy[rt] = 0;
if (l == r) {
segtree[rt] = arr[l];
return;
}
int m = l + r >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
pushup(rt);
}
int query(int l, int r, int rt) {
if (l == r) {
return l;
}
int m = l + r >> 1;
pushdown(rt);
if (!segtree[rt << 1 | 1])
return query(m + 1, r, rt << 1 | 1);
else
return query(l, m, rt << 1);
}
void update(int l, int r, int L, int R, int C, int rt) {
if (L <= l && r <= R) {
segtree[rt] = max(0, segtree[rt] - C);
lazy[rt] += C;
return;
}
int m = l + r >> 1;
pushdown(rt);
if (L <= m) update(l, m, L, R, C, rt << 1);
if (R > m) update(m + 1, r, L, R, C, rt << 1 | 1);
pushup(rt);
}
void update(int l, int r, int pos, int rt) {
if (l == r && l == pos) {
segtree[rt] = INF;
return;
}
int m = l + r >> 1;
pushdown(rt);
if (pos <= m)
update(l, m, pos, rt << 1);
else
update(m + 1, r, pos, rt << 1 | 1);
pushup(rt);
}
signed main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &arr[i]);
}
build(1, n, 1);
int now = 0;
for (int i = n; i > 0; --i) {
int index = query(1, n, 1);
ans[index] = ++now;
if (index + 1 <= n) update(1, n, index + 1, n, now, 1);
update(1, n, index, 1);
}
for (int i = 1; i <= n; ++i) printf("%d ", ans[i]);
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import static java.lang.Math.*;
import java.util.*;
public class D {
static PrintWriter pw=new PrintWriter(System.out);
static BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st=new StringTokenizer(""," ");
static void solve(){
int n=readInt();
int[] a=new int[n];
Arrays.setAll(a,z->readInt());
ArrayList<Integer> list=new ArrayList(n);
HashMap<Integer,Integer> triangular=new HashMap<>();
for (int i = 1; i <= n; i++) {
triangular.put(i*(i-1)/2,i);
}
int prev=-1,k=0;
for (int i = a.length-1; i >=0; i--) {
if(prev==a[i])
{ k++;
list.add(triangular.get(a[i])+k);
}
else{
list.add(triangular.get(a[i]));
prev=a[i];
k=0;
}
}
for (int i = list.size()-1; i >= 0; i--) {
write(list.get(i));
}
}
public static void main(String... args){
solve();
pw.flush();
}
static String read() {
try {
if(st.hasMoreTokens())
return st.nextToken();
st=new StringTokenizer(bf.readLine()," ");
if(st.hasMoreTokens())
return st.nextToken();
}
catch (IOException e){e.printStackTrace();}
return "";
}
static int readInt(){
return Integer.parseInt(read());
}
static long readLong(){
return Long.parseLong(read());
}
static <T> void write(T s) {
pw.write(String.valueOf(s)+" ");
}
static <T> void writeln(T s) {
pw.write(String.valueOf(s)+"\n");
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
long long tree[maxn * 40], lazy[maxn * 40];
void creat(int l, int r, int cnt, long long val, int pos) {
if (l == r) {
tree[cnt] = val;
return;
}
int mid = (l + r) >> 1;
if (mid >= pos) {
creat(l, mid, (cnt << 1), val, pos);
} else {
creat(mid + 1, r, ((cnt << 1) + 1), val, pos);
}
tree[cnt] = min(tree[(cnt << 1)], tree[((cnt << 1) + 1)]);
}
void creat_len(int l, int r, int cnt, long long val, int ll, int rr) {
if (l >= ll && r <= rr) {
tree[cnt] += val;
lazy[cnt] += val;
return;
}
int mid = (l + r) >> 1;
if (mid >= ll) {
creat_len(l, mid, (cnt << 1), val, ll, rr);
}
if (mid + 1 <= rr) {
creat_len(mid + 1, r, ((cnt << 1) + 1), val, ll, rr);
}
tree[cnt] = min(tree[(cnt << 1)], tree[((cnt << 1) + 1)]) + lazy[cnt];
}
int find(int l, int r, int cnt, long long val) {
if (l == r) return l;
int mid = (l + r) >> 1;
if (val + tree[((cnt << 1) + 1)] + lazy[cnt] == 0)
return find(mid + 1, r, ((cnt << 1) + 1), val + lazy[cnt]);
return find(l, mid, (cnt << 1), val + lazy[cnt]);
}
int a[maxn];
int main() {
int n, m;
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%d", &m);
creat(1, n, 1, m, i);
}
for (int i = 1; i <= n; i++) {
int pos = find(1, n, 1, 0);
a[pos] = i;
creat_len(1, n, 1, 0x3f3f3f, pos, pos);
creat_len(1, n, 1, -i, pos, n);
}
for (int i = 1; i <= n; i++) {
cout << a[i] << " ";
}
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 5;
long long a[maxn], b[maxn];
bool vis[maxn];
uint64_t mysqrt(uint64_t a) {
uint64_t min = 0;
uint64_t max = ((uint64_t)1) << 32;
while (1) {
if (max <= 1 + min) return min;
uint64_t sqt = min + (max - min) / 2;
uint64_t sq = sqt * sqt;
if (sq == a) return sqt;
if (sq > a)
max = sqt;
else
min = sqt;
}
}
long long solve(long long v) {
long long x = (1LL + mysqrt(1LL - (4LL * (-v)))) / 2LL;
return x;
}
int32_t main(int32_t argc, char const *argv[]) {
long long n;
scanf("%lld", &n);
for (long long i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
b[n] = solve(2 * a[n]);
memset(vis, 0, sizeof vis);
vis[b[n]] = 1;
for (long long i = n - 1; i >= 1; i--) {
if (a[i] == a[i + 1]) {
long long j = b[i + 1];
while (vis[j] && j <= n) j++;
b[i] = j;
vis[j] = 1;
} else {
b[i] = solve(2 * a[i]);
vis[b[i]] = 1;
}
}
for (long long i = 1; i <= n; i++) {
printf("%lld ", b[i]);
}
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 100;
const long long inf = 1e18 + 10;
int n, m;
long long ST[maxn * 4], a[maxn * 4], c[maxn * 4], ans[maxn * 4], k[maxn * 4];
void push(int o, int l, int r) {
if (c[o]) {
long long cc = c[o];
c[o << 1] = cc;
c[(o << 1) | 1] = cc;
ST[o << 1] -= cc;
ST[(o << 1) | 1] -= cc;
c[o] = 0;
}
}
void up(int o) { ST[o] = max(ST[o << 1], ST[(o << 1) | 1]); }
void build(int o, int l, int r) {
if (l == r)
ST[o] = a[l];
else {
int mid = (l + r) >> 1;
build(o << 1, l, mid);
build((o << 1) | 1, mid + 1, r);
ST[o] = max(ST[o << 1], ST[(o << 1) | 1]);
}
}
void update(int o, int l, int r, int ql, int qr, long long cc) {
if (ql <= l && qr >= r) {
c[o] = cc;
ST[o] -= cc;
return;
}
push(o, l, r);
int mid = (l + r) >> 1;
if (ql <= mid) update(o << 1, l, mid, ql, qr, cc);
if (qr >= mid + 1) update((o << 1) | 1, mid + 1, r, ql, qr, cc);
up(o);
}
int query(int o, int l, int r, long long v) {
push(o, l, r);
if (l == r) return l;
int mid = (r + l) >> 1;
if (ST[o << 1] >= v)
return query(o << 1, l, mid, v);
else
return query((o << 1) | 1, mid + 1, r, v);
}
void g(int o, int l, int r, long long v) {
if (l == r)
ST[o] = -1;
else {
int mid = (r + l) >> 1;
if (mid >= v)
g(o << 1, l, mid, v);
else
g((o << 1) | 1, mid + 1, r, v);
up(o);
}
}
int main() {
int t;
cin >> n;
for (int i = 1; i <= n; i++) a[i] = a[i - 1] + i - 1;
build(1, 1, n);
for (int i = 1; i <= n; i++) cin >> k[i];
for (int i = n; i >= 1; i--) {
ans[i] = query(1, 1, n, k[i]);
g(1, 1, n, ans[i]);
update(1, 1, n, ans[i], n, ans[i]);
}
for (int i = 1; i <= n; i++) cout << ans[i] << " ";
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
/* Rajkin Hossain */
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class D{
//FastInput k = new FastInput(System.in);
FastInput k = new FastInput("/home/rajkin/Desktop/input.txt");
FastOutput z = new FastOutput();
int n;
int [] y;
SegmentTree tree;
int [] ans;
void startAlgorithm() {
tree = new SegmentTree(n);
for(int i = 1; i<=n; i++) {
Pair p = tree.tree[1];
ans[p.index] = i;
tree.update(p.index);
}
}
void startProgram() {
while(k.hasNext()) {
n = k.nextInt();
y = new int[n+1];
ans = new int[n+1];
for(int i = 1; i<=n; i++) {
y[i] = k.nextInt();
}
startAlgorithm();
}
z.flush();
System.exit(0);
}
class SegmentTree {
Pair [] tree;
int [] prop;
int treeLength;
public SegmentTree(int max_size) {
tree = new Pair[max_size * 4];
prop = new int[max_size * 4];
treeLength = max_size;
initialize(1, 1, treeLength);
}
public Pair getMin(Pair a, Pair b) {
if(a.value == b.value) {
if(a.index > b.index) {
return a;
}
else {
return b;
}
}
else if(a.value < b.value) {
return a;
}
return b;
}
public void initialize(int nodeId, int i, int j) {
if (i == j) {
tree[nodeId] = new Pair(i, y[i]);
return;
}
int left = nodeId * 2;
int right = nodeId * 2 + 1;
int mid = (i + j) / 2;
initialize(left, i, mid);
initialize(right, mid + 1, j);
tree[nodeId] = getMin(tree[left], tree[right]);
}
public void update(int index) {
update(1, 1, treeLength, index);
}
public void update(int nodeId, int i, int j, int index) {
if (i>index || j<index) {
return;
}
if (i==index && j==index) {
tree[nodeId] = new Pair(-1, Integer.MAX_VALUE);
return;
}
int left = nodeId * 2;
int right = nodeId * 2 + 1;
int mid = (i + j) / 2;
update(left, i, mid, index);
update(right, mid + 1, j, index);
tree[nodeId] = getMin(tree[left], tree[right]);
}
public void update(int b, int e, int value) {
update(1, 1, treeLength, b, e, value);
}
public void update(int nodeId, int i, int j, int b, int e, int value) {
if (i>e || j<b) {
return;
}
if (i>=b && j<=e) {
prop[nodeId] += value;
return;
}
int left = nodeId * 2;
int right = nodeId * 2 + 1;
int mid = (i + j) / 2;
update(left, i, mid, b, e, value);
update(right, mid + 1, j, b, e, value);
tree[nodeId] = getMin(tree[left], tree[right]);
}
}
class Pair {
int index;
int value;
public Pair(int index, int value) {
this.index = index;
this.value = value;
}
}
public static void main(String [] args) throws IOException {
new Thread(null, new Runnable(){
public void run(){
try{
new D().startProgram();
}
catch(Exception e){
e.printStackTrace();
}
}
},"Main",1<<28).start();
}
/* MARK: FastInput and FastOutput */
class FastInput {
BufferedReader reader;
StringTokenizer tokenizer;
FastInput(InputStream stream){
reader = new BufferedReader(new InputStreamReader(stream));
}
FastInput(String path){
try {
reader = new BufferedReader(new FileReader(path));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tokenizer = null;
}
String next() {
return nextToken();
}
String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
boolean hasNext(){
try {
return reader.ready();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
String nextToken() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
String line = null;
try {
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (line == null) {
return null;
}
tokenizer = new StringTokenizer(line);
}
return tokenizer.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.valueOf(nextToken());
}
double nextDouble() {
return Double.valueOf(nextToken());
}
}
class FastOutput extends PrintWriter {
FastOutput() {
super(new BufferedOutputStream(System.out));
}
public void debug(Object...obj) {
System.err.println(Arrays.deepToString(obj));
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
t=int(input())
l1=[int(num) for num in input().split()]
k=0
c=1
l3=[]
l=0
for i in range(1,t):
if(l1[i]==l1[i-1]) or (l1[i]==0):
c+=1
continue
else:
if(l1[i-1]==0):
if(i!=1):
l=c
c=c-1
p=int(((l1[i]-k)-(((c-1)*c)/2))/c)
for j in range(1,l+1):
l3.append(p+(c-j))
if(i==t-1):
l3.append(p+l)
else:
l=c
p=int(((l1[i]-k)-(((c-1)*c)/2))/c)
for j in range(1,l+1):
l3.append(p+(l-j))
if(i==t-1):
l3.append(p+c)
else:
l=c
p=int(((l1[i]-k)-(((c-1)*c)/2))/c)
for j in range(1,l+1):
l3.append(p+(l-j))
if(i==t-1):
l3.append(p+c)
c=1
k=l1[i]
if(c==t):
for i in range(1,t+1):
l3.append(t-i+1)
for i in range(0,t):
print(l3[i],end=" ")
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
class {
public:
pair<long long, long long> T[200009 << 2];
long long lz[200009 << 2];
long long n, f[200009], ans[200009];
void solve() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> f[i];
build(1, 1, n);
for (int i = 1; i <= n; ++i) {
auto res = query(1, 1, n, 1, n);
ans[res.second] = i;
update(1, 1, n, res.second + 1, n, -i);
update(1, 1, n, res.second, res.second, 1e18);
}
for (int i = 1; i <= n; ++i) cout << ans[i] << " ";
}
void build(int rt, int l, int r) {
lz[rt] = 0;
if (l == r) {
T[rt] = {f[l], l};
return;
}
int mid = l + r >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
up(rt);
}
void up(int rt) {
if (T[rt << 1].first == T[rt << 1 | 1].first) {
T[rt] = T[rt << 1 | 1];
} else {
T[rt] = min(T[rt << 1], T[rt << 1 | 1]);
}
}
void update(int rt, int l, int r, int ll, int rr, int v) {
if (ll > rr) return;
if (ll <= l && rr >= r) {
lz[rt] += v;
T[rt].first += v;
return;
}
int mid = l + r >> 1;
down(rt);
if (ll <= mid) update(rt << 1, l, mid, ll, rr, v);
if (rr > mid) update(rt << 1 | 1, mid + 1, r, ll, rr, v);
up(rt);
}
pair<long long, long long> query(int rt, int l, int r, int ll, int rr) {
if (ll <= l && rr >= r) {
return T[rt];
}
int mid = l + r >> 1;
down(rt);
pair<long long, long long> res = {1e18, 1e18};
if (ll <= mid) {
pair<long long, long long> ret = query(rt << 1, l, mid, ll, rr);
if (ret.first == res.first)
res.second = ret.second;
else
res = min(res, ret);
}
if (rr > mid) {
pair<long long, long long> ret = query(rt << 1 | 1, mid + 1, r, ll, rr);
if (ret.first == res.first)
res.second = ret.second;
else
res = min(res, ret);
}
return res;
}
void down(int rt) {
if (lz[rt]) {
T[rt << 1].first += lz[rt];
lz[rt << 1] += lz[rt];
T[rt << 1 | 1].first += lz[rt];
lz[rt << 1 | 1] += lz[rt];
lz[rt] = 0;
}
}
} NSPACE;
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
NSPACE.solve();
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
import sys
def main():
a = sys.stdin.read()
a = a.split('\n')
N = int(a[0])
input_values = [int(lol) for lol in a[1].split(' ')]
res = [0 for i in input_values]
i = N
index = 1
while i > 0:
i -= 1
if input_values[i] == 0 and res[i] == 0:
index = i
break
for i in range(1, N + 1):
res[index] = i
new_index = None
j = N
while j > index:
j -= 1
input_values[j] -= i
if input_values[j] == 0 and res[i] == 0 and not new_index:
new_index = j
if new_index:
index = new_index
else:
while j > 0:
j -= 1
if input_values[j] == 0 and res[j] == 0:
index = j
break
for i in res:
print(i, end=' ')
main()
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
from sys import stdin
from sys import setrecursionlimit as SRL; SRL(10**7)
rd = stdin.readline
rrd = lambda: map(int, rd().strip().split())
cnt = [0]*(1<<21)
n = int(input())
a = list(rrd())
def insert(x,y):
if cnt[x|y]>=2:
return
if x == 0:
cnt[y] += 1
return
insert(x&(x-1),y|(x&-x))
insert(x&(x-1),y)
def query(x):
ans = 0
for i in range(20,-1,-1):
if not x&(1<<i) and cnt[ans|(1<<i)]>=2:
ans |= (1<<i)
return x|ans
ans = 0
for i in range(n-1,-1,-1):
if n-i>=3:
ans = max(ans,query(a[i]))
insert(a[i],0)
print(ans)
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.util.*;
public class algo_2404
{
public static void main(String args[])
{
Scanner ex=new Scanner(System.in);
int n=ex.nextInt();
long s[]=new long[n];
int ans[]=new int[n];
for(int i=0;i<n;i++)
s[i]=ex.nextLong();
TreeMap<Long,ArrayList<Integer>> tmap=new TreeMap<Long,ArrayList<Integer>>();
for(int i=0;i<n;i++)
{
if(tmap.containsKey(s[i]))
tmap.get(s[i]).add(i);
else
{
ArrayList<Integer> temp=new ArrayList<Integer>();
temp.add(i);
tmap.put(s[i],temp);
}
}
Set<Map.Entry<Long,ArrayList<Integer>>> set = tmap.entrySet();
for(Map.Entry<Long,ArrayList<Integer>> me : set)
Collections.sort(tmap.get(me.getKey()));
for(int i=0;i<n;i++)
{
long num=tmap.firstKey();
int size=tmap.get(num).size();
ans[tmap.get(num).get(size-1)]=i+1;
if(size==1)
tmap.remove(num);
else
tmap.get(num).remove(size-1);
}
StringBuilder str=new StringBuilder();
for(int i=0;i<n;i++)
str.append(ans[i]+" ");
System.out.println(str.toString());
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
inline void Boost() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int NMax = 2e5 + 50;
const long long int INF = LLONG_MAX / 2;
struct Node {
long long int val, mn, pos;
};
Node aint[4 * NMax];
long long int lazy[4 * NMax];
void push(int node, int lo, int hi) {
aint[node].mn -= lazy[node];
if (lo != hi) {
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
void update(int node, int lo, int hi, int pos, long long int val) {
push(node, lo, hi);
if (lo == hi) {
aint[node].val += val;
aint[node].mn = aint[node].val;
aint[node].pos = pos;
} else {
int mid = (lo + hi) / 2;
if (pos <= mid) {
update(node * 2, lo, mid, pos, val);
} else {
update(node * 2 + 1, mid + 1, hi, pos, val);
}
if (aint[node * 2 + 1].mn <= aint[node * 2].mn) {
aint[node].mn = aint[node * 2 + 1].mn;
aint[node].pos = aint[node * 2 + 1].pos;
} else {
aint[node].mn = aint[node * 2].mn;
aint[node].pos = aint[node * 2].pos;
}
}
}
void update(int node, int lo, int hi, int b, int e, long long int val) {
push(node, lo, hi);
if (lo > hi || lo > e || hi < b) return;
if (lo >= b && hi <= e) {
lazy[node] = val;
push(node, lo, hi);
} else {
int mid = (lo + hi) / 2;
update(node * 2, lo, mid, b, e, val);
update(node * 2 + 1, mid + 1, hi, b, e, val);
if (aint[node * 2 + 1].mn <= aint[node * 2].mn) {
aint[node].mn = aint[node * 2 + 1].mn;
aint[node].pos = aint[node * 2 + 1].pos;
} else {
aint[node].mn = aint[node * 2].mn;
aint[node].pos = aint[node * 2].pos;
}
}
}
Node query(int node, int lo, int hi, int pos) {
if (lo == hi) {
return aint[node];
} else {
int mid = (lo + hi) / 2;
if (pos <= mid) {
return query(node * 2, lo, mid, pos);
} else {
return query(node * 2 + 1, mid + 1, hi, pos);
}
}
}
int main() {
Boost();
int n;
cin >> n;
vector<long long int> v(n + 5);
for (int i = 1; i <= n; ++i) {
cin >> v[i];
update(1, 1, n, i, v[i]);
}
for (int i = 1; i <= n; ++i) {
int pos = aint[1].pos;
v[pos] = i;
Node self = query(1, 1, n, pos);
update(1, 1, n, pos, INF);
update(1, 1, n, pos, n, -self.val);
}
for (int i = 1; i <= n; ++i) cout << v[i] << " ";
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class Manthan19D {
public static void main(String[] args) {
JS scan = new JS();
PrintWriter out = new PrintWriter(System.out);
int n = scan.nextInt();
dood[] arr =new dood[n];
for(int i = 0;i<n;i++){
arr[i]=new dood(i,scan.nextInt());
}
int start = 0;
ArrayList[] list = new ArrayList[n];
for(int i = 0;i<n;i++){
list[i] = new ArrayList<Integer>();
}
int[] ans = new int[n];
Arrays.sort(arr);
int last = 0;
for(int i = 0;i<n;i++){
dood curr = arr[i];
if(last!=curr.val){
start++;
}
list[start].add(curr.idx);
last = curr.val;
}
int at = 1;
for(int i = 0;i<n;i++){
for(int j = list[i].size()-1;j>=0;j--){
//System.out.println(i+" "+j+" "+list[i].size());
ans[(int) list[i].get(j)] = at;
at++;
}
}
for(int i = 0;i<n;i++){
out.print(ans[i]+" ");
}
out.flush();
out.close();
}
static class dood implements Comparable<dood>{
int idx, val;
public dood(int idx1, int val1){
idx = idx1;
val = val1;
}
@Override
public int compareTo(dood o) {
return val-o.val;
}
}
static class JS{
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public JS() {
in = new BufferedInputStream(System.in, BS);
}
public JS(String s) throws FileNotFoundException {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
public char nextChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
num=1;
boolean neg = false;
if(c==NC)c=nextChar();
for(;(c<'0' || c>'9'); c = nextChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=nextChar()) {
res = (res<<3)+(res<<1)+c-'0';
num*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/num;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c>32) {
res.append(c);
c=nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c!='\n') {
res.append(c);
c=nextChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=nextChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
class {
public:
pair<int, int> T[200009 << 2];
int lz[200009 << 2];
int n, f[200009], ans[200009];
void solve() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> f[i];
build(1, 1, n);
for (int i = 1; i <= n; ++i) {
auto res = query(1, 1, n, 1, n);
ans[res.second] = i;
update(1, 1, n, res.second + 1, n, -i);
update(1, 1, n, res.second, res.second, 0xfffffff);
}
for (int i = 1; i <= n; ++i) cout << ans[i] << " ";
}
void build(int rt, int l, int r) {
lz[rt] = 0;
if (l == r) {
T[rt] = {f[l], l};
return;
}
int mid = l + r >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
up(rt);
}
void up(int rt) {
if (T[rt << 1].first == T[rt << 1 | 1].first) {
T[rt] = T[rt << 1 | 1];
} else {
T[rt] = min(T[rt << 1], T[rt << 1 | 1]);
}
}
void update(int rt, int l, int r, int ll, int rr, int v) {
if (ll > rr) return;
if (ll <= l && rr >= r) {
lz[rt] += v;
T[rt].first += v;
return;
}
int mid = l + r >> 1;
down(rt);
if (ll <= mid) update(rt << 1, l, mid, ll, rr, v);
if (rr > mid) update(rt << 1 | 1, mid + 1, r, ll, rr, v);
up(rt);
}
pair<int, int> query(int rt, int l, int r, int ll, int rr) {
if (ll <= l && rr >= r) {
return T[rt];
}
int mid = l + r >> 1;
down(rt);
pair<int, int> res = {0xfffffff, 0xfffffff};
if (ll <= mid) {
pair<int, int> ret = query(rt << 1, l, mid, ll, rr);
if (ret.first == res.first)
res.second = ret.second;
else
res = min(res, ret);
}
if (rr > mid) {
pair<int, int> ret = query(rt << 1 | 1, mid + 1, r, ll, rr);
if (ret.first == res.first)
res.second = ret.second;
else
res = min(res, ret);
}
return res;
}
void down(int rt) {
if (lz[rt]) {
T[rt << 1].first += lz[rt];
lz[rt << 1] += lz[rt];
T[rt << 1 | 1].first += lz[rt];
lz[rt << 1 | 1] += lz[rt];
lz[rt] = 0;
}
}
} NSPACE;
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
NSPACE.solve();
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
const int N = 1e6 + 10;
const int INF = 1e9;
const int MOD = 998244353;
const int lgN = 20;
using namespace std;
int tree[4 * N], lz[4 * N], v[N], w[N];
void build(int node, int st, int en) {
lz[node] = 0;
if (st == en) {
tree[node] = v[st];
return;
}
int m = (st + en) / 2;
build(2 * node + 1, st, m);
build(2 * node + 2, m + 1, en);
tree[node] = min(tree[2 * node + 1], tree[2 * node + 2]);
}
void pushdown(int node, int st, int en) {
if (lz[node] > 0) {
tree[node] -= lz[node];
if (st < en) {
lz[2 * node + 1] += lz[node];
lz[2 * node + 2] += lz[node];
}
lz[node] = 0;
}
}
void pupd(int pos, int val, int node, int st, int en) {
pushdown(node, st, en);
if (st == en) {
tree[node] = val;
return;
}
int m = (st + en) / 2;
pushdown(2 * node + 1, st, m);
pushdown(2 * node + 2, m + 1, en);
if (pos <= m)
pupd(pos, val, 2 * node + 1, st, m);
else
pupd(pos, val, 2 * node + 2, m + 1, en);
tree[node] = min(tree[2 * node + 1], tree[2 * node + 2]);
}
void upd(int l, int r, int node, int st, int en) {
pushdown(node, st, en);
if (st > r || en < l) return;
int m = (st + en) / 2;
if (st >= l && en <= r) {
tree[node]--;
if (st < en) {
lz[2 * node + 1]++;
lz[2 * node + 2]++;
}
return;
}
upd(l, r, 2 * node + 1, st, m);
upd(l, r, 2 * node + 2, m + 1, en);
tree[node] = min(tree[2 * node + 1], tree[2 * node + 2]);
}
int query(int node, int st, int en) {
pushdown(node, st, en);
if (st == en) return st;
int m = (st + en) / 2;
pushdown(2 * node + 1, st, m);
pushdown(2 * node + 2, m + 1, en);
if (tree[2 * node + 2] <= tree[2 * node + 1])
return query(2 * node + 2, m + 1, en);
return query(2 * node + 1, st, m);
}
void solve() {
int n, i, go = 1;
cin >> n;
for (i = 0; i < n; i++) cin >> v[i];
build(0, 0, n - 1);
while (go <= n) {
int ans = query(0, 0, n - 1);
w[ans] = go;
go++;
if (ans + 1 < n) upd(ans + 1, n - 1, 0, 0, n - 1);
pupd(ans, INF, 0, 0, n - 1);
}
for (i = 0; i < n; i++) cout << w[i] << " \n"[i == n - 1];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int T = 1;
for (int t = 1; t <= T; t++) {
solve();
}
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
class Tree:
#responsible for interval [a, b)
def __init__(self, a, b, arr):
self.a = a
self.b = b
self.to_subtract = 0
self.is_leaf = ((b - a) == 1)
if not self.is_leaf:
mid = (a + b) // 2
self.left = Tree(a, mid, arr)
self.right = Tree(mid, b, arr)
self.mval = self.right.mval if self.right.mval[0] <= self.left.mval[0] else self.left.mval
else:
self.mval = (arr[a], a)
#remove delta from all elements in interval [a, b)
def subtract_from_range(self, a, b, delta):
if b <= self.a or a >= self.b:
return
if a <= self.a and b >= self.b:
self.to_subtract += delta
return
self.left.subtract_from_range(a, b, delta)
self.right.subtract_from_range(a, b, delta)
def remove_element(self, i):
if i >= self.a and i < self.b:
if not self.is_leaf:
self.left.to_subtract += self.to_subtract
self.right.to_subtract += self.to_subtract
self.to_subtract = 0
self.left.remove_element(i)
self.right.remove_element(i)
self.mval = (self.right.mval[0] - self.right.to_subtract, self.right.mval[1]) if self.right.mval[0] - self.right.to_subtract <= self.left.mval[0] - self.left.to_subtract else (self.left.mval[0] - self.left.to_subtract, self.left.mval[1])
else:
self.mval = (10 ** 6, self.a)
def print_tree(self):
print(f'a: {self.a}, b: {self.b}, to_subtract: {self.to_subtract}, is_leaf: {self.is_leaf} mval: {self.mval}')
if not self.is_leaf:
self.left.print_tree()
self.right.print_tree()
n = int(input())
f_perm = list(map(int, input().split()))
t = Tree(0, n, f_perm)
perm = [-1] * n
for el in range(1, n + 1):
_, pos = t.mval
perm[pos] = el
t.subtract_from_range(pos+1, 10, el)
t.remove_element(pos)
for el in perm:
print(el, end = ' ')
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-9, PI = acos(-1.);
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 5;
int n, ans[N];
long long mi[4 * N], pos[4 * N], v[N], lz[4 * N];
void build(int p, int l, int r) {
if (l == r) {
mi[p] = v[l], pos[p] = l;
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m + 1, r);
mi[p] = min(mi[2 * p], mi[2 * p + 1]);
if (mi[p] == mi[2 * p + 1])
pos[p] = pos[2 * p + 1];
else
pos[p] = pos[2 * p];
}
void push(int p, int l, int r) {
if (lz[p]) {
mi[p] += lz[p];
if (l != r) {
lz[2 * p] += lz[p];
lz[2 * p + 1] += lz[p];
}
lz[p] = 0;
}
}
pair<long long, long long> query(int p, int l, int r, int i, int j) {
push(p, l, r);
if (r < i or l > j) assert(0);
if (i <= l and r <= j) return {mi[p], pos[p]};
int m = (l + r) / 2;
if (m > j) return query(2 * p, l, m, i, j);
if (m < i) return query(2 * p + 1, m + 1, r, i, j);
pair<long long, long long> a = query(2 * p, l, m, i, j);
pair<long long, long long> b = query(2 * p + 1, m + 1, r, i, j);
if (a.first < b.first)
return a;
else
return b;
}
void update(int p, int l, int r, int i, int j, int v) {
push(p, l, r);
if (r < i or l > j) return;
if (i <= l and r <= j) {
lz[p] = v;
push(p, l, r);
return;
}
update(2 * p, l, (l + r) / 2, i, j, v);
update(2 * p + 1, (l + r) / 2 + 1, r, i, j, v);
mi[p] = min(mi[2 * p], mi[2 * p + 1]);
if (mi[p] == mi[2 * p + 1])
pos[p] = pos[2 * p + 1];
else
pos[p] = pos[2 * p];
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &v[i]);
build(1, 1, n);
memset(ans, -1, sizeof(ans));
for (int i = 1; i <= n; i++) {
auto p = query(1, 1, n, 1, n);
ans[p.second] = i;
update(1, 1, n, p.second + 1, n, -i);
update(1, 1, n, p.second, p.second, LINF);
}
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
printf("\n");
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
t=int(input())
l1=[int(num) for num in input().split()]
k=0
c=1
l3=[]
l=0
for i in range(1,t):
if(l1[i]==l1[i-1]):
c+=1
continue
else:
l=c
p=int(((l1[i]-k)-(((c-1)*c)/2))/c)
for j in range(1,l+1):
l3.append(p+(l-j))
if(i==t-1):
l3.append(p+c)
c=1
k=l1[i]
if(c==t):
for i in range(1,t+1):
l3.append(t-i+1)
for i in range(0,t):
print(l3[i],end=" ")
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python2
|
n=input()
lst=map(int,raw_input().split())
from copy import *
lst2=deepcopy(list(set(lst)))
lst2.sort(reverse=True)
val=[]
ck={}
p=0
ans=[0]*(n)
for i in lst2:
ck[i]=p
p=p+1
for i in range(0,n):
val.append([])
f=[]
for i in range(0,n):
val[ck[lst[i]]].append(i)
ok=n
for tlst in val:
for j in range(0,len(tlst)):
ans[tlst[j]]=ok
ok=ok-1
for i in ans:
print i,
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class Main {
private static final int MAXN = 5000;
private static final String NO = "NO";
private static final String YES = "YES";
InputStream is;
PrintWriter out;
String INPUT = "";
private static long MOD;
private static final int MAX = Integer.MAX_VALUE / 4;
long BIT[];
void update(int ind, int vl) {
for (int i = ind; i < BIT.length; i += (i & -i))
BIT[i] += vl;
}
long query(int ind) {
long ans = 0;
for (int i = ind; i >= 1; i -= (i & -i)) {
ans += BIT[i];
}
return ans;
}
int search(long prefSum) {
int num = 0;
long sum = 0;
for (int i = 21; i >= 0; --i) {
if ((num + (1 << i) < BIT.length) && (sum + BIT[num + (1 << i)] <= prefSum)) {
num += (1 << i);
sum += BIT[num];
}
}
return num + 1;
}
void solve() {
int N = ni();
BIT = new long[N + 1];
for (int i = 1; i <= N; i++)
update(i, i);
int pre[] = na(N);
for (int i = N - 1; i >= 0; i--) {
pre[i] = search(pre[i]);
update(pre[i], -pre[i]);
}
for (int i : pre)
out.print(i + " ");
}
long power(long a, long b) {
long x = 1, y = a;
while (b > 0) {
if (b % 2 != 0) {
x = (x * y) % MOD;
}
y = (y * y) % MOD;
b /= 2;
}
return x % MOD;
}
private long gcd(long a, long b) {
while (a != 0) {
long tmp = b % a;
b = a;
a = tmp;
}
return b;
}
void run() throws Exception {
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if (!INPUT.isEmpty())
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Main().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private boolean vis[];
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
// ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n) {
if (!(isSpaceChar(b)))
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private Integer[] na2(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private int[][] na(int n, int m) {
int[][] a = new int[n][];
for (int i = 0; i < n; i++)
a[i] = na(m);
return a;
}
private Integer[][] na2(int n, int m) {
Integer[][] a = new Integer[n][];
for (int i = 0; i < n; i++)
a[i] = na2(m);
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) {
System.out.println(Arrays.deepToString(o));
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long int n;
long long int tree[200001 * 10];
long int a[200002];
void build(long int node, long int start, long int end) {
if (start == end) {
tree[node] = a[start];
} else {
long int mid = (start + end) / 2;
build(2 * node + 1, start, mid);
build(2 * node + 2, mid + 1, end);
tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
}
}
void update(long int node, long int start, long int end, long int ind) {
if (start == end)
tree[node] = a[ind];
else {
long int mid = (start + end) / 2;
if (start <= ind && ind <= mid) {
update(2 * node + 1, start, mid, ind);
} else {
update(2 * node + 2, mid + 1, end, ind);
}
tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
}
}
long long int query(long int node, long int start, long int end, long int l,
long int r) {
if (r < start || end < l) {
return 0;
}
if (l <= start && end <= r) {
return tree[node];
}
long int mid = (start + end) / 2;
long long int p1 = query(2 * node + 1, start, mid, l, r);
long long int p2 = query(2 * node + 2, mid + 1, end, l, r);
return (p1 + p2);
}
long int idx[200002];
long int root(long int x) {
while (idx[x] != x) {
idx[x] = idx[idx[x]];
x = idx[x];
}
return x;
}
int main() {
cin >> n;
long int ans = 0;
for (long int i = 0; i <= n + 1; i++) a[i] = i, idx[i] = i;
build(0, 0, n);
long long int b[n];
for (long int i = 0; i < n; i++) cin >> b[i];
vector<long int> x;
for (long int i = n - 1; i >= 0; i--) {
long int l = 0, r = n - 1;
long int ans = -1;
while (l <= r) {
long int mid = (l + r) / 2;
long long int temp1 = query(0, 0, n - 1, 0, mid);
if (temp1 == b[i]) {
ans = mid + 1;
break;
} else if (temp1 < b[i]) {
l = mid + 1;
} else
r = mid - 1;
}
ans = root(ans);
x.push_back(ans);
a[ans] = 0;
update(0, 0, n, ans);
idx[root(ans)] = root(ans + 1);
}
for (long int i = 0; i < x.size(); i++) cout << x[x.size() - 1 - i] << " ";
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int maxN = 1e6 + 10;
int tree[4 * maxN];
int n, p[maxN], s[maxN];
void update(int x, int l, int r, int k, int w) {
if (l == r)
tree[x] = w;
else {
int mid = (l + r) / 2;
if (k <= mid)
update(2 * x, l, mid, k, w);
else
update(2 * x + 1, mid + 1, r, k, w);
tree[x] = tree[2 * x] + tree[2 * x + 1];
}
}
void build(int x, int l, int r) {
if (l == r)
tree[x] = p[l];
else {
int mid = (l + r) / 2;
build(2 * x, l, mid);
build(2 * x + 1, mid + 1, r);
tree[x] = tree[2 * x] + tree[2 * x + 1];
}
}
int query(int x, int l, int r, int i, int j) {
if (l > j || r < i)
return 0;
else if (l >= i && r <= j)
return tree[x];
int mid = (l + r) / 2;
return query(2 * x, l, mid, i, j) + query(2 * x + 1, mid + 1, r, i, j);
}
void input() {
cin >> n;
for (int i = int(1); i <= int(n); ++i) p[i] = i;
for (int i = int(1); i <= int(n); ++i) cin >> s[i];
}
void solve() {
build(1, 1, n);
for (int i = int(n); i >= int(1); --i) {
int l = 0, r = n, mid, tmp;
while (r - l > 1) {
mid = (l + r) / 2;
if (mid == 0)
tmp = 0;
else
tmp = query(1, 1, n, 1, mid);
if (tmp <= s[i]) {
l = mid;
} else
r = mid;
}
p[i] = l + 1;
update(1, 1, n, l + 1, 0);
}
}
void output() {
for (int i = int(1); i <= int(n); ++i) cout << p[i] << " ";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
input();
solve();
output();
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class Test {
static int readInt() {
int ans = 0;
boolean neg = false;
try {
boolean start = false;
for (int c = 0; (c = System.in.read()) != -1; ) {
if (c == '-') {
start = true;
neg = true;
continue;
} else if (c >= '0' && c <= '9') {
start = true;
ans = ans * 10 + c - '0';
} else if (start) break;
}
} catch (IOException e) {
}
return neg ? -ans : ans;
}
static long readLong() {
long ans = 0;
boolean neg = false;
try {
boolean start = false;
for (int c = 0; (c = System.in.read()) != -1; ) {
if (c == '-') {
start = true;
neg = true;
continue;
} else if (c >= '0' && c <= '9') {
start = true;
ans = ans * 10 + c - '0';
} else if (start) break;
}
} catch (IOException e) {
}
return neg ? -ans : ans;
}
static String readLine() {
StringBuilder b = new StringBuilder();
try {
boolean start = false;
for (int c = 0; (c = System.in.read()) != -1; ) {
if (Character.isLetterOrDigit(c)) {
start = true;
b.append((char)c);
} else if (start) break;
}
} catch (IOException e) {
}
return b.toString();
}
static PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
void start() {
int n = readInt();
long[] s = new long[n+1];
Map<Long, Stack<Integer>> sum = new HashMap<>();
for (int i = 1; i <= n; i++) {
s[i] = readInt();
sum.computeIfAbsent(s[i], x -> new Stack<>()).push(i);
}
int[] ans = new int[n+1];
TreeMap<Integer, Integer> pos = new TreeMap<>();
long total = 0;
loop:
for (int i = 1; i <= n; i++) {
long c = total;
for (int k : pos.keySet()) {
Stack<Integer> stk = sum.get(c);
if (stk != null && !stk.isEmpty() && stk.peek() > -k) {
int p = stk.pop();
ans[p] = i;
pos.put(-p, i);
total += i;
continue loop;
}
c -= pos.get(k);
}
if (sum.get(0L).isEmpty()) {
writer.println(i + " ");
return;
}
int p = sum.get(c).pop();
ans[p] = i;
pos.put(-p, i);
total += i;
}
for (int i = 1; i <= n; i++) writer.print(ans[i] + " ");
}
public static void main(String[] args) {
Test te = new Test();
te.start();
writer.flush();
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int maxn = 2e5 + 10;
int a[maxn], n, ans[maxn];
struct node {
int l, r;
long long lazy, min;
} t[maxn << 2];
void build(int u, int l, int r) {
t[u].l = l, t[u].r = r;
t[u].lazy = 0;
if (l == r) {
t[u].min = a[l];
return;
}
int mid = (l + r) >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
t[u].min = min(t[u << 1].min, t[u << 1 | 1].min);
}
void pushdown(int u) {
t[u << 1].lazy += t[u].lazy;
t[u << 1 | 1].lazy += t[u].lazy;
t[u << 1].min += t[u].lazy;
t[u << 1 | 1].min += t[u].lazy;
t[u].lazy = 0;
}
int find(int u) {
int l = t[u].l, r = t[u].r;
if (l == r) {
t[u].min = inf;
return l;
}
pushdown(u);
int ans;
if (t[u << 1 | 1].min == 0)
ans = find(u << 1 | 1);
else
ans = find(u << 1);
t[u].min = min(t[u << 1].min, t[u << 1 | 1].min);
return ans;
}
void update(int u, int L, int R, int v) {
int l = t[u].l, r = t[u].r;
if (l == L && R == r) {
t[u].min += v;
t[u].lazy += v;
return;
}
pushdown(u);
int mid = (l + r) >> 1;
if (R <= mid)
update(u << 1, L, R, v);
else if (L > mid)
update(u << 1 | 1, L, R, v);
else {
update(u << 1, L, mid, v);
update(u << 1 | 1, mid + 1, R, v);
}
}
signed main() {
ios::sync_with_stdio(NULL);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
build(1, 1, n);
for (int i = 1; i <= n; ++i) {
int cur = find(1);
ans[cur] = i;
update(1, cur, n, -i);
}
for (int i = 1; i <= n; ++i) cout << ans[i] << (i == n ? '\n' : ' ');
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n;
long long t[N];
long long s[N], a[N];
int p[N];
void inc(int i, long long delta) {
for (; i < n; i = (i | (i + 1))) t[i] += delta;
}
void init() {
for (int i = 0; i < n; ++i) t[i] = 0;
for (int i = 0; i < n; ++i) inc(i, a[i]);
}
long long sum(int r) {
long long result = 0;
for (; r >= 0; r = (r & (r + 1)) - 1) result += t[r];
return result;
}
long long sum(int l, int r) { return sum(r) - sum(l - 1); }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%lld", s + i);
for (int i = 0; i < n; ++i) a[i] = i;
init();
for (int i = n; i > 0; --i) {
int l = 1, r = n, m;
while (r - l > 1) {
m = (l + r) / 2;
int calc = sum(m - 1);
if (calc > s[i])
r = m;
else
l = m;
}
if (sum(r - 1) == s[i]) {
p[i] = r;
inc(r, -r);
} else {
p[i] = l;
inc(l, -l);
}
}
for (int i = 1; i <= n; ++i) printf("%d ", p[i]);
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
n = int(input())
arr = list(map(int, input().split()))
res = [0]*n
for i in range(n-1,-1,-1):
le = int((2*arr[i])**0.5)
res[i] = le+1
index = n-1
while index >= 0:
temp = res[index]
l = 0
while res[index] == temp and index >= 0:
res[index] = str(temp+l)
l += 1
index -= 1
print(' '.join(res))
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
#from fractions import Fraction
from collections import defaultdict
from math import*
import os
import sys
from io import BytesIO, IOBase
from heapq import nlargest
from bisect import*
import copy
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def input(): return sys.stdin.readline().rstrip("\r\n")
#-------------above part copied-----------------------
n=int(input())
arr=list(map(int,input().split()))
brr=copy.deepcopy(arr)
brr.sort()
d=defaultdict(int)
for i in range(n):
d[brr[i]]=i+1
for i in range(n):
print(d[arr[i]],end=' ')
d[arr[i]]-=1
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
long long int pres[500005];
long long int n;
bool viz[500005];
long long int lampard(long long int x) {
long long int ans = -1;
long long int lo = 1;
long long int hi = n;
long long int mid;
while (lo < hi) {
mid = (lo + hi + 1) / 2;
if (pres[mid] > x)
hi = mid - 1;
else
lo = mid;
if (pres[lo] == x) break;
}
ans = lo;
if (viz[lo] == true) ans = -1;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t = 1;
while (t--) {
long long int m = 0, k = 0, i = 0, j = 0, p = 0, q = 0, x = 0, y = 0, z = 0,
cnt = 0, l = 0, r = 0, mid = 0, lo = 0, hi = 0;
string s;
bool flag = false;
cin >> n;
long long int a[n];
for (i = 0; i <= n - 1; i++) cin >> a[i];
pres[0] = 1000000007;
pres[1] = 0;
for (i = 2; i <= n; i++) pres[i] = pres[i - 1] + (i - 1);
long long int ans[n];
long long int mx = n;
memset(viz, false, sizeof(viz));
j = n;
y = n;
long long int buf = 0;
for (i = n - 1; i >= 0; i--) {
x = -1;
x = lampard(a[i]);
if (x == -1) x = lampard(buf + a[i]);
viz[x] = true;
ans[i] = x;
if (x == mx) {
buf = 0;
while (viz[mx] == true) mx--;
} else
buf += x;
}
for (i = 0; i < n; i++) cout << ans[i] << " ";
cout << endl;
}
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class F {
public static long[] min;
public static long[] t;
public static void push(int v){
t[v*2] += t[v];
t[v*2+1] += t[v];
}
public static long get_min(int v, int vl, int vr, int ql, int qr){
if(ql <= vl && vr <= qr){
return get_val(v);
}
if(qr <= vl || ql >= vr){
return Long.MAX_VALUE;
}
push(v);
long ans = Math.min(get_min(v*2, vl, (vl+vr)/2, ql, qr), get_min(v*2+1, (vl+vr)/2, vr, ql, qr));
min[v] = Math.min(get_val(v*2), get_val(v*2+1));
return ans;
}
public static int find(int v, int vl, int vr){
if(vl == vr - 1){
return v;
}
push(v);
if(get_min(v * 2 + 1, (vl + vr) / 2, vr, (vl + vr) / 2, vr) == 0){
return find(v * 2 + 1, (vl + vr) / 2, vr);
}
else{
return find(v*2, vl, (vl+vr)/2);
}
}
public static void add(int v, int vl, int vr, int ql, int qr, int x){
if(ql <= vl && qr >= vr){
t[v] += x;
return;
}
else if(qr <= vl || ql >= vr){
return;
}
push(v);
add(v*2, vl, (vl+vr)/2, ql, qr,x);
add(v*2+1, (vl+vr)/2, vr, ql, qr,x);
min[v] = Math.min(get_val(v*2), get_val(v*2+1));
}
public static long get_val(int v){
long start = min[v];
return Math.max(start - t[v], 0);
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
int n = Integer.parseInt(reader.readLine());
long[] array = new long[n];
StringTokenizer st = new StringTokenizer(reader.readLine());
for(int i = 0; i < n; ++i){
array[i] = Long.parseLong(st.nextToken());
}
int new_n = n;
int counter = 0;
while(new_n > 0){
new_n /= 2;
++counter;
}
new_n = 1 << counter;
min = new long[new_n * 2];
t = new long[new_n * 2];
for(int i = 0; i < min.length; ++i){
min[i] = Long.MAX_VALUE;
}
int[] ans = new int[n];
int now = 1;
for(int i = new_n; i < new_n + n; ++i){
min[i] = array[i - new_n];
}
for(int i = new_n - 1; i >= 0; --i){
min[i] = Math.min(min[i * 2], min[i * 2 + 1]);
}
for(int i = 1; i <= n; ++i){
int node = find(1, 0, new_n) - new_n;
ans[node] = now;
min[node + new_n] = Long.MAX_VALUE;
int ver = node + new_n;
while(ver != 1){
ver/= 2;
min[ver] = Math.min(min[ver * 2], min[ver * 2 + 1]);
}
add(1, 0, new_n, node + 1, new_n, now);
++now;
}
for(int i = 0; i < n; ++i){
writer.print(ans[i] + " ");
}
writer.close();
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long f(long long x) { return (x * (x + 1)) / 2; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
map<long long, stack<int> > mapper;
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
mapper[x].push(i);
}
vector<int> ans(n);
for (int i = 0; i < n; i++) {
long long c = f(i);
auto it = mapper.upper_bound(c);
it--;
stack<int> &st = (*it).second;
int pos = st.top();
st.pop();
ans[pos] = i;
if (((int)st.size()) == 0) mapper.erase(it);
}
for (int i = 0; i < n; i++) cout << ans[i] + 1 << " ";
cout << '\n';
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int value, index;
Node() {}
Node(int v, int i) {
value = v;
index = i;
}
Node(const Node &node) {
value = node.value;
index = node.index;
}
};
class SegmentTree {
vector<Node> tree;
int arrLength;
vector<int> lazy;
Node cmpNodes(Node leftChild, Node rightChild) {
if (leftChild.value < rightChild.value) {
return leftChild;
}
return rightChild;
}
Node constructTree(int *arr, int leftInd, int rightInd, int treeInd) {
if (rightInd == leftInd) {
tree[treeInd] = Node(arr[leftInd], leftInd);
} else {
Node leftChild = constructTree(arr, leftInd, (rightInd + leftInd) / 2,
treeInd * 2 + 1);
Node rightChild = constructTree(arr, (rightInd + leftInd) / 2 + 1,
rightInd, treeInd * 2 + 2);
tree[treeInd] = cmpNodes(leftChild, rightChild);
}
return tree[treeInd];
}
void _updateLz(int leftIndex, int rightIndex, int value, int treeIndex,
int currLeft, int currRight) {
if ((currLeft >= leftIndex) && (currRight <= rightIndex)) {
lazy[treeIndex] += value;
tree[treeIndex].value += value;
} else if ((currRight >= leftIndex) && (currLeft <= rightIndex)) {
_updateLz(leftIndex, rightIndex, value, treeIndex * 2 + 1, currLeft,
(currLeft + currRight) / 2);
_updateLz(leftIndex, rightIndex, value, treeIndex * 2 + 2,
(currLeft + currRight) / 2 + 1, currRight);
tree[treeIndex] =
cmpNodes(tree[2 * treeIndex + 1], tree[2 * treeIndex + 2]);
}
}
Node _queryLz(int leftIndex, int rightIndex, int treeIndex, int currLeft,
int currRight, int lz) {
if ((currLeft >= leftIndex) && (currRight <= rightIndex)) {
lazy[treeIndex] += lz;
tree[treeIndex].value += lz;
return tree[treeIndex];
} else if ((currRight >= leftIndex) && (currLeft <= rightIndex)) {
Node leftNode =
_queryLz(leftIndex, rightIndex, treeIndex * 2 + 1, currLeft,
(currLeft + currRight) / 2, lz + lazy[treeIndex]);
Node rightNode = _queryLz(leftIndex, rightIndex, treeIndex * 2 + 2,
(currLeft + currRight) / 2 + 1, currRight,
lz + lazy[treeIndex]);
lazy[treeIndex] = 0;
return cmpNodes(leftNode, rightNode);
} else {
lazy[treeIndex] += lz;
tree[treeIndex].value += lz;
return Node(9999999, -1);
}
}
public:
SegmentTree(int *init, int length) {
for (int k = 0; k < 2 * length - 1; k++) {
lazy.push_back(0);
tree.push_back(Node());
}
arrLength = length;
Node x = constructTree(init, 0, length - 1, 0);
}
void update(int leftIndex, int rightIndex, int value) {
_updateLz(leftIndex, rightIndex, value, 0, 0, arrLength - 1);
}
Node query(int leftIndex, int rightIndex) {
return _queryLz(leftIndex, rightIndex, 0, 0, arrLength - 1, 0);
}
};
int main() {
int n;
scanf("%d", &n);
int p[n];
for (int k = 0; k < n; k++) {
scanf("%d", (p + k));
}
SegmentTree st(p, n);
int t = 1;
for (int k = 0; k < n; k++) {
Node ans = st.query(0, n - 1);
st.update(ans.index + 1, n - 1, -t);
st.update(ans.index, ans.index, 99999999);
p[ans.index] = t;
t += 1;
}
for (int k = 0; k < n; k++) {
printf("%d ", p[k]);
}
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
/*
Code for task B by detestmaths
*/
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Solver {
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
// FastScanner in = new FastScanner("search.in");
//PrintWriter out = new PrintWriter("search.out");
int n = in.nextInt();
long s[] = new long[n];
for (int i = 0; i < n; i++) {
s[i] = in.nextLong();
}
segment_tree st = new segment_tree(s);
int ans[] = new int[n];
for (int i = 1; i < n + 1; i++) {
int ind = st.get(0,n-1).ind;
ans[ind] = i;
st.remove(ind);
st.add(ind+1,n-1,-i);
}
for (int i = 0; i < n; i++) {
out.print(ans[i] + " ");
}
out.close();
}
}
class segment_tree {
pair t[];
long a[];
long add[];
segment_tree(long a[]) {
this.a = a;
t = new pair[4 * a.length];
add = new long[4 * a.length];
for (int i = 0; i < 4 * a.length; i++) {
add[i] = -1;
}
build(1, 0, a.length - 1);
}
private void push(int v) {
if (add[v] != -1) {
add[v * 2] = add[v];
add[v * 2 + 1] = add[v];
add[v] = -1;
t[v].min += add[v];
}
}
void remove(int i) {
remove(1, 0, a.length - 1, i);
}
private void remove(int v, int tl, int tr, int pos) {
if (tl == tr) {
t[v] = new pair(Long.MAX_VALUE, -1);
add[v] = -1;
} else {
push(v);
int tm = (tl + tr) >> 1;
if (pos <= tm) remove(v * 2, tl, tm, pos);
else remove(v * 2 + 1, tm + 1, tr, pos);
t[v] = combine(v*2, v*2+1);
}
}
void add(int l, int r, long add) {
add(1, 0, a.length - 1, l, r, add);
}
private void add(int v, int tl, int tr, int l, int r, long val) {
if (l > tr || r < tl) return;
if (l <= tl && tr <= r) add[v] += val;
else {
push(v);
int tm = (tl + tr) >> 1;
add(v * 2, tl, tm, l, r, val);
add(v * 2 + 1, tm + 1, tr, l, r, val);
t[v] = combine(v * 2, v * 2 + 1);
}
}
private pair combine(int p1, int p2) {
pair a = t[p1];
pair b = t[p2];
long add1 = add[p1] == -1 ? 0 : add[p1];
long add2 = add[p2] == -1 ? 0 : add[p2];
if (a.min + add2 < b.min + add2) return a;
if (a.min + add1 > b.min + add2) return b;
return new pair(a.min + add1, Math.max(a.ind, b.ind));
}
private pair combine(pair a, pair b) {
if (a.min < b.ind) return a;
if (a.min > b.ind) return b;
return new pair(a.min, Math.max(a.ind, b.ind));
}
private void build(int v, int tl, int tr) {
if (tl == tr) t[v] = new pair(a[tl], tl);
else {
int tm = (tl + tr) >> 1;
build(v * 2, tl, tm);
build(v * 2 + 1, tm + 1, tr);
t[v] = combine(v * 2, v * 2 + 1);
}
}
pair get(int l, int r) {
return get(1, 0, a.length - 1, l, r);
}
private pair get(int v, int tl, int tr, int l, int r) {
if (l > tr || r < tl) return new pair(Long.MAX_VALUE, -1);
if (l <= tl && tr <= r) return t[v];
push(v);
int tm = (tl + tr) >> 1;
return combine(get(v * 2, tl, tm, l, r), get(v * 2 + 1, tm + 1, tr, l, r));
}
}
class pair {
long min;
int ind;
public pair(long min, int ind) {
this.min = min;
this.ind = ind;
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
public FastScanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public FastScanner(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
public String next() throws IOException {
if (!st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 10;
const int inf = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
set<int> s;
long long a[M], ans[M];
long long sum[4 * M];
int v[M * 4];
void pushup(int x) {
sum[x] = sum[x << 1] + sum[x << 1 | 1];
v[x] = v[x << 1] & v[x << 1 | 1];
}
void built(int l, int r, int i) {
if (l == r) {
sum[i] = l - 1;
return;
}
int mid = l + r >> 1;
built(l, mid, i << 1);
built(mid + 1, r, i << 1 | 1);
pushup(i);
}
void add(int l, int r, int x, int i) {
if (l == r && l == x) {
sum[i] = 0;
return;
}
int mid = l + r >> 1;
if (x <= mid)
add(l, mid, x, i << 1);
else
add(mid + 1, r, x, i << 1 | 1);
pushup(i);
}
long long query(int l, int r, long long s, int i) {
if (l == r) {
v[i] = 1;
return l;
}
int mid = l + r >> 1, k = 0;
if (sum[i << 1] > s || (sum[i << 1] == s && v[i << 1 | 1]))
k = query(l, mid, s, i << 1);
else
k = query(mid + 1, r, s - sum[i << 1], i << 1 | 1);
return k;
}
int main() {
int n;
scanf("%d", &n);
built(1, n, 1);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
for (int i = n; i >= 1; i--) {
ans[i] = query(1, n, a[i], 1);
if (ans[i] != n) add(1, n, ans[i] + 1, 1);
}
for (int i = 1; i <= n; i++) {
printf("%d ", ans[i]);
}
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
struct node {
long long x, y, z;
} a[N];
bool cmp1(node a, node b) {
if (a.x == b.x) return a.y > b.y;
return a.x < b.x;
}
bool cmp2(node a, node b) { return a.y < b.y; }
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].x, a[i].y = i;
sort(a + 1, a + 1 + n, cmp1);
int k = 0;
for (int i = 1; i <= n; i++) a[i].z = ++k;
sort(a + 1, a + 1 + n, cmp2);
for (int i = 1; i <= n; i++) printf("%lld%c", a[i].z, " \n"[i == n]);
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Washoum
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
inputClass in = new inputClass(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DRestorePermutation solver = new DRestorePermutation();
solver.solve(1, in, out);
out.close();
}
static class DRestorePermutation {
static int[] st;
static long[] arr;
int getMid(int s, int e) {
return s + (e - s) / 2;
}
int MaxUtil(int ss, int se, int l,
int r, int node) {
if (l <= ss && r >= se)
return st[node];
if (se < l || ss > r)
return 1000000000;
int mid = getMid(ss, se);
int a = MaxUtil(ss, mid, l, r, 2 * node + 1);
int b = MaxUtil(mid + 1, se, l, r, 2 * node + 2);
if (arr[a] == arr[b]) {
if (a > b) {
return a;
}
return b;
}
if (arr[a] <= arr[b]) {
return a;
}
return b;
}
void updateValue(int ss, int se,
int index, int value, int node) {
if (ss == se) {
arr[index] = value;
st[node] = ss;
} else {
int mid = getMid(ss, se);
if (index >= ss && index <= mid)
updateValue(ss, mid, index, value, 2 * node + 1);
else
updateValue(mid + 1, se, index, value, 2 * node + 2);
if (arr[st[2 * node + 1]] == arr[st[2 * node + 2]]) {
if (st[2 * node + 1] > st[2 * node + 2]) {
st[node] = st[2 * node + 1];
} else {
st[node] = st[2 * node + 2];
}
} else if (arr[st[2 * node + 1]] < arr[st[2 * node + 2]]) {
st[node] = st[2 * node + 1];
} else {
st[node] = st[2 * node + 2];
}
}
return;
}
int getMax(int n, int l, int r) {
return MaxUtil(0, n - 1, l, r, 0);
}
int constructSTUtil(int ss, int se, int si) {
if (ss == se) {
st[si] = ss;
return ss;
}
int mid = getMid(ss, se);
int a = constructSTUtil(ss, mid, si * 2 + 1);
int b = constructSTUtil(mid + 1, se, si * 2 + 2);
if (arr[a] == arr[b]) {
if (a > b) {
st[si] = a;
} else {
st[si] = b;
}
} else if (arr[a] < arr[b]) {
st[si] = a;
} else {
st[si] = b;
}
return st[si];
}
void constructST(int n) {
int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
int max_size = 2 * (1 << x) - 1;
st = new int[max_size];
constructSTUtil(0, n - 1, 0);
}
public void solve(int testNumber, inputClass sc, PrintWriter out) {
int n = sc.nextInt();
arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
}
constructST(n);
int[] ans = new int[n];
int act = 1;
for (int i = 0; i < n; i++) {
int m = getMax(n, 0, n - 1);
ans[m] = act;
act++;
updateValue(0, n - 1, m, 1000000000, 0);
}
for (int i : ans) {
out.print(i + " ");
}
}
}
static class inputClass {
BufferedReader br;
StringTokenizer st;
public inputClass(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class F {
public static long[] min;
public static long[] t;
public static void push(int v){
t[v*2] += t[v];
t[v*2+1] += t[v];
t[v] = 0;
}
public static long get_min(int v, int vl, int vr, int ql, int qr){
if(ql <= vl && vr <= qr){
return get_val(v);
}
if(qr <= vl || ql >= vr){
return Long.MAX_VALUE;
}
push(v);
long ans = Math.min(get_min(v*2, vl, (vl+vr)/2, ql, qr), get_min(v*2+1, (vl+vr)/2, vr, ql, qr));
min[v] = Math.min(get_val(v*2), get_val(v*2+1));
return ans;
}
public static int find(int v, int vl, int vr){
if(vl == vr - 1){
return v;
}
push(v);
if(get_min(v * 2 + 1, (vl + vr) / 2, vr, (vl + vr) / 2, vr) == 0){
return find(v * 2 + 1, (vl + vr) / 2, vr);
}
else{
return find(v*2, vl, (vl+vr)/2);
}
}
public static void add(int v, int vl, int vr, int ql, int qr, int x){
if(ql <= vl && qr >= vr){
t[v] += x;
return;
}
else if(qr <= vl || ql >= vr){
return;
}
push(v);
add(v*2, vl, (vl+vr)/2, ql, qr,x);
add(v*2+1, (vl+vr)/2, vr, ql, qr,x);
min[v] = Math.min(get_val(v*2), get_val(v*2+1));
}
public static long get_val(int v){
long start = min[v];
return Math.max(start - t[v], 0);
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
int n = Integer.parseInt(reader.readLine());
long[] array = new long[n];
StringTokenizer st = new StringTokenizer(reader.readLine());
for(int i = 0; i < n; ++i){
array[i] = Integer.parseInt(st.nextToken());
}
int new_n = n;
int counter = 0;
while(new_n > 0){
new_n /= 2;
++counter;
}
new_n = 1 << counter;
min = new long[new_n * 2];
t = new long[new_n * 2];
for(int i = 0; i < min.length; ++i){
min[i] = Long.MAX_VALUE;
}
int[] ans = new int[n];
int now = 1;
for(int i = new_n; i < new_n + n; ++i){
min[i] = array[i - new_n];
}
for(int i = new_n - 1; i >= 0; --i){
min[i] = Math.min(min[i * 2], min[i * 2 + 1]);
}
for(int i = 1; i <= n; ++i){
int node = find(1, 0, new_n) - new_n;
ans[node] = now;
min[node + new_n] = Long.MAX_VALUE;
int ver = node + new_n;
while(ver != 1){
ver/= 2;
min[ver] = Math.min(min[ver * 2], min[ver * 2 + 1]);
}
add(1, 0, new_n, node, new_n, now);
++now;
}
for(int i = 0; i < n; ++i){
writer.print(ans[i] + " ");
}
writer.close();
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.