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
|
python3
|
n=int(input())
queue = list(map(int,input().split()))
queue = queue[::-1]
outf = [''] * n
for i in range(1, n+1):
index=0
while True:
if queue[index]==0:
outf[index]=str(i)
break
else:
b=queue[index]-i
queue[index]=b
index += 1
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
|
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 ** 10, 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, n, 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>
#pragma GCC optimize("Ofast")
#pragma GCC target( \
"avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
long long MOD = 998244353;
const int NN = 4000000;
long long dr[NN];
long long gg[NN];
void push(long long v, long long l, long long r) {
dr[v] += gg[v];
gg[2 * v + 1] += gg[v];
gg[2 * v + 2] += gg[v];
gg[v] = 0;
}
void ch(long long v, long long l, long long r, long long ln, long long rn,
long long x) {
push(v, l, r);
if ((r <= ln || rn <= l)) {
return;
}
if (ln <= l && r <= rn) {
gg[v] += x;
push(v, l, r);
return;
}
ch(2 * v + 1, l, (l + r) / 2, ln, rn, x);
ch(2 * v + 2, (l + r) / 2, r, ln, rn, x);
dr[v] = max(dr[2 * v + 1], dr[2 * v + 2]);
}
long long sm(long long v, long long l, long long r, long long x) {
push(v, l, r);
push(2 * v + 1, l, r);
push(2 * v + 2, l, r);
if (l + 1 == r) {
return l + 1;
}
if (dr[2 * v + 1] < x) {
return sm(2 * v + 2, (l + r) / 2, r, x);
}
return sm(2 * v + 1, l, (l + r) / 2, x);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0),
cout << fixed << setprecision(20);
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
ch(0, 0, n, i, i + 1, i * (i + 1) / 2);
}
vector<long long> ans(n);
for (int i = n - 1; i > -1; i--) {
long long p = a[i];
long long num = sm(0, 0, n, p);
ans[i] = num;
ch(0, 0, n, num - 1, n, -num);
}
for (auto i : ans) {
cout << 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.*;
import java.util.*;
public class Main
{
public static void main (String [] args) throws Exception
{
Scanner sc = new Scanner();
PrintWriter out = new PrintWriter(System.out) ;
int n = sc.nextInt() ;
PriorityQueue<long [] > pq = new PriorityQueue<>((x , y)->x[0] != y[0] ? Long.compare(x[0] , y[0]) : Long.compare(y[1] , x[1])) ;
for(int i = 0 ; i < n ; i++)
{
long x = sc.nextLong() ;
pq.add(new long []{x , i }) ;
}
int [] ans = new int [n] ;
for(int i = 1 ; i <= n; i ++)
ans[(int)pq.poll()[1]] = i ;
for(int x : ans)
out.print(x+" ");
out.flush();
}
static class Scanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) ;
StringTokenizer st ;
String next() throws Exception
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine()) ;
return st.nextToken() ;
}
int nextInt() throws Exception
{
return Integer.parseInt(next()) ;
}
long nextLong() throws Exception
{
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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.TreeSet;
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);
Task19D solver = new Task19D();
solver.solve(1, in, out);
out.close();
}
static class Task19D {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
long arr[] = in.nextLongArray(n);
long ans[] = new long[n];
TreeSet<Long> ts = new TreeSet<>();
int i;
for (i = 1; i <= n; i++) {
ts.add(1l * i);
}
for (i = n - 1; i >= 0; i--) {
long fact = factor(arr[i]);
fact++;
if (ts.contains(fact)) {
ans[i] = fact;
ts.remove(fact);
} else {
long y = ts.higher(fact);
ans[i] = y;
ts.remove(y);
}
}
out.println(ans);
}
long factor(long v) {
long d = (long) Math.sqrt(1 + 8 * v);
d--;
d /= 2;
return d;
}
}
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(long[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void println(long[] array) {
print(array);
writer.println();
}
public void close() {
writer.close();
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(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 long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long 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 long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; ++i) array[i] = nextLong();
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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 11;
long long INF = 1e17;
long long ans[maxn * 40], pos[maxn * 40];
long long ad[maxn * 40];
int push(int node, int be, int en) {
int l = node * 2;
int r = node * 2 + 1;
int mid = be + en >> 1;
if (ad[node]) {
ad[l] += ad[node];
ad[r] += ad[node];
ans[l] += ad[node];
ans[r] += ad[node];
ad[node] = 0;
}
return 0;
}
int update(int node, int be, int en, int i, int val) {
int l = node * 2;
int r = node * 2 + 1;
int mid = be + en >> 1;
if (be == en) {
ans[node] = val;
pos[node] = i;
return 0;
}
push(node, be, en);
if (i <= mid)
update(l, be, mid, i, val);
else
update(r, mid + 1, en, i, val);
if (ans[l] < ans[r]) {
ans[node] = ans[l];
pos[node] = pos[l];
} else if (ans[l] > ans[r]) {
ans[node] = ans[r];
pos[node] = pos[r];
} else {
ans[node] = ans[l];
pos[node] = max(pos[l], pos[r]);
}
return 0;
}
int add(int node, int be, int en, int LL, int RR, long long val) {
int l = node * 2;
int r = node * 2 + 1;
int mid = be + en >> 1;
if (LL <= be && en <= RR) {
ans[node] += val;
ad[node] += val;
return 0;
}
push(node, be, en);
if (LL <= mid) add(l, be, mid, LL, RR, val);
if (RR > mid) add(r, mid + 1, en, LL, RR, val);
if (ans[l] < ans[r]) {
ans[node] = ans[l];
pos[node] = pos[l];
} else if (ans[l] > ans[r]) {
ans[node] = ans[r];
pos[node] = pos[r];
} else {
ans[node] = ans[l];
pos[node] = max(pos[l], pos[r]);
}
return 0;
}
int list[maxn];
int main() {
int n;
cin >> n;
long long x;
for (int i = 1; i <= n; i++) {
cin >> x;
update(1, 1, n, i, x);
}
for (int i = 1; i <= n; i++) {
int p = pos[1];
list[p] = i;
update(1, 1, n, p, INF);
add(1, 1, n, p, n, -i);
}
for (int i = 1; i <= n; i++) {
cout << list[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;
const double PI = 3.141592653589793;
const long long I_MAX = 1LL << 60;
long long mod = 1000000007;
const long long maxN = 2E5;
vector<long long> inv(maxN + 5, 1);
vector<long long> fac(maxN + 5, 1);
void ADD(long long &x, long long y) {
x += y;
if (x >= mod) x -= mod;
if (x < 0) x += mod;
}
void MUL(long long &x, long long y) {
x *= y;
x = (x % mod + mod) % mod;
}
long long mypow(long long b, long long e) {
long long ans = 1;
while (e) {
if (e & 1) MUL(ans, b);
MUL(b, b);
e >>= 1;
}
return ans;
}
long long modinv(long long n) { return mypow(n, mod - 2); }
void calcInv(long long n) {
for (int i = 2; i <= n; ++i) {
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
}
}
void calcFac(long long n) {
for (int i = 2; i <= n; ++i) {
fac[i] = fac[i - 1] * i % mod;
}
}
long long cm(long long a, long long b) {
long long ans = 1;
if (a < b) return 0;
MUL(ans, inv[fac[a - b]]);
MUL(ans, inv[fac[b]]);
MUL(ans, fac[a]);
return ans;
}
long long Lucas(long long n, long long m) {
if (m == 0) return 1;
return cm(n % mod, m % mod) * Lucas(n / mod, m / mod) % mod;
}
long long doLucas(long long n, long long m) {
calcInv(mod);
calcFac(mod);
return Lucas(n, m);
}
const long long maxn = 2E5 + 5;
long long arr[maxn + 1];
int lowbit(int x) { return x & (-x); }
void change(int x, long long add, int size) {
for (int i = x; i <= size; i += lowbit(i)) {
arr[i] += add;
}
}
long long query(int x, int size) {
long long ret = 0;
for (int i = x; i > 0; i -= lowbit(i)) {
ret += arr[i];
}
return ret;
}
void solve() {
long long n;
cin >> n;
bool used[n + 1];
for (int i = 0; i <= n; ++i) used[i] = false;
vector<long long> num(n), res(n, 0);
for (int i = 0; i < n; ++i) {
cin >> num[i];
}
for (int i = 1; i <= n; ++i) {
change(i, i - 1, n);
}
for (int i = n - 1; i >= 1; --i) {
int lo = 1, hi = n, mi;
while (lo < hi) {
mi = (lo + hi) >> 1;
long long tmp = query(mi, n);
if (tmp < num[i]) {
lo = mi + 1;
} else if (tmp == num[i] and used[mi]) {
lo = mi + 1;
} else {
hi = mi;
}
}
change(lo, -lo + 1, n);
used[lo] = true;
res[i] = lo;
}
for (int i = 1; i <= n; ++i) {
if (!used[i]) {
res[0] = i;
break;
}
}
for (int i = 0; i < n; ++i) {
cout << res[i] << ' ';
}
cout << '\n';
}
int main() {
int t = 1;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
for (int i = 1; i <= t; ++i) {
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
|
# https://codeforces.com/contest/1208/problem/D
from sys import stdin, stdout
input = stdin.readline
# print = stdout.write
# 1. the right most 0 is the index of current minimum value
# 2. we abstract the current value for all the elements to the right
# 3. Then we can do recusion
# To find the last index with value zero,
# we can use segment tree to get range minimum query with lazy propagation.
# https://www.geeksforgeeks.org/lazy-propagation-in-segment-tree-set-2/
n = int(input())
array = list(map(int, input().split()))
MAX = n * n
class Node:
def __init__(self, val, index, s, e):
self.lazy = 0
# for leaf node, store the value of array
# for internal node, store the value of interval
self.val = val
self.index = index
self.s = s
self.e = e
self.left = None
self.right = None
class SegmentTree:
def __init__(self, array):
self.root = self.build(array)
def build(self, array):
def _build(array, s, e):
if s == e:
cur = Node(array[s], s, s, e)
return cur
mid = (s + e) // 2
left = _build(array, s, mid)
right = _build(array, mid + 1, e)
if left.val < right.val:
cur = Node(left.val, left.index, s, e)
else:
cur = Node(right.val, right.index, s, e)
cur.left = left
cur.right = right
return cur
root = _build(array, 0, len(array) - 1)
return root
def modify(self, s, e, diff):
# add diff to all the array elements from s to e (inclusive)
def _modify(cur, s, e, diff):
# print("cur: [%d, %d], midify: [%d, %d], diff: %d" %
# (cur.s, cur.e, s, e, diff))
if cur.s > e or cur.e < s:
return cur.val + cur.lazy, cur.index
if cur.s == s and cur.e == e:
cur.lazy += diff
return cur.val + cur.lazy, cur.index
mid = (cur.s + cur.e) // 2
if s - 1 < mid < e:
left_val, left_index = _modify(
cur.left, s, mid, diff + cur.lazy)
right_val, right_index = _modify(
cur.right, mid + 1, e, diff + cur.lazy)
else:
left_val, left_index = _modify(
cur.left, s, e, diff + cur.lazy)
right_val, right_index = _modify(
cur.right, s, e, diff + cur.lazy)
if left_val < right_val:
cur.val = left_val
cur.index = left_index
else:
cur.val = right_val
cur.index = right_index
cur.lazy = 0
return cur.val, cur.index
_modify(self.root, s, e, diff)
def query(self, s, e):
# return the index of the minimum of array from s to e (inclusive)
def _query(cur, s, e):
if cur.s > e or cur.e < s:
return MAX, -1
if cur.s == s and cur.e == e:
return cur.val + cur.lazy, cur.index
mid = (cur.s + cur.e) // 2
left_val, left_index = _query(cur.left, s, mid)
right_val, right_index = _query(cur.right, mid+1, e)
if left_val < right_val:
return left_val + cur.lazy, left_index
else:
return right_val + cur.lazy, right_index
val, index = _query(self.root, s, e)
return val, index
def debug(tree):
l = [tree.root]
while l:
next_l = []
for node in l:
print("[%d, %d]: %d, %d, %d\t" %
(node.s, node.e, node.val, node.lazy, node.index), end="")
if node.left:
next_l.append(node.left)
if node.right:
next_l.append(node.right)
print()
l = next_l
tree = SegmentTree(array)
result = [0 for i in range(n)]
cur = 1
while cur <= n:
val, index = tree.query(0, n - 1)
if index < n - 1:
tree.modify(index + 1, n - 1, -cur)
tree.modify(index, index, MAX)
# debug(tree)
result[index] = cur
cur += 1
print(" ".join(map(str, result)))
|
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);
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>
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<ll> 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;
}
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
|
java
|
import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner();
PrintWriter out = new PrintWriter(System.out);
TreeMap<Long, Integer> map = new TreeMap();
int n = sc.nextInt();
long sum = 0;
for (int i = 1; i <= n; i++) {
map.put(sum, i);
sum += i;
}
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextLong();
int[] ans = new int[n];
for (int i = n - 1; i >= 0; i--) {
long x = map.ceilingKey(a[i]);
ans[i] = map.get(x);
map.remove(x);
}
for (int x : ans) {
out.print(x + " ");
}
out.println();
out.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
Scanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
Scanner(String fileName) throws FileNotFoundException {
br = new BufferedReader(new FileReader(fileName));
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
String nextLine() throws IOException {
return br.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(next());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(next());
}
boolean ready() throws IOException {
return br.ready();
}
}
static void sort(int[] a) {
shuffle(a);
Arrays.sort(a);
}
static void shuffle(int[] a) {
int n = a.length;
Random rand = new Random();
for (int i = 0; i < n; i++) {
int tmpIdx = rand.nextInt(n);
int tmp = a[i];
a[i] = a[tmpIdx];
a[tmpIdx] = tmp;
}
}
}
|
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;
auto it2 = st.lower_bound({nxt, 0});
if (it2 == st.end() || it2->first != nxt) {
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>
using namespace std;
const int maxn = 2e5 + 6;
const long long mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
int readInt() {
int x = 0;
bool sign = false;
char c = getchar();
while (!isdigit(c)) {
sign = c == '-';
c = getchar();
}
while (isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
return sign ? -x : x;
}
long long readLong() {
long long x = 0;
bool sign = false;
char c = getchar();
while (!isdigit(c)) {
sign = c == '-';
c = getchar();
}
while (isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
return sign ? -x : x;
}
string readString() {
string s;
char c = getchar();
while (isspace(c)) {
c = getchar();
}
while (!isspace(c)) {
s += c;
c = getchar();
}
return s;
}
long long mn[maxn << 2], tag[maxn << 2], a[maxn];
int ans[maxn];
int n;
void gather(int p) { mn[p] = min(mn[p << 1], mn[p << 1 | 1]); }
void build(int p, int l, int r) {
if (l == r) {
mn[p] = a[l];
return;
}
int mid = (l + r) / 2;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
gather(p);
}
void push(int p) {
if (tag[p]) {
tag[p << 1] += tag[p];
tag[p << 1 | 1] += tag[p];
mn[p << 1] += tag[p];
mn[p << 1 | 1] += tag[p];
tag[p] = 0;
}
}
void add(int p, int l, int r, int pos, int val) {
if (l == r) {
mn[p] += val;
return;
}
push(p);
int mid = (l + r) / 2;
if (pos <= mid)
add(p << 1, l, mid, pos, val);
else
add(p << 1 | 1, mid + 1, r, pos, val);
gather(p);
}
void add(int p, int tl, int tr, int l, int r, int val) {
if (tl > tr) return;
if (tl > r || tr < l) return;
if (l <= tl && tr <= r) {
mn[p] += val;
tag[p] += val;
return;
}
push(p);
int mid = (tl + tr) / 2;
add(p << 1, tl, mid, l, r, val);
add(p << 1 | 1, mid + 1, tr, l, r, val);
gather(p);
}
int lastzero(int p, int l, int r) {
if (l == r) {
return l;
}
push(p);
int mid = (l + r) / 2;
if (mn[p << 1 | 1] == 0) {
int tmp = lastzero(p << 1 | 1, mid + 1, r);
gather(p);
return tmp;
} else if (mn[p << 1] == 0) {
int tmp = lastzero(p << 1, l, mid);
gather(p);
return tmp;
}
}
int main() {
n = readInt();
for (int i = (1); i <= (n); i++) a[i] = readLong();
build(1, 1, n);
for (int i = (1); i <= (n); i++) {
int pos = lastzero(1, 1, n);
ans[pos] = i;
add(1, 1, n, pos, INF);
add(1, 1, n, pos + 1, n, -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
|
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);
tree.update(p.index+1, n, i);
}
for(int i = 1; i<=n; i++) {
z.print(ans[i]+" ");
}
z.println();
}
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]);
tree[nodeId].value -= prop[nodeId];
}
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;
tree[nodeId].value -= prop[nodeId];
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]);
tree[nodeId].value -= prop[nodeId];
}
}
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
|
java
|
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
import static java.lang.Math.*;
public class Solution{
static InputReader sc;
static PrintWriter wc;
static long[] tree,lazy,arr;
static void build(int l,int r,int i){
if(l==r){
tree[i]=arr[l];
return;
}
int m=(l+r)>>1;
build(l,m,2*i+1);
build(m+1,r,2*i+2);
tree[i]=Math.min(tree[2*i+1],tree[2*i+2]);
}
static void update(int i,long val){
tree[i]+=val;
lazy[i]+=val;
}
static void lazy_update(int ql,int qr,int l,int r,int i,long val){
if(l>qr||r<ql){
return;
}
else{
if(l>=ql&&r<=qr){
tree[i]+=val;
lazy[i]+=val;
}
else{
int m=(l+r)>>1;
lazy_update(ql,qr,l,m,2*i+1,val);
lazy_update(ql,qr,m+1,r,2*i+2,val);
tree[i]=Math.min(tree[2*i+1],tree[2*i+2]);
}
}
}
static int getMin(int l,int r,int i){
if(l==r){
return l;
}
else{
if(lazy[i]!=0){
update(2*i+1,lazy[i]);
update(2*i+2,lazy[i]);
lazy[i]=0;
}
int m=(l+r)>>1,res;
if(tree[2*i+2]==0){
res = getMin(m+1,r,2*i+2);
}
else
res = getMin(l,m,2*i+1);
tree[i]=Math.min(tree[2*i+1],tree[2*i+2]);
return res;
}
}
static void print(int a[]){
for(int i=0;i<a.length;i++){
wc.print(a[i]+" ");
}
//wc.println(a[0]);
}
public static void main(String[] args) {
sc = new InputReader(System.in);
wc = new PrintWriter(System.out);
int n=200000;
long sum=0;
int i,tlen,r[]=new int[n],j;
long inf=(long) 2e11;
tlen=(int)Math.ceil(Math.log(n)/Math.log(2))+1;
tlen=(1<<tlen)-1;
arr=new long[n];
tree=new long[tlen];
lazy = new long[tlen];
for(i=0;i<n;i++){
arr[i]=sum;
sum+=(i+1);
}
build(0,n-1,0);
for(i=1;i<=n;i++){
j=getMin(0,n-1,0);
r[j]=i;
lazy_update(j,j,0,n-1,0,inf);
lazy_update(j+1,n-1,0,n-1,0,-i);
}
print(r);
//wc.println(r[0]);
wc.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(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 String nextLine()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
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 long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long 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 double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(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;
}
public String next()
{
return readString();
}
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
|
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;
idx[root(ans)] = root(ans + 1);
update(0, 0, n, ans);
}
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 long long INF = 1e18;
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(int n, int s, int e, int p) {
if (s + 1 == e) {
seg[n] = A[s];
} else {
int m = (s + e) >> 1;
if (p < m)
init(n * 2, s, m, p);
else
init(n * 2 + 1, m, e, p);
seg[n] = min(seg[n * 2], seg[n * 2 + 1]);
}
}
void add_lazy(int n, int s, int e, long long x) {
lazy[n] = x;
seg[n] += x;
}
void push(int n, int s, int e) {
if (s + 1 == e) return;
int 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(int n, int s, int e, int a, int 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);
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);
}
seg[n] = min(seg[n * 2], seg[n * 2 + 1]);
}
}
int query(int n, int s, int e) {
if (s + 1 == e)
return s;
else {
if (lazy[n]) push(n, s, e);
int m = (s + e) >> 1;
long long ans;
if (seg[n * 2] < seg[n * 2 + 1])
ans = query(n * 2, s, m);
else
ans = query(n * 2 + 1, m, e);
seg[n] = min(seg[n * 2], seg[n * 2 + 1]);
return ans;
}
}
int n;
long long ans[MAXV];
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> A[i];
for (int i = 0; i < n; i++) init(1, 0, n, i);
long long curr = 1;
for (int i = 0; i < n; i++) {
int p = query(1, 0, n);
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.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
public class P1208D4 {
public static void main(String[] args) throws IOException {
InputReader2 ir = new InputReader2();
PrintWriter pw = new PrintWriter(System.out);
int n = ir.nextInt();
int[] s = new int[n];
for (int i = 0; i < n; i++) {
s[i] = ir.nextInt();
}
BIT tree = new BIT(n);
for (int i = 1; i <= n; i++) {
tree.add(i, i);
}
int[] ans = new int[n];
for (int i = n - 1; i >= 0; i--) {
int cur = s[i];
if (cur == 0) {
ans[i] = binSearch(tree, 0, n);
tree.remove(ans[i]);
} else {
ans[i] = binSearch(tree, cur, n);
tree.remove(ans[i]);
}
}
for (int an : ans) {
pw.print(an + " ");
}
pw.close();
}
private static int binSearch(BIT tree, int key, int n) {
int low = 1, high = n;
while (low <= high) {
int mid = (low + high) / 2;
long amt = tree.prefixSum(mid);
if (amt <= key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return low;
}
public static class BIT {
// The size of the array holding the Fenwick tree values
final int N;
// This array contains the Fenwick tree ranges
private final long[] tree;
// Create an empty Fenwick Tree with 'sz' parameter zero based.
public BIT(int sz) {
tree = new long[(N = sz + 1)];
}
// Returns the value of the least significant bit (LSB)
private static int lsb(int i) {
// Isolates the lowest one bit value
return i & -i;
}
// Computes the prefix sum from [1, i], O(log(n))
private long prefixSum(int i) {
long sum = 0L;
while (i != 0) {
sum += tree[i];
i &= ~lsb(i); // Equivalently, i -= lsb(i);
}
return sum;
}
// Returns the sum of the interval [left, right], O(log(n))
private long sum(int left, int right) {
if (right < left) throw new IllegalArgumentException("Make sure right >= left");
return prefixSum(right) - prefixSum(left - 1);
}
// Add 'v' to index 'i', O(log(n))
public void add(int i, long v) {
while (i < N) {
tree[i] += v;
i += lsb(i);
}
}
// Set index i to be equal to v, O(log(n))
public void remove(int i) {
add(i, -sum(i, i));
}
}
private static class InputReader2 {
final private int BUFFER_SIZE = 1 << 16;
private final DataInputStream dis;
private final byte[] buffer;
private int bufferPointer, bytesRead;
public InputReader2() {
dis = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private 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;
}
private void fillBuffer() throws IOException {
bytesRead = dis.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
}
}
|
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 n, s[300001];
long long isFit(long long i, long long x) {
long long p = (i * (i - 1)) / 2;
if (x <= p)
return 1;
else
return 0;
}
long long bs(long long l, long long r, long long x) {
if (x < 2) return x + 1;
if (l > r) return -1;
long long mid = (l + r) >> 1;
if (isFit(mid, x)) {
if (mid == 0 || !isFit(mid - 1, x))
return mid;
else
return bs(l, mid - 1, x);
} else {
return bs(mid + 1, r, x);
}
}
vector<long long> v[300001];
long long res[300001];
int main() {
cin >> n;
for (long long i = 0; i < n; i++) cin >> s[i];
for (long long i = 0; i <= n; i++) v[i].clear();
for (long long i = 0; i < n; i++) {
long long p = bs(0, n, s[i]);
v[p].push_back(i);
}
long long cnt = 1;
for (long long i = 1; i <= n; i++) {
long long sz = v[i].size();
for (long long j = sz - 1; j >= 0; j--) {
res[v[i][j]] = cnt;
cnt++;
}
}
for (long long 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
|
import java.util.*;
import java.io.*;
public class A
{
public static void main(String ar[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String s1[]=br.readLine().split(" ");
int a[]=new int[n];
int b[]=new int[n];
TreeMap<Integer,TreeSet<Integer>> tm=new TreeMap<Integer,TreeSet<Integer>>();
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(s1[i]);
if(tm.containsKey(a[i]))
{ tm.get(a[i]).add(i); }
else
{
TreeSet<Integer> ts=new TreeSet<Integer>();
ts.add(i);
tm.put(a[i],ts);
}
}
int j=1;
for(int x:tm.keySet())
{
TreeSet<Integer> ts=tm.get(x);
while(!ts.isEmpty())
{
int u=ts.pollLast();
b[u]=j;
j++;
}
}
StringBuffer sb=new StringBuffer();
for(int i=0;i<n;i++)
sb.append(b[i]).append(" ");
System.out.println(sb);
}
}
|
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 <typename T>
inline bool uax(T &x, T y) {
return (y > x) ? x = y, true : false;
}
template <typename T>
inline bool uin(T &x, T y) {
return (y < x) ? x = y, true : false;
}
string to_string(char c) { return "'" + string(1, c) + "'"; }
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
template <typename A>
string to_string(A);
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ": " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool f = false;
string r = "{";
for (auto x : v) {
if (f) r += ", ";
r += to_string(x);
f = true;
}
return r += "}";
}
template <typename A>
string to_string(vector<vector<A>> v) {
string r;
for (auto x : v) r += "\n" + to_string(x);
return r;
}
void err(istream_iterator<string>) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " =: " << to_string(a) << "; ";
err(++it, args...);
}
template <typename T>
void kek(T ans) {
cout << ans << endl;
exit(0);
}
int const MOD = 1e9 + 7;
long long const INF = 1e18 + 42;
template <class T>
struct Fenwick {
int n;
vector<T> bit;
Fenwick(int _n) : n(_n), bit(n + 1, 0){};
T ask(int R) {
T ans = 0;
for (int x = R; x > 0; x -= x & -x) ans += bit[x];
return ans;
}
T ask(int L, int R) { return ask(R) - ask(L - 1); }
void add(int pos, T v) {
if (pos <= 0) return;
for (; pos <= n; pos += pos & -pos) bit[pos] += v;
}
vector<T> values(int upto = -1) {
if (upto == -1) return values(n);
vector<T> fenwick_values;
for (int i = 1; i <= upto; ++i) fenwick_values.push_back(ask(i, i));
return fenwick_values;
}
};
int32_t main() {
const int nax = 1e6 + 42;
vector<long long> b(nax);
map<long long, int> mp;
for (int i = 0; i < nax; ++i) {
b[i] = (long long)i * (i + 1) / 2;
mp[b[i]] = i;
}
int n;
cin >> n;
vector<long long> a(n);
for (long long &z : a) cin >> z;
vector<int> ans(n);
ans[n - 1] = mp[a[n - 1]] + 1;
Fenwick<long long> fen(n + 1);
fen.add(ans[n - 1], ans[n - 1]);
for (int i = n - 2; i >= 0; --i) {
long long val = a[i];
if (a[i] >= a[i + 1]) {
val += fen.ask(ans[i + 1]);
}
ans[i] = mp[val] + 1;
fen.add(ans[i], ans[i]);
}
for (int z : ans) {
cout << z << ' ';
}
cout << '\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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ll long long
#define ld long double
#define vll vector <ll>
#define vvll vector <vll>
#define pll pair <ll, ll>
#define rep(i, a, b) for(ll i = a; i < b; i++)
#define per(i, a, b) for(ll i = a - 1; i >= b; --i)
#define endl "\n"
#define pb push_back
#define pf push_front
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sorta(v) sort(all(v))
#define sortd(v) sort(rall(v))
#define vld vector<ld>
#define debug if(0)
#define test if(0)
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
const ll N = 2 * 1e5;
ll ans[N];
ll a[N];
pll t[4 * N];
void build(ll v, ll l, ll r) {
if(l == r) {
t[v] = {a[l], -l};
} else {
ll m = (l + r) / 2;
build(v * 2 + 1, l, m);
build(v * 2 + 2, m + 1, r);
t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
}
}
void update(ll v, ll tl, ll tr, ll pos, ll val) {
if(tl == tr) {t[v] = {val, -pos};return;}
ll tm = (tl + tr) / 2;
if(pos <= tm) {
update(v * 2 + 1, tl, tm, pos, val);
} else {
update(v * 2 + 2, tm + 1, tr, pos, val);
}
t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
}
pll sum(ll v, ll tl, ll tr, ll l, ll r) {
if(l > r) return {2e9, 2e9};
if(tl == l && tr == r) {
//cout << l << " " << r << " " << t[v] << " " << v << endl;
return t[v];
}
ll tm = (tl + tr) / 2;
return min(sum(v * 2 + 1, tl, tm, l, min(tm, r)), sum(v * 2 + 2, tm + 1, tr, max(tm + 1, l), r));
}
ll n;
void findAns(ll l, ll r, ll st1, ll curin = 2) {
//cout << st1 << ' ' << curin << endl;
if(curin == n + 1) return;
ll oo = 0;
pll cur = sum(0, 0, n - 1, l, st1 - 1);
if(cur.first == 2e9) {
cur = sum(0, 0, n - 1, st1 + 1, r);
}
cur.second *= -1;
//cout << "cur : " << cur.first << " " << cur.second << endl;
update(0, 0, n - 1, cur.second, 2e9);
ans[cur.second] = curin;
findAns(0, r, cur.second, curin + 1);
}
int main() {
//freopen("bees.in", "r", stdin);
//freopen("bees.out", "w", stdout);
//ios;
cin >> n;
ll st = 0;
rep(i, 0, n) {
cin >> a[i];
if(!a[i]) {st = i;}
}
build(0, 0, n - 1);
ans[st] = 1;
update(0, 0, n - 1, st, 2e9);
findAns(0, n - 1, st);
rep(i, 0, n) 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;
const long long MOD = 1e9 + 7;
long long n = 1, n2, s[200010], ans[200010], dat[800010];
void INIT(int k, int lb, int ub) {
if (lb == ub) return;
INIT(k * 2 + 1, lb, (lb + ub) / 2);
INIT(k * 2 + 2, (lb + ub) / 2 + 1, ub);
dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2];
}
int query(int k, int lb, int ub, int tlb, int tub) {
if (ub < tlb || tub < lb) return 0;
if (tlb <= lb && ub <= tub) return dat[k];
return query(k * 2 + 1, lb, (lb + ub) / 2, tlb, tub) +
query(k * 2 + 2, (lb + ub) / 2 + 1, ub, tlb, tub);
}
void upd(int val) {
int k = val - 1 + n - 1;
dat[k] -= val;
while (k > 0) {
dat[(k - 1) / 2] -= val;
k = (k - 1) / 2;
}
}
bool check(int mid, int i) { return query(0, 0, n - 1, 0, mid - 2) <= s[i]; }
int main() {
cin >> n2;
while (n < n2) n *= 2;
for (int i = 0; i < n2; i++) scanf("%I64d", &s[i]);
for (int i = 0; i < n; i++) dat[i + n - 1] = i + 1;
INIT(0, 0, n - 1);
for (int i = n2 - 1; i >= 0; i--) {
int lb = 1, ub = n2, mid;
while (lb < ub) {
mid = (lb + ub + 1) / 2;
if (check(mid, i))
lb = mid;
else
ub = mid - 1;
}
ans[i] = lb;
upd(lb);
}
check(2, 2);
for (int i = 0; i < n2; 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
|
python3
|
n=int(input())
a=[]
b={}
for i in input().split():
a.append(int(i))
b[int(i)]=True
qq=list(b.keys())
ii=1
b=[0 for i in range(n)]
for i in qq:
for i2 in range(n-1,-1,-1):
if a[i2]==i:
b[i2]=ii
ii+=1
for i in b:
print(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
|
python3
|
n=int(input())
queue = list(map(int,input().split()))
queue = queue[::-1]
outf = [''] *n
for i in range(n):
a = queue.index(0)
outf[a]=str(i+1)
for j in range(i+1):
queue[j]=queue[j]-i
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>
#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 - 1) {
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]);
}
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 = -tree[1].second;
cout << pom << ' ';
add(pom, pom, INF);
add(pom + 1, n, -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
|
cpp
|
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
typedef long long ll;
#define inf 100000000000
ll tree[1000000];
ll index[1000000];
ll lazy[1000000];
ll a[1000000];
void build(int node,int l,int r){
if(l==r){
tree[node]=a[l];
index[node]=l;
return ;
}
build(2*node+1,l,(l+r)/2);
build(2*node+2,(l+r)/2+1,r);
if(tree[2*node+1]<tree[2*node+2]){
tree[node]=tree[2*node+1];
index[node]=index[2*node+1];
return ;
}
if(tree[2*node+1]>tree[2*node+2]){
tree[node]=tree[2*node+2];
index[node]=index[2*node+2];
return ;
}
tree[node]=tree[2*node+1];
index[node]=max(index[2*node+1],index[2*node+2]);
}
pair<ll,ll> get(int node,int l,int r,int x,int y){
if(lazy[node]!=0){
tree[node]+=lazy[node];
lazy[2*node+1]+=lazy[node];
lazy[2*node+2]+=lazy[node];
lazy[node]=0;
}
if(r<x||l>y){
return mp(100000,inf);
}
if(l>=x&&r<=y)return mp(index[node],tree[node]);
pair<ll,ll>ans1=get(2*node+1,l,(l+r)/2,x,y);
pair<ll,ll>ans2=get(2*node+2,(l+r)/2+1,r,x,y);
if(ans1.second<ans2.second){
tree[node]=ans1.second;
index[node]=ans1.first;
}
else if(ans1.second>ans2.second){
tree[node]=ans2.second;
index[node]=ans2.first;
}
else {
tree[node]=ans1.second;
index[node]=max(ans1.first,ans2.first);
}
return mp(index[node],tree[node]);
}
void update(int node,int l,int r,int x,int y,ll z){
if(r<x||l>y)return ;
if(lazy[node]!=0){
tree[node]+=lazy[node];
lazy[2*node+1]+=lazy[node];
lazy[2*node+2]+=lazy[node];
lazy[node]=0;
}
if(l>=x&&r<=y){
tree[node]+=z;
lazy[2*node+1]+=z;
lazy[2*node+2]+=z;
return ;
}
update(2*node+1,l,(l+r)/2,x,y,z);
update(2*node+2,(l+r)/2+1,r,x,y,z);
if(tree[2*node+1]<tree[2*node+2]){
tree[node]=tree[2*node+1];
index[node]=index[2*node+1];
return ;
}
if(tree[2*node+1]>tree[2*node+2]){
tree[node]=tree[2*node+2];
index[node]=index[2*node+2];
return ;
}
tree[node]=tree[2*node+2];
index[node]=max(index[2*node+1],index[2*node+2]);
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
vector<int>p(n);
build(0,0,n-1);
for(int i=1;i<=n;i++){
int is=get(0,0,n-1,0,n-1).first;
p[is]=i;
update(0,0,n-1,is,is,inf);
update(0,0,n-1,is+1,n-1,-a[is]);
}
for(int i=0;i<n;i++){
cout<<p[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
|
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+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)
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;
long long mini[8 * 200200];
int occ[8 * 200200];
long long lazy[8 * 200200];
long long v[200200];
void refresh(int ini, int fim, int p) {
mini[p] -= lazy[p];
lazy[2 * p] += lazy[p];
lazy[2 * p + 1] += lazy[p];
lazy[p] = 0;
}
void build(int ini, int fim, int p) {
if (ini == fim) {
mini[p] = v[ini];
occ[p] = ini;
return;
}
int mid = (ini + fim) / 2;
build(ini, mid, 2 * p);
build(mid + 1, fim, 2 * p + 1);
if (mini[2 * p] < mini[2 * p + 1]) {
mini[p] = mini[2 * p];
occ[p] = occ[2 * p];
} else {
mini[p] = mini[2 * p + 1];
occ[p] = occ[2 * p + 1];
}
("node %d..%d mini %lld\n", ini, fim, mini[p]);
}
void update(int ini, int fim, int p, int l, int r, long long dec) {
if (l > fim || r < ini) return;
refresh(ini, fim, p);
if (l <= ini && r >= fim) {
("oi %d..%d\n", l, r);
lazy[p] += dec;
refresh(ini, fim, p);
return;
}
int mid = (ini + fim) / 2;
update(ini, mid, 2 * p, l, r, dec);
update(mid + 1, fim, 2 * p + 1, l, r, dec);
if (mini[2 * p] < mini[2 * p + 1]) {
mini[p] = mini[2 * p];
occ[p] = occ[2 * p];
} else {
mini[p] = mini[2 * p + 1];
occ[p] = occ[2 * p + 1];
}
("mini[%d] := %lld (%lld %lld)\n", p, mini[p], mini[2 * p], mini[2 * p + 1]);
}
int ans[200200];
int main() {
int n;
scanf("%d", &n);
;
for (int i = 0; i < n; i++) scanf("%lld", v + i);
build(0, n - 1, 1);
for (int i = 1; i <= n; i++) {
("root %d %d\n", mini[1], occ[1]);
int id = occ[1];
update(0, n - 1, 1, id + 1, n - 1, i);
update(0, n - 1, 1, id, id, -(1e12));
ans[id] = i;
}
for (int i = 0; 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;
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;
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);
for (int i = 1; i <= n; i++) {
auto p = query(1, 1, n, 1, n);
assert(query(1, 1, n, p.second + 1, n).first > p.first);
v[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 ", v[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
|
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;
/**
* @author khokharnikunj8
*/
public class Main {
public static void main(String[] args) {
new Thread(null, new Runnable() {
public void run() {
new Main().solve();
}
}, "1", 1 << 26).start();
}
void solve() {
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();
}
int[] temp = new int[n + 1];
for (int i = 1; i <= n; i++) temp[i] = i;
SegmentTree sg = new SegmentTree(temp, n);
int[] ans = new int[n];
for (int i = n - 1; i >= 0; i--) {
int low = 1;
int high = n;
int index = 0;
while (low <= high) {
int mid = (low + high) >> 1;
if (sg.query(1, mid) <= ar[i]) {
index = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
ans[i] = index + 1;
sg.modify(index + 1, 0);
}
for (int i : ans) out.print(i + " ");
}
}
static class SegmentTree {
int n;
long[] t;
int[] data;
public SegmentTree(int[] data, int n) {
this.data = data;
this.n = n;
this.t = new long[2 * (n + 3)];
build();
}
void build() {
for (int i = 0; i < n; i++) t[i + n] = data[i + 1];
for (int i = n - 1; i > 0; --i) t[i] = (t[i << 1] + t[i << 1 | 1]);
}
public void modify(int p, int value) {
for (t[p += n - 1] = value; p > 1; p >>= 1) t[p >> 1] = (t[p] + t[p ^ 1]);
}
public long query(int l, int r) {
long res = 0;
for (l += n - 1, r += n; l < r; l >>= 1, r >>= 1) {
if ((l & 1) != 0) res += t[l++];
if ((r & 1) != 0) res += t[--r];
}
return res;
}
}
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 integer = 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') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
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 tree[1000000] = {0};
void add(int node, int l, int r, int x) {
if (x > r || x < l) return;
if (x == l && x == r) {
tree[node] = x;
return;
}
add(2 * node + 1, l, (l + r) / 2, x);
add(2 * node + 2, (l + r) / 2 + 1, r, x);
tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
return;
}
int get(int node, int l, int r, int x, int y) {
if (r < x || l > y) return 0;
if (l >= x && r <= y) return tree[node];
return get(2 * node + 1, l, (l + r) / 2, x, y) +
get(2 * node + 2, (l + r) / 2 + 1, r, x, y);
}
int main() {
int n;
cin >> n;
vector<long long> s(n);
for (int i = 0; i < n; i++) cin >> s[i];
vector<long long> p(n);
for (int i = n - 1; i >= 0; i--) {
int l = 1, r = n;
int is = 1;
while (l <= r) {
long long mid = (l + r) / 2;
int last = get(0, 0, 300001, 0, mid);
int ans = ((mid * (mid - 1)) / 2 - last);
if (ans > s[i]) {
r = mid - 1;
} else if (ans < s[i])
l = mid + 1;
else {
is = mid;
break;
}
}
p[i] = is;
add(0, 0, 300001, is);
}
for (int i = 0; i < n; i++) {
cout << p[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>
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 val, 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] -= val;
if (st < en) {
lz[2 * node + 1] += val;
lz[2 * node + 2] += val;
}
return;
}
upd(l, r, val, 2 * node + 1, st, m);
upd(l, r, val, 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;
if (ans + 1 < n) upd(ans + 1, n - 1, go, 0, 0, n - 1);
go++;
pupd(ans, INF, 0, 0, n - 1);
}
for (i = 0; i < n; i++) cout << w[i] << " \n"[i == n - 1];
}
int main() {
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
|
a = int(input())
A = list(map(int,input().split()))
b = a-1
B = [i+1 for i in range(a)]
C = []
while b>=0:
l = 0
r = b
while r-1>l:
m = (r+l)//2
if (B[m]*(B[m]-1))//2>A[b]:
r = m
else:
l = m
if (B[l]*(B[l]-1))//2<A[b]:
C=[B[r]]+C
B.pop(r)
else:
C = [B[l]]+C
B.pop(l)
b-=1
print(*C)
|
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 a[210000];
int b[210000];
bool book[210000];
int main() {
int n;
map<long long, int> mp;
int ans = 0;
for (int i = 1; i <= 200010; i++) {
mp[ans] = i;
ans += i;
}
while (~scanf("%d", &n)) {
memset(book, 0, sizeof(book));
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (int i = n; i >= 1; i--) {
int x = mp[a[i]];
while (book[x] == 1) x++;
b[i] = x;
book[x] = 1;
}
for (int i = 1; i <= n; i++) {
printf("%d", b[i]);
if (i != n) printf(" ");
}
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;
#define fRead(x) freopen(x,"r",stdin)
#define fWrite(x) freopen (x,"w",stdout)
#define LL long long
#define ULL unsigned long long
#define ff first
#define ss second
#define pb push_back
#define INF 2e16
#define PI acos(-1.0)
#define mk make_pair
#define pii pair<int,int>
#define pll pair<LL,LL>
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define min4(a,b,c,d) min(a,min(b,min(c,d)))
#define max4(a,b,c,d) max(a,max(b,max(c,d)))
#define SQR(a) ((a)*(a))
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define REP(i,b) for(int i=0;i<b;i++)
#define MEM(a,x) memset(a,x,sizeof(a))
#define ABS(x) ((x)<0?-(x):(x))
#define SORT(v) sort(v.begin(),v.end())
#define REV(v) reverse(v.begin(),v.end())
#define FastRead ios_base::sync_with_stdio(0);cin.tie(nullptr);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T> using orderset = tree <T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; // find_by_order, order_of_key
const int N = 200005;
int n , ara[N];
LL bit[N];
void update(int idx,int val)
{
while(idx < N){
bit[idx] += val;
idx += (idx & -idx);
}
}
LL query(int idx)
{
LL ret = 0;
while(idx > 0){
ret += bit[idx];
idx -= (idx & -idx);
}
return ret;
}
int ans[N];
int main()
{
FastRead
cin >> n;
FOR(i,1,n)cin >> ara[i];
FOR(i,1,n)update(i,i);
for(int i = n;i >= 1;i--){
int lo = 1 , hi = n,mid,ANS;
while(lo <= hi){
mid = (lo + hi)/2;
if(query(mid) > ara[i]){
ANS = mid;
hi = mid - 1;
}else{
lo = mid + 1;
}
}
ans[i] = ANS;
update(ANS,-ANS);
}
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
|
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
public static void update(long[] BIT, int idx, long val) {
for(; idx < BIT.length; idx += idx & (-idx)) {
BIT[idx] += val;
}
}
public static long query(long[] BIT, int idx) {
long ans = 0;
for(; idx > 0; idx -= idx & (-idx)) {
ans += BIT[idx];
}
return ans;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
long[] arr = new long[n];
for(int i = 0; i < n; i++) {
arr[i] = s.nextLong();
}
long[] BIT = new long[n + 1];
int[] temp = new int[n];
for(int i = n - 1; i >= 0; i--) {
int start = 1, end = n, ans = 1;
while(start <= end) {
int mid = (start + end) / 2;
long val = (mid * (mid - 1)) / 2 - query(BIT, mid - 1);
// System.out.println(start + " " + end + " " + mid + " " + val);
if(arr[i] < val) {
end = mid - 1;
} else {
ans = mid;
start = mid + 1;
}
}
temp[i] = ans;
update(BIT, ans, ans);
}
StringBuffer strbf = new StringBuffer();
for(int i = 0; i < n; i++) {
strbf.append(temp[i] + " ");
}
System.out.println(strbf);
}
}
|
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;
long long BIT[N], s[N];
int n;
int ans[N];
void update(int x, int delta) {
for (; x <= n; x += x & -x) BIT[x] += delta;
}
long long query(int x) {
long long sum = 0;
for (; x > 0; x -= x & -x) sum += BIT[x];
return sum;
}
int searchNumber(long long prefSum) {
int num = 0;
long long sum = 0;
for (int i = 21; i >= 0; --i) {
if ((num + (1 << i) <= n) && (sum + BIT[num + (1 << i)] <= prefSum)) {
num += (1 << i);
sum += BIT[num];
}
}
return num + 1;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
update(i, i);
scanf("%64d", &s[i]);
}
for (int i = n; i >= 1; --i) {
ans[i] = searchNumber(s[i]);
update(ans[i], -ans[i]);
}
for (int i = 1; i <= n; ++i) {
printf("%d ", 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.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];
int curr = 1;
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 = st.ans[1];
ans[ans1] = i;
st.increment(ans1, n - 1, i * -1);
st.increment(ans1, ans1, Integer.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 {
int[] lo;
int[] hi;
int[] min;
int[] delta;
int[] ans;
int n;
public SegmentTree(int n) {
this.n = n;
this.hi = new int[4 * n + 1];
this.lo = new int[4 * n + 1];
this.min = new int[4 * n + 1];
this.delta = new int[4 * n + 1];
this.ans = new int[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, int val) {
increment(1, a, b, val);
}
private void increment(int i, int a, int b, int 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 int minimum(int a, int b) {
return minimum(1, a, b);
}
private int minimum(int i, int a, int b) {
if (a > hi[i] || b < lo[i]) {
return Integer.MAX_VALUE;
}
if (a <= lo[i] && hi[i] <= b) {
return min[i] + delta[i];
}
prop(i);
int minLeft = minimum(2 * i, a, b);
int 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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int bit[200005];
int n;
void update(int j, int x) {
for (; j < 200005; j += j & (-j)) bit[j] += x;
}
int query(int j) {
if (j == 0) return 0;
int x = 0;
for (; j > 0; j -= (j) & (-j)) x += bit[j];
return x;
}
int next_ele(long long int s) {
int l, r, m;
long long int q;
l = 0, r = n;
while (l < r - 1) {
m = (l + r) / 2;
q = query(m);
if (q < s) l = m + 1;
if (q > s) r = m - 1;
if (q == s) l = m;
}
m = l;
if (query(r) == s) m = r;
return m + 1;
}
int compute(long long int s) {
int x;
s *= 2;
x = sqrt(s);
return x + 1;
}
int main() {
int i;
scanf("%d", &n);
vector<long long int> sum(n);
for (i = 0; i < n; i++) cin >> sum[i];
vector<int> ans(n);
ans[n - 1] = compute(sum[n - 1]);
for (i = 1; i < n + 1; i++) update(i, i);
update(ans[n - 1], -ans[n - 1]);
for (i = n - 2; i >= 0; i--) {
ans[i] = next_ele(sum[i]);
update(ans[i], -ans[i]);
}
for (i = 0; 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.*;
import java.io.IOException;
import java.util.*;
//import javafx.util.Pair;
//import java.util.concurrent.LinkedBlockingDeque;
import javax.swing.Icon;
//import sun.net.www.content.audio.wav;
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 int countSet(int a){
int c=0;
while(a>0){
a&=(a-1);
c++;
}
return c;
}
public static void Zfunction(String s){
int n=s.length();
int a[]=new int[n];
int r1=0,r2=1;
for(int i=1;i<n;){
int f=0;
while(r2<n&&s.charAt(r1)==s.charAt(r2)){
r1++;
r2++;
}
a[i]=r1;
i++;
int l2=1;
for(;i<r2;i++){
if(i+a[l2]<r2)
a[i]=a[l2++];
else{
f=1;
r1=r2-i;
break;
}
}
if(f==0){
r2=i;
r1=0;
}
}
pw.println(Arrays.toString(a));
}
public static ArrayList<ArrayList <Integer>> GetGraph(int n,int m){
ArrayList<ArrayList <Integer>> a=new ArrayList<>();
for(int i=0;i<n;i++){
a.add(new ArrayList<>());
}
for(int i=0;i<m;i++){
int u=sc.nextInt()-1;
int v=sc.nextInt()-1;
a.get(u).add(v);
a.get(v).add(u);
}
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();
long a[]=scanLongArray(n);
Bit h=new Bit(n);
for(int i=1;i<=n;i++){
h.update(i, i);
}
int ans[]=new int[n];
for(int i=n;i>0;i--){
int in=h.searchNumber(a[i-1]);
ans[i-1]=in+1;
h.update(in+1, -(in+1));
//pw.println(h.query(1));
}
pw.println(Arrays.toString(ans));
// Code ends...
pw.flush();
pw.close();
}
public static Comparator<Integer> C(){
return
new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);//for descending
}
};
}
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<Long[]> column1(int i){
return
new Comparator<Long[]>() {
@Override
public int compare(Long[] o1, Long[] 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;
}
}
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
long a[];int n;
Bit(int n){
this.n=n;
a=new long[n+1];
}
void update(int i,int delta){
while(i<a.length){
a[i]+=delta;
i+=i&(-i);
}
}
long query(int i){
long sum=0;
while(i>0){
sum+=a[i];
i-=i&(-i);
}
return sum;
}
int searchNumber(long sum){
int l=1,r=n,ans=0;
while(l<=r){
int m=(l+r)/2;
long v=query(m);
if(v>sum){
r=m-1;
}
else if(v<sum){
l=m+1;
}
else{
ans=m;
l=m+1;
}
}
return 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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<long long int, long long int> &a1,
pair<long long int, long long int> &a2) {
return (a1.first < a2.first) ? 1
: (a1.first > a2.first) ? 0
: a1.second > a2.second;
}
vector<pair<long long int, long long int> > v;
const int N = 1e6;
long long int a[N], b[N];
int main() {
long long int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
v.emplace_back(make_pair(a[i], i));
}
sort(v.begin(), v.end(), cmp);
long long int t = 0;
for (int i = 0; i < n; i++) {
b[v[i].second] = t + 1;
t++;
}
for (int i = 0; i < n; i++) {
cout << b[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.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]);
f.add(ind, -ind);
Integer u = set.ceiling(ind);
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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e5 + 123, inf = 1e9, mod = 1e9 + 7;
const long long INF = 1e18;
long long t[N << 2], mn[N << 2], second[N];
int n, p[N];
void upd(int v, int tl, int tr, int l, int r, int k) {
if (r < tl || tr < l || l > r) return;
if (l <= tl && tr <= r) {
t[v] += k;
mn[v] += k;
return;
}
int tm = (tl + tr) >> 1;
upd(v << 1, tl, tm, l, r, k);
upd(v << 1 | 1, tm + 1, tr, l, r, k);
mn[v] = min(mn[v << 1], mn[v << 1 | 1]) + t[v];
}
int get(int v, int tl, int tr, int k) {
if (tl == tr) return tl;
int tm = (tl + tr) >> 1;
if (mn[v << 1 | 1] <= k) return get(v << 1 | 1, tm + 1, tr, k - t[v]);
return get(v << 1, tl, tm, k - t[v]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &second[i]);
for (int i = 1; i <= n; i++) upd(1, 1, n, i + 1, n, i);
for (int i = n; i >= 1; i--) {
p[i] = get(1, 1, n, second[i]);
upd(1, 1, n, p[i] + 1, n, -p[i]);
}
for (int i = 1; i <= n; i++) printf("%d ", p[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>
#pragma GCC optimize("O3")
#pragma GCC optimize("-Ofast", "-funroll-all-loops", "-ffast-math")
#pragma GCC target("avx2,fma")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long long Linf = 1e12;
const int Inf = 0x3f3f3f3f;
const int MAXN = 2e5 + 10;
int n, a[MAXN];
struct Node {
long long mi, addv;
};
struct Segment_Tree {
Node v[MAXN << 2];
void PushUp(int rt) { v[rt].mi = min(v[rt << 1].mi, v[rt << 1 | 1].mi); }
void Build_Tree(int l, int r, int rt) {
if (l == r) return v[rt].mi = a[l], void();
int mid = (l + r) >> 1;
Build_Tree(l, mid, rt << 1);
Build_Tree(mid + 1, r, rt << 1 | 1);
PushUp(rt);
}
void Add(int l, int r, long long C, int rt) {
v[rt].mi += C;
v[rt].addv += C;
}
void PushDown(int l, int r, int rt) {
if (v[rt].addv) {
int mid = (l + r) >> 1;
Add(l, mid, v[rt].addv, rt << 1);
Add(mid + 1, r, v[rt].addv, rt << 1 | 1);
v[rt].addv = 0;
}
}
void UpdateAdd(int L, int R, long long C, int l, int r, int rt) {
if (L <= l && r <= R) return Add(l, r, C, rt), void();
int mid = (l + r) >> 1;
PushDown(l, r, rt);
if (L <= mid) UpdateAdd(L, R, C, l, mid, rt << 1);
if (R > mid) UpdateAdd(L, R, C, mid + 1, r, rt << 1 | 1);
PushUp(rt);
}
int QueryPos(int l, int r, int rt) {
if (l == r) return l;
int mid = (l + r) >> 1;
PushDown(l, r, rt);
if (v[rt << 1 | 1].mi == 0)
return QueryPos(mid + 1, r, rt << 1 | 1);
else
return QueryPos(l, mid, rt << 1);
}
} t;
int ans[MAXN];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
t.Build_Tree(1, n, 1);
for (int i = 1; i <= n; i++) {
int pos = t.QueryPos(1, n, 1);
ans[pos] = i;
if (pos + 1 <= n) t.UpdateAdd(pos + 1, n, -i, 1, n, 1);
t.UpdateAdd(pos, pos, 1e12, 1, n, 1);
}
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>
using namespace std;
const int N = 2e5 + 5;
int n, a[N], ans[N];
long long bit[N];
void update(int pos) {
for (int i = pos; i < N; i += (i & -i)) {
bit[i] += pos;
}
}
long long query(int pos) {
long long res = 0;
for (int i = pos; i; i -= (i & -i)) {
res += bit[i];
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = n; i; i--) {
int l = 1, r = n;
while (l <= r) {
int mid = (l + r) / 2;
if (1ll * (mid - 1) * mid / 2 - query(mid - 1) <= a[i]) {
ans[i] = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
update(ans[i]);
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << (i == n ? "\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
|
import sys
import math
from collections import defaultdict,deque
import heapq
n = int(sys.stdin.readline())
arr = list(map(int,sys.stdin.readline().split()))
tree = [0 for _ in range(n + 1)]
def upd(ind,val):
k = ind
while ind <= n:
tree[k] += val
k += (k&-k)
def presum(ind):
s = 0
k = ind
while k >= 1:
s += tree[k]
k -= (k&-k)
return s
heap = []
heapq.heapify(heap)
for i in range(n):
heapq.heappush(heap,[arr[i],-i])
ans = [0 for _ in range(n)]
cnt = 1
while heap:
a,ind = heapq.heappop(heap)
ans[-ind] = cnt
cnt += 1
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
|
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) return {LINF, 0};
if (i <= l and r <= j) return {mi[p], pos[p]};
int m = (l + r) / 2;
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);
assert(a.first != LINF or b.first != LINF);
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
|
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)));
long[] f = new long[234567];
long[] s = new long[234567];
int n;
void add(int i, long v) {
while (i <= n) {
f[i] += v;
i += (i & (-i));
}
}
int sum(int i) {
int sum = 0;
while (i > 0) {
sum += f[i];
i -= (i & (-i));
}
return sum;
}
void start() {
n = readInt();
for (int i = 1; i <= n; i++) s[i] = readLong();
for (int i = 1; i <= n; i++) add(i, i-1);
int[] ans = new int[n+1];
for (int i = n; i >= 1; i--) {
int ll = 0, rr = n + 1;
while (rr - ll > 1) {
int mid = (rr + ll) / 2;
int a = sum(mid);
if (a <= s[i]) ll = mid;
else rr = mid;
}
ans[i] = ll;
add(ll+1, -ll);
}
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;
struct node {
long long sum;
node *left, *right;
node() : sum(0), left(NULL), right(NULL) {}
};
node *root;
long long n, m, i, j, ans, sum, ind, x, r, l, a[500009], mas[500009], p[500009],
fix[500009];
void upd(node *&parent, long long L, long long R) {
if (!parent) {
parent = new node();
}
if (L + 1 == R) {
parent->sum = ind;
return;
}
if ((L + R) / 2 > ind) {
upd(parent->left, L, (L + R) / 2);
} else {
upd(parent->right, (L + R) / 2, R);
}
parent->sum = (parent->left ? parent->left->sum : 0) +
(parent->right ? parent->right->sum : 0);
}
long long get(node *parent, long long L, long long R) {
if (parent) {
if (L >= r || R <= l) return 0;
if (l <= L && R <= r) return parent->sum;
return get(parent->left, L, (L + R) / 2) +
get(parent->right, (L + R) / 2, R);
}
return 0;
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
mas[i] = mas[i - 1] + i - 1;
}
mas[n + 1] = mas[n] + n;
for (i = n; i >= 1; i--) {
long long Ll = 0, rr = n + 1, pas = 0, mm = 0;
while (Ll <= rr) {
mm = (rr + Ll + 1) / 2;
if (mas[mm] - get(root, 0, mm) <= a[i]) {
pas = mm;
Ll = mm + 1;
} else {
rr = mm - 1;
}
}
while (fix[pas] != 0) pas++;
p[i] = pas;
ind = pas;
fix[pas] = 1;
upd(root, 0, n + 1);
}
for (i = 1; i <= n; i++) {
cout << p[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>
const long long SIZE = (long long)2e5 + 5;
long long n, a[SIZE], ans[SIZE];
class SegmentTree {
public:
void build(const long long tl = 1, const long long tr = n,
const long long p = 1) {
if (tl == tr) {
node[p].val = a[tl];
return;
}
const long long mid = (tl + tr) >> 1;
build(tl, mid, p << 1);
build(mid + 1, tr, p << 1 | 1);
node[p].val = std::min(node[p << 1].val, node[p << 1 | 1].val);
return;
}
void update(const long long l, const long long r, const long long v,
const long long tl = 1, const long long tr = n,
const long long p = 1) {
if (l == tl && r == tr) {
node[p].val -= v;
node[p].tag += v;
return;
}
down(p);
const long long mid = (tl + tr) >> 1;
if (r <= mid) {
update(l, r, v, tl, mid, p << 1);
} else if (l > mid) {
update(l, r, v, mid + 1, tr, p << 1 | 1);
} else {
update(l, mid, v, tl, mid, p << 1);
update(mid + 1, r, v, mid + 1, tr, p << 1 | 1);
}
node[p].val = std::min(node[p << 1].val, node[p << 1 | 1].val);
return;
}
void del(const long long x, const long long tl = 1, const long long tr = n,
const long long p = 1) {
if (tl == tr) {
node[p].val = 0x3f3f3f3f;
return;
}
const long long mid = (tl + tr) >> 1;
if (x <= mid)
del(x, tl, mid, p << 1);
else
del(x, mid + 1, tr, p << 1 | 1);
node[p].val = std::min(node[p << 1].val, node[p << 1 | 1].val);
return;
}
long long query(const long long x = 0, const long long tl = 1,
const long long tr = n, const long long p = 1) {
if (tl == tr) return tl;
const long long mid = (tl + tr) >> 1;
down(p);
if (node[p << 1 | 1].val <= x)
return query(x, mid + 1, tr, p << 1 | 1);
else
return query(x, tl, mid, p << 1);
}
private:
struct Node {
long long val, tag;
Node(void) : val(0), tag(0) {}
} node[SIZE << 2];
inline void down(const long long p) {
if (node[p].tag == 0) return;
node[p << 1].val -= node[p].tag;
node[p << 1 | 1].val -= node[p].tag;
node[p << 1].tag += node[p].tag;
node[p << 1 | 1].tag -= node[p].tag;
node[p].tag = 0;
return;
}
} tree;
signed main(void) {
scanf("%lld", &n);
for (long long i = 1; i <= n; ++i) scanf("%lld", &a[i]);
tree.build();
for (long long i = 1; i <= n; ++i) {
const long long pos = tree.query();
ans[pos] = i;
tree.del(pos);
if (pos != n) tree.update(pos + 1, n, i);
}
for (long long i = 1; i <= n; ++i) printf("%lld%c", ans[i], " \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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 2e5 + 10;
const long long INF = 0x3f3f3f3f;
const long long LLINF = 0x3f3f3f3f3f3f3f3f;
const long long MOD = 1e9 + 7;
long long segtree[MAXN << 2];
long long lazy[MAXN << 2];
long long arr[MAXN];
long long ans[MAXN];
void pushup(long long rt) {
segtree[rt] = min(segtree[rt << 1], segtree[rt << 1 | 1]);
}
void pushdown(long long rt) {
if (lazy[rt]) {
segtree[rt << 1] = max(0ll, segtree[rt << 1] - lazy[rt]);
segtree[rt << 1 | 1] = max(0ll, segtree[rt << 1 | 1] - lazy[rt]);
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
lazy[rt] = 0;
}
}
void build(long long l, long long r, long long rt) {
lazy[rt] = 0;
if (l == r) {
segtree[rt] = arr[l];
return;
}
long long m = l + r >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
pushup(rt);
}
long long query(long long l, long long r, long long rt) {
if (l == r) {
return l;
}
long long 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(long long l, long long r, long long L, long long R, long long C,
long long rt) {
if (L <= l && r <= R) {
segtree[rt] = max(0ll, segtree[rt] - C);
lazy[rt] += C;
return;
}
long long 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(long long l, long long r, long long pos, long long rt) {
if (l == r && l == pos) {
segtree[rt] = INF;
return;
}
long long 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() {
long long n;
scanf("%lld", &n);
for (long long i = 1; i <= n; ++i) {
scanf("%lld", &arr[i]);
}
build(1, n, 1);
long long now = 0;
for (long long i = n; i > 0; --i) {
long long 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 (long long i = 1; i <= n; ++i) printf("%lld ", 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
|
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:
print([bit.get(i) for i in range(1, 16)])
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(ok, n + 2, -tmp)
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
|
python3
|
n = int(input())
result = [0 for i in range(n)]
s = set()
data = list(map(int, input().split()))
k = 0
while k != n:
m = -1
ind = -1
for i, d in enumerate(data):
if result[i] == 0:
if m < d:
m = d
ind = i
result[ind] = n - k
k += 1
print(' '.join(map(str, result)))
|
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 public class SegmentTree { // 1-based DS, OOP
int N; //the number of elements in the array as a power of 2 (i.e. after padding)
pair[] array, sTree;
long[]lazy;
SegmentTree(pair[] in)
{
array = in; N = in.length - 1;
sTree = new pair[N<<1]; //no. of nodes = 2*N - 1, we add one to cross out index zero
lazy = new long[N<<1];
build(1,1,N);
}
void build(int node, int b, int e) // O(n)
{
if(b == e)
sTree[node] = array[b];
else
{
int mid = b + e >> 1;
build(node<<1,b,mid);
build(node<<1|1,mid+1,e);
if(sTree[node<<1].compareTo(sTree[node<<1|1])<=0) {
sTree[node]=new pair(sTree[node<<1].n,sTree[node<<1].idx);
}
else {
sTree[node]=new pair(sTree[node<<1|1].n,sTree[node<<1|1].idx);
}
}
}
void update_range(int i, int j, long val) // O(log n)
{
update_range(1,1,N,i,j,val);
}
void update_range(int node, int b, int e, int i, int j, long val)
{
if(i > e || j < b)
return;
if(b >= i && e <= j)
{
sTree[node].n += (e-b+1)*val;
lazy[node] += val;
}
else
{
int mid = b + e >> 1;
propagate(node, b, mid, e);
update_range(node<<1,b,mid,i,j,val);
update_range(node<<1|1,mid+1,e,i,j,val);
if(sTree[node<<1].compareTo(sTree[node<<1|1])<=0) {
sTree[node]=new pair(sTree[node<<1].n,sTree[node<<1].idx);
}
else {
sTree[node]=new pair(sTree[node<<1|1].n,sTree[node<<1|1].idx);
}
}
}
void propagate(int node, int b, int mid, int e)
{
lazy[node<<1] += lazy[node];
lazy[node<<1|1] += lazy[node];
sTree[node<<1].n += (mid-b+1)*lazy[node];
sTree[node<<1|1].n += (e-mid)*lazy[node];
lazy[node] = 0;
}
pair query(int i, int j)
{
return query(1,1,N,i,j);
}
pair query(int node, int b, int e, int i, int j) // O(log n)
{
if(i>e || j <b)
return inf;
if(b>= i && e <= j)
return sTree[node];
int mid = b + e >> 1;
propagate(node, b, mid, e);
pair q1 = query(node<<1,b,mid,i,j);
pair q2 = query(node<<1|1,mid+1,e,i,j);
if(q1.compareTo(q2)<=0) {
return q1;
}
else {
return q2;
}
}
}
static pair inf=new pair(100000000000l,100000000);
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) {
if(n!=o.n) {
if(n>o.n)return 1;
return -1;
}
return idx-o.idx;
}
}
public static void main(String[] args) throws Exception{
MScanner sc = new MScanner(System.in);
PrintWriter pw=new PrintWriter(System.out);
int n = sc.nextInt();
int N = 1; while(N < n) N <<= 1; //padding
pair[] in = new pair[N + 1];
for(int i = 1; i <= n; i++)
in[i] = new pair(sc.nextInt(),-i);
for(int i=n+1;i<=N;i++) {
in[i]=inf;
}
int[]ans=new int[n];
SegmentTree st=new SegmentTree(in);
for(int i=1;i<=n;i++) {
pair p=st.query(1, n);
int indx=-p.idx;
ans[indx-1]=i;
st.update_range(indx, indx, 100000000000l);
if(indx!=n) {
st.update_range(indx+1, n, -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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = double;
using ull = unsigned long long;
void useiostream() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cout.setf(ios::fixed), cout.precision(20);
}
void drop(char const* bu) {
printf("%s", bu);
exit(0);
}
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ld const pi = acos(-1.0L);
ld const eps = 0.0001L;
template <typename T, typename TT>
bool mi(T& a, TT b) {
return a > b ? (a = b, true) : false;
}
template <typename T, typename TT>
bool ma(T& a, TT b) {
return a < b ? (a = b, true) : false;
}
mt19937 ran(time(0));
int const mod = 1000000007;
int bp(int a, int p, int mod) {
int re = 1;
while (p) {
if (p & 1) re = (re * 1LL * a) % mod;
p >>= 1;
a = (a * 1LL * a) % mod;
}
return re;
}
int n;
ll p[200505];
int arr[200505];
int const gs = 500;
struct gr {
ll adding = 0;
int mn = INT_MAX;
int ps = INT_MIN;
int l = INT_MAX;
int r = INT_MIN;
int gid = 0;
void drop() {
for (int i = l; i <= r; i++) {
p[i] += adding;
if (p[i] < 0) p[i] = INT_MAX / 2;
}
adding = 0;
mn = INT_MAX;
for (int i = l; i <= r; i++) {
if (mn > p[i]) {
mn = p[i];
}
if (mn == p[i]) {
ps = i;
}
}
}
};
gr g[gs];
int main() {
cin >> n;
int gg = 0;
for (int i = 0; i < n; i++) {
cin >> p[i];
if (i >= (gg + 1) * gs) gg++;
g[gg].gid = gg;
mi(g[gg].l, i);
ma(g[gg].r, i);
if (p[i] < g[gg].mn) {
g[gg].mn = p[i];
}
if (p[i] == g[gg].mn) {
g[gg].ps = i;
}
}
for (int i = 1; i <= n; i++) {
int id = -1;
int gega = -1;
for (int j = gg; j > -1; j--) {
if (g[j].mn + g[j].adding == 0) {
gega = j;
id = g[j].ps;
break;
}
}
arr[id] = i;
int l = id;
int r = g[gega].r;
for (int j = l; j <= r; j++) {
p[j] -= i;
}
g[gega].drop();
for (int j = gega + 1; j <= gg; j++) {
g[j].adding -= i;
}
}
for (int i = 0; i < n; i++) {
cout << arr[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;
long long n;
class fenwicktree {
public:
long long n;
long long b[100005];
fenwicktree(long long N) {
n = N;
for (long long i = 0; i <= n; i++) b[i] = 0;
}
long long sum(long long idx) {
long long ret = 0;
for (idx; idx > 0; idx -= (idx & -idx)) ret += b[idx];
return ret;
}
void add(long long idx, long long delta) {
for (idx; idx <= n; idx += (idx & -idx)) b[idx] += delta;
}
};
long long bin(long long l, long long r, long long x, fenwicktree &f) {
while (l < r) {
long long mid = (l + r + 1) / 2;
if (f.sum(mid) <= x)
l = mid;
else
r = mid - 1;
}
return l + 1;
}
void solve() {
cin >> n;
fenwicktree f(n);
for (long long i = 1; i <= n; i++) f.add(i, i);
long long a[200005];
long long res[200005];
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = n - 1; i >= 0; i--) {
long long x = bin(0, n - 1, a[i], f);
res[i] = x;
f.add(x, -x);
}
for (long long i = 0; i < n; i++) cout << res[i] << " ";
cout << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
t = 1;
while (t--) 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>
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
const int N = 2e5 + 5;
using namespace std;
long long s[N], n, ans[N], cnt;
struct node {
int l, r;
long long val;
} tree[N << 2];
void build(int root, int l, int r) {
tree[root].l = l;
tree[root].r = r;
if (tree[root].l == tree[root].r) {
tree[root].val = ++cnt;
return;
}
int mid = (l + r) >> 1;
build(root << 1, l, mid);
build(root << 1 | 1, mid + 1, r);
tree[root].val = tree[root << 1].val + tree[root << 1 | 1].val;
}
void query(int root, long long v) {
if (tree[root].l == tree[root].r) {
ans[cnt--] = tree[root].val;
tree[root].val = 0;
return;
}
if (v > tree[root << 1].val)
query(root << 1 | 1, v);
else
query(root << 1, v);
tree[root].val = tree[root << 1].val + tree[root << 1 | 1].val;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
build(1, 1, n);
for (long long i = 1; i <= n; ++i) cin >> s[i];
for (long long i = n; i > 0; --i) query(1, s[i] + 1);
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>
using namespace std;
const int MX = 2e5 + 7;
const int INF = 1e9;
pair<long long, int> first[4 * MX];
long long lz[4 * MX];
long long n, a[MX];
void add(int x, int val) {
first[x].first += val;
lz[x] += val;
}
void push(int x, int l, int r) {
if (l == r) return;
add(x << 1, lz[x]);
add(x << 1 | 1, lz[x]);
lz[x] = 0;
}
void upd(int x, int l, int r, int i, int j, int val) {
if (l > j || r < i) return;
push(x, l, r);
if (l >= i && r <= j) {
if (l == r) first[x].second = -l;
add(x, val);
return;
}
push(x, l, r);
int mid = (l + r) >> 1;
upd(x << 1, l, mid, i, j, val);
upd(x << 1 | 1, mid + 1, r, i, j, val);
first[x] = min(first[x << 1], first[x << 1 | 1]);
}
pair<long long, int> get(int x, int l, int r, int i, int j) {
if (l > j || r < i) return {INF, INF};
if (l >= i && r <= j) return first[x];
push(x, l, r);
int mid = (l + r) >> 1;
return min(get(x << 1, l, mid, i, j), get(x << 1 | 1, mid + 1, r, i, j));
}
long long ans[MX];
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
upd(1, 0, MX, i, i, a[i]);
}
for (int i = 1; i <= n; ++i) {
pair<long long, int> P = get(1, 0, MX, 1, n);
P.second *= -1;
ans[P.second] = i;
upd(1, 0, MX, P.second + 1, n, -1);
upd(1, 0, MX, P.second, P.second, INF);
}
for (int i = 1; i <= n; ++i) cout << ans[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
|
cpp
|
#include<bits/stdc++.h>
using namespace std;
bool cmp(pair<long long, int> a,pair<long long, int> b){
if(a.first==b.first)
return a.second>b.second;
else
return a.first<b.first;
}
int main(){
int n;
cin>>n;
vector<pair<long long, int> > v;
for(int i=1;i<=n;i++){
long long c;
cin>>c;
v.push_back(make_pair(c,i));
}
sort(v.begin(),v.end(),cmp);
int b[n+1]={0};
int cnt=1;
for(auto x:v){
//cout<<x.second<<" ";
b[x.second]=cnt;
cnt++;
}
for(int i=1;i<=n;i++)
cout<<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 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]);
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
|
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, ans[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 - 1) {
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]);
}
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 = -tree[1].second;
ans[pom] = i;
add(pom, pom, INF);
add(pom + 1, n, -pom);
}
for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, a[N], ans[N];
long long tag[N * 4], pre[N * 4];
inline int ls(int p) { return p << 1; }
inline int rs(int p) { return p << 1 | 1; }
inline void pushup(int p) { pre[p] = min(pre[ls(p)], pre[rs(p)]); }
void build(int p, int l, int r) {
if (l == r) {
pre[p] = a[l];
return;
}
int mid = (l + r) >> 1;
build(ls(p), l, mid);
build(rs(p), mid + 1, r);
pushup(p);
}
inline void f(int p, int l, int r, int k) {
tag[p] += k;
pre[p] += k;
}
inline void pushdown(int p, int l, int r) {
int mid = (l + r) >> 1;
f(ls(p), l, mid, tag[p]);
f(rs(p), mid + 1, r, tag[p]);
tag[p] = 0;
}
void update(int nl, int nr, int l, int r, int p, int k) {
if (nl <= l && r <= nr) {
pre[p] += k;
tag[p] += k;
return;
}
pushdown(p, l, r);
int mid = (l + r) >> 1;
if (nl <= mid) update(nl, nr, l, mid, ls(p), k);
if (nr > mid) update(nl, nr, mid + 1, r, rs(p), k);
pushup(p);
}
int find(int p, int l, int r) {
if (l == r) return l;
pushdown(p, l, r);
int mid = (l + r) >> 1;
int tmp = -1;
if (pre[rs(p)] == 0)
tmp = find(rs(p), mid + 1, r);
else
tmp = find(ls(p), l, mid);
return tmp;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
build(1, 1, n);
for (int i = 1; i <= n; i++) {
int pos = find(1, 1, n);
ans[pos] = i;
update(pos, pos, 1, n, 1, 1e9);
update(pos + 1, n, 1, n, 1, -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
|
java
|
/*Author: Satyajeet Singh, Delhi Technological University*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.math.*;
public class Main{
/*********************************************Constants******************************************/
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static long mod=(long)1e9+7;
static long mod1=998244353;
static boolean sieve[];
static ArrayList<Integer> primes;
static ArrayList<Long> factorial;
static HashSet<Pair> graph[];
static boolean oj = System.getProperty("ONLINE_JUDGE") != null;
/****************************************Solutions Begins***************************************/
static class segmentTree{
int n=0;
int[] lo,hi;
int[] value,delta;
int idx[];
segmentTree(int n){
this.n=n;
lo=new int[7*n+1];
hi=new int[7*n+1];
value=new int[7*n+1];
delta=new int[7*n+1];
init(1,1,n);
idx=new int[7*n+1];
}
void init(int i,int left,int right){
lo[i]=left;
hi[i]=right;
if(left==right){
return;
}
int mid=(left+right)/2;
init(2*i,left,mid);
init(2*i+1,mid+1,right);
}
void update(int left,int right,int val){
update(1,left,right,val);
}
Pair query(int left,int right){
return query(1,left,right);
}
void prop(int i){
delta[2*i]+=delta[i];
delta[2*i+1]+=delta[i];
delta[i]=0;
}
void update(int i){
int a=value[2*i]+delta[2*i];
int b=value[2*i+1]+delta[2*i+1];
if(a<=b){
value[i]=a;
if(a==b){
idx[i]=Math.max(idx[2*i],idx[2*i+1]);
}
else{
idx[i]=idx[2*i];
}
}
else{
value[i]=b;
idx[i]=idx[2*i+1];
}
}
void update(int i,int left,int right,int val){
if(left>hi[i]||right<lo[i]){
return;
}
if(lo[i]>=left&&hi[i]<=right){
delta[i]+=val;
value[i]+=delta[i];
prop(i);
if(left==right){
idx[i]=left;
}
return;
}
prop(i);
update(2*i,left,right,val);
update(2*i+1,left,right,val);
update(i);
}
Pair query(int i,int left,int right){
if(left>hi[i]||right<lo[i]){
return new Pair(Integer.MAX_VALUE,-1);
}
if(lo[i]>=left&&hi[i]<=right){
value[i]+=delta[i];
prop(i);
return new Pair(value[i],idx[i]);
}
prop(i);
Pair l=query(2*i,left,right);
Pair r=query(2*i+1,left,right);
update(i);
if(l.u<=r.u){
if(l.u==r.u){
return new Pair(l.u,Math.max(r.v,l.v));
}
else{
return l;
}
}
else{
return r;
}
}
}
public static void main (String[] args) throws Exception {
String st[]=nl();
int n=pi(st[0]);
int input[]=new int[n];
st=nl();
segmentTree seg=new segmentTree(n);
for(int i=0;i<n;i++){
input[i]=pi(st[i]);
seg.update(i+1,i+1,input[i]);
}
// debug(seg.query(1,n));
// debug(seg.lo);
// debug(seg.hi);
// debug(seg.value);
// debug(seg.idx);
int output[]=new int[n];
for(int i=1;i<=n;i++){
Pair p=seg.query(1,n);
int idx=p.v;
output[idx-1]=i;
seg.update(idx,idx,99);
if(idx+1<=n)
seg.update(idx+1,n,-i);
// debug(i);
// debug(seg.lo);
// debug(seg.hi);
// debug(seg.value);
// debug(seg.idx);
// debug(seg.delta);
}
for(int u:output){
out.print(u+" ");
}
/****************************************Solutions Ends**************************************************/
out.flush();
out.close();
}
/****************************************Template Begins************************************************/
static String[] nl() throws Exception{
return br.readLine().split(" ");
}
static String[] nls() throws Exception{
return br.readLine().split("");
}
static int pi(String str) {
return Integer.parseInt(str);
}
static long pl(String str){
return Long.parseLong(str);
}
static double pd(String str){
return Double.parseDouble(str);
}
/***************************************Precision Printing**********************************************/
static void printPrecision(double d){
DecimalFormat ft = new DecimalFormat("0.00000000000000000");
out.println(ft.format(d));
}
/**************************************Bit Manipulation**************************************************/
static void printMask(long mask){
System.out.println(Long.toBinaryString(mask));
}
static int countBit(int mask){
int ans=0;
while(mask!=0){
if(mask%2==1){
ans++;
}
mask/=2;
}
return ans;
}
/******************************************Graph*********************************************************/
static void Makegraph(int n){
graph=new HashSet[n];
for(int i=0;i<n;i++){
graph[i]=new HashSet<>();
}
}
static void addEdge(int a,int b,int c){
graph[a].add(new Pair(b,c));
}
/*********************************************PAIR********************************************************/
static class PairComp implements Comparator<Pair>{
public int compare(Pair p1,Pair p2){
return -((p1.u+p1.v)-(p2.u+p2.v));
}
}
static class Pair implements Comparable<Pair> {
int u;
int v;
int index=-1;
public Pair(int u, int v) {
this.u = u;
this.v = v;
}
public int hashCode() {
int hu = (int) (u ^ (u >>> 32));
int hv = (int) (v ^ (v >>> 32));
return 31 * hu + hv;
}
public boolean equals(Object o) {
Pair other = (Pair) o;
return u == other.u && v == other.v;
}
public int compareTo(Pair other) {
if(index!=other.index)
return Long.compare(index, other.index);
return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
}
public String toString() {
return "[u=" + u + ", v=" + v + "]";
}
}
/******************************************Long Pair*************************************************/
static class PairCompL implements Comparator<Pairl>{
public int compare(Pairl p1,Pairl p2){
long aa=p2.v-p1.v;
if(aa<0){
return -1;
}
else if(aa>0){
return 1;
}
else{
return 0;
}
}
}
static class Pairl implements Comparable<Pairl> {
long u;
long v;
int index=-1;
public Pairl(long u, long v) {
this.u = u;
this.v = v;
}
public int hashCode() {
int hu = (int) (u ^ (u >>> 32));
int hv = (int) (v ^ (v >>> 32));
return 31 * hu + hv;
}
public boolean equals(Object o) {
Pair other = (Pair) o;
return u == other.u && v == other.v;
}
public int compareTo(Pairl other) {
if(index!=other.index)
return Long.compare(index, other.index);
return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
}
public String toString() {
return "[u=" + u + ", v=" + v + "]";
}
}
/*****************************************DEBUG***********************************************************/
public static void debug(Object... o) {
if(!oj)
System.out.println(Arrays.deepToString(o));
}
/************************************MODULAR EXPONENTIATION***********************************************/
static long modulo(long a,long b,long c) {
long x=1;
long y=a;
while(b > 0){
if(b%2 == 1){
x=(x*y)%c;
}
y = (y*y)%c; // squaring the base
b /= 2;
}
return x%c;
}
/********************************************GCD**********************************************************/
static long gcd(long x, long y)
{
if(x==0)
return y;
if(y==0)
return x;
long r=0, a, b;
a = (x > y) ? x : y; // a is greater number
b = (x < y) ? x : y; // b is smaller number
r = b;
while(a % b != 0)
{
r = a % b;
a = b;
b = r;
}
return r;
}
/******************************************SIEVE**********************************************************/
static void sieveMake(int n){
sieve=new boolean[n];
Arrays.fill(sieve,true);
sieve[0]=false;
sieve[1]=false;
for(int i=2;i*i<n;i++){
if(sieve[i]){
for(int j=i*i;j<n;j+=i){
sieve[j]=false;
}
}
}
primes=new ArrayList<Integer>();
for(int i=0;i<n;i++){
if(sieve[i]){
primes.add(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;
using ll = long long;
using ii = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<ii>;
const ll MOD = 998244353;
const ll INF = 1e11 + 9;
const int MAXN = 1000006;
template <class T, class Operations>
struct LazySegmentTree {
int n, h;
vector<T> st;
Operations op;
void build(int L, int R) {
int len = 2;
for (L += n, R += n - 1; L > 1; len <<= 1) {
L >>= 1, R >>= 1;
for (int i = R; i >= L; --i) op.calc(i, len);
}
}
void build(int p) {
for (int len = 2; p > 1; len >>= 1) p >>= 1, op.calc(p, len);
}
void push(int L, int R) {
int s = h, len = 1 << h;
for (L += n, R += n - 1; s > 0; --s, len >>= 1)
for (int i = L >> s; i <= R >> s; ++i) op.pushDelOps(i, len);
}
void push(int p) {
for (int s = h, len = 1 << (h - 1); s > 0; --s, len >>= 1)
op.pushDelOps(p >> s, len);
}
void update(int L, int R, typename Operations::UpdateObject value) {
push(L, L + 1), push(R - 1, R);
int L0 = L, R0 = R, len = 1;
for (L += n, R += n; L < R; L >>= 1, R >>= 1, len <<= 1) {
if (L & 1) op.updateOp(L++, len, value);
if (R & 1) op.updateOp(--R, len, value);
}
build(L0, L0 + 1), build(R0 - 1, R0);
}
T query(int L, int R) {
push(L, L + 1), push(R - 1, R);
T resl = op.identity, resr = op.identity;
for (L += n, R += n; L < R; L >>= 1, R >>= 1) {
if (L & 1) resl = op.op(resl, st[L++]);
if (R & 1) resr = op.op(st[--R], resr);
}
return op.op(resl, resr);
}
LazySegmentTree(const vector<T> &A)
: n((int)A.size()),
h(sizeof(int) * 8 - __builtin_clz(n)),
st(n << 1),
op(*this) {
copy(A.begin(), A.end(), st.begin() + n);
build(0, n);
}
};
template <class T>
struct SumForMinMax {
const T identity = {INF, INF};
LazySegmentTree<T, SumForMinMax> &st;
vector<int> d;
SumForMinMax(LazySegmentTree<T, SumForMinMax> &st) : st(st), d(st.n) {}
inline T op(T a, T b) { return min(a, b); }
inline void calc(int p, int len) {
st.st[p] = min(st.st[p << 1], st.st[p << 1 | 1]);
}
using UpdateObject = int;
inline void updateOp(int p, int len, UpdateObject value) {
st.st[p].first += value;
if (p < st.n) d[p] += value;
}
inline void pushDelOps(int p, int len) {
updateOp(p << 1, len >> 1, d[p]);
updateOp(p << 1 | 1, len >> 1, d[p]);
d[p] = 0;
}
};
int n, m, k, ans[MAXN];
vii arr;
bool feasible = true;
string s;
char c, t[MAXN], C[MAXN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
arr.resize(n);
for (int i = 0; i < n; i++) {
cin >> arr[i].first;
arr[i].second = -i;
}
LazySegmentTree<ii, SumForMinMax<ii>> st(arr);
for (int i = 0; i < n; i++) {
int pos = -st.query(0, n).second;
ans[pos] = i + 1;
st.update(pos, pos + 1, INF);
if (pos < n - 1) st.update(pos + 1, n, -i - 1);
}
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
|
java
|
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static int inf = (int) 1e9;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int n = nextInt();
boolean a[] = new boolean[2 * n + 5];
int down = inf;
int b[] = new int [n - 1];
int last = n + 2;
a[last] = true;
for(int i = 0;i < n - 1;i++) {
b[i] = nextInt();
last += b[i];
if (last < 0 || last >= 2 * n + 5 || a[last]) {
pw.print(-1);
pw.close();
return;
}
a[last] = true;
}
boolean ans = true;
int cnt = 0;
for(int i = 0;i < 2 * n + 5;i++) {
if (a[i]) {
if (down == inf) down = i - 1;
cnt++;
}else{
if (cnt != 0 && cnt != n) ans = false;
}
}
if (cnt != n) ans = false;
if (!ans) pw.print(-1);
else{
int first = n + 2 - down;
for(int i = 0;i < n;i++) {
if (i != 0) first = b[i - 1] + first;
pw.print(first + " ");
}
}
pw.close();
}
static BufferedReader br;
static StringTokenizer st = new StringTokenizer("");
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static String next() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.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
|
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;
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);
for (int i = 1; i <= n; i++) {
auto p = query(1, 1, n, 1, n);
assert(p.second >= 1 and p.second <= n);
v[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 ", v[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>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> s(n);
map<long long, vector<int>> dp;
for (int i = 0; i < n; i++) cin >> s[i];
vector<long long> p(n);
for (int i = 0; i < n; i++) {
dp[s[i]].push_back(i);
}
int x = 1;
for (auto i : dp) {
reverse(i.second.begin(), i.second.end());
for (auto j : i.second) {
p[j] = x++;
}
}
for (int i = 0; i < n; i++) {
cout << p[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 = 2e5 + 5;
long long arr[MAXN];
long long answer[MAXN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
answer[n - 1] = (1 + abs((int)sqrt(1 + 8 * arr[n - 1]))) / 2;
for (int i = n - 2; i >= 0; i--) {
if (arr[i] >= arr[i + 1])
answer[i] = answer[i + 1] + (arr[i] - arr[i + 1] + 1);
else
answer[i] = (1 + abs((int)sqrt(1 + 8 * arr[i]))) / 2;
}
for (int i = 0; i < n; i++) cout << answer[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;
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;
}
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;
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("%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
|
cpp
|
#include <bits/stdc++.h>
const long long Inf = 1e18;
const long double eps = 1e-7;
long long LINF = (long long)2e18;
using namespace std;
long long mod = 1e9 + 7;
long long mod5 = 1e9 + 9;
long long mod3 = 998244353;
long long mod4 = 1000003;
long long mod2 = 1e9 + 123;
const int MAXN = 301000;
const int INF = 1000000000;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.setf(ios::fixed);
cout.precision(20);
int n;
cin >> n;
vector<int> a(n);
map<long long, vector<int> > pos;
set<int> zero;
for (long long(i) = 0; (i) < n; (i)++) {
cin >> a[i];
pos[a[i]].push_back(i);
if (a[i] == 0) zero.insert(i);
}
vector<int> ans(n);
long long tmp = 1;
long long cur = 2;
long long lol = 1;
while (zero.size() > 0) {
auto it = zero.end();
it--;
ans[(*it)] = lol;
zero.erase(it);
lol++;
for (auto& x : pos[tmp]) zero.insert(x);
tmp += cur;
cur++;
}
for (long long(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
|
python3
|
a = int(input())
b = list(map(int, input().split()))
l = 1
r = a
su = set()
for i in range(a - 1, -1, -1):
x = b[i]
d = (1 + (1 + 8 * x)**0.5)//2
while d in su:
d += 1
su.add(d)
b[i] = int(d)
print(b)
|
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
|
from __future__ import division, print_function
import heapq
def main():
n=int(input())
l1=list(map(int,input().split()))
temp=[]
for i in range(n):
temp.append((l1[i],-1*i))
heapq.heapify(temp)
res=[0]*n
y=1
while temp:
x=heapq.heappop(temp)
res[-1*x[1]]=y
y+=1
print(*res,sep=" ")
######## Python 2 and 3 footer by Pajenegod and c1729
# Note because cf runs old PyPy3 version which doesn't have the sped up
# unicode strings, PyPy3 strings will many times be slower than pypy2.
# There is a way to get around this by using binary strings in PyPy3
# but its syntax is different which makes it kind of a mess to use.
# So on cf, use PyPy2 for best string performance.
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
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)
input = lambda: sys.stdin.readline().rstrip('\r\n')
# Cout implemented in Python
import sys
class ostream:
def __lshift__(self,a):
sys.stdout.write(str(a))
return self
cout = ostream()
endl = '\n'
# Read all remaining integers in stdin, type is given by optional argument, this is fast
def readnumbers(zero = 0):
conv = ord if py2 else lambda x:x
A = []; numb = zero; sign = 1; i = 0; s = sys.stdin.buffer.read()
try:
while True:
if s[i] >= b'0' [0]:
numb = 10 * numb + conv(s[i]) - 48
elif s[i] == b'-' [0]: sign = -1
elif s[i] != b'\r' [0]:
A.append(sign*numb)
numb = zero; sign = 1
i += 1
except:pass
if s and s[-1] >= b'0' [0]:
A.append(sign*numb)
return A
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;
const int N = 1e5 + 5;
int su[N];
struct Node {
int l, r;
long long sum, add;
} t[N * 4];
void build(int p, int l, int r) {
t[p].l = l;
t[p].r = r;
if (l == r) {
t[p].sum = 1;
return;
}
int mid = (l + r) >> 1;
build((p << 1), l, mid);
build((p << 1) | 1, mid + 1, r);
t[p].sum = t[p << 1].sum + t[(p << 1) | 1].sum;
return;
}
void spread(int p) {
int l = p * 2;
int r = p * 2 + 1;
if (t[p].add) {
t[l].sum += t[p].add * (t[l].r - t[l].l + 1);
t[r].sum += t[p].add * (t[r].r - t[r].l + 1);
t[l].add += t[p].add;
t[r].add += t[p].add;
t[p].add = 0;
}
}
void change(int p, int l, int r, long long x) {
if (l <= t[p].l && r >= t[p].r) {
t[p].sum += x * (t[p].r - t[p].l + 1);
t[p].add += x;
return;
}
spread(p);
int mid = (t[p].r + t[p].l) >> 1;
if (l <= mid) change(p << 1, l, r, x);
if (r > mid) change((p << 1) | 1, l, r, x);
t[p].sum = t[p << 1].sum + t[(p << 1) | 1].sum;
}
long long ask(int p, int k) {
if (t[p].l == t[p].r) return t[p].l;
int mid = (t[p].l + t[p].r) >> 1;
int te = t[p << 1].sum;
if (k <= te)
return ask(p << 1, k);
else
return ask(p << 1 | 1, k - te);
}
int ans[N];
int main() {
int n, m;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &su[i]);
build(1, 1, n);
for (int i = n; i >= 1; i--) {
ans[i] = ask(1, su[i] + 1);
change(1, ans[i], ans[i], -1);
}
for (int i = 1; i <= n; i++) {
printf("%d", ans[i]);
if (i != n) printf(" ");
}
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>
#define int long long
#define endl '\n'
using namespace std;
typedef long long ll;
#define READ(a) ll a; cin >> a;
#define READARR(a, n) ll a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define READMAT(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}
#define pb(x) push_back(x)
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e15;
const ll mod = 1e9 + 7;
vector <ll> tree, a, lazy;
vector <char> black;
void build (ll v, ll start, ll end)
{
if (start == end) tree[v] = a[start];
else
{
int mid = (start + end)/2;
build(2*v, start, mid);
build(2*v + 1, mid + 1, end);
tree[v] = max(tree[2*v], tree[2*v + 1]);
}
}
void utility (ll v)
{
tree[2*v] -= lazy[v];
tree[2*v + 1] -= lazy[v];
lazy[2*v] = lazy[2*v + 1] = lazy[v];
lazy[v] = 0;
}
ll find (ll v, ll start, ll end, ll x)
{
if (start == end) {black[v] = true; tree[v] = 0; return start;}
if (lazy[v]) utility(v);
ll mid = (start + end)/2, temp;
if ((tree[2*v + 1] <= x) && !black[2*v + 1]) temp = find(2*v + 1, mid + 1, end, x);
else temp = find(2*v, start, mid, x);
if (black[2*v] && black[2*v + 1]) black[v] = true;
return temp;
}
void update (ll v, ll start, ll end, ll l, ll r, ll x)
{
if (lazy[v] && start != end) utility(v);
if (l > end || r < start) return;
if (l <= start && r >= end) {tree[v] -= x; lazy[v] = x;}
else
{
ll mid = (start + end)/2;
update(2*v, start, mid, l, r, x);
update(2*v + 1, mid + 1, end, l, r, x);
tree[v] = max(tree[2*v], tree[2*v + 1]);
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
READ(n);
a.resize(n + 1);
FOR (i, 1, n) a[i] = i*(i - 1)/2;
tree.resize(4*n);
black.resize(4*n);
lazy.resize(4*n);
build(1, 1, n);
READARR(s, n);
ll ans[n + 1] = {};
FORD (i, n, 1)
{
ans[i] = find(1, 1, n, s[i]);
update(1, 1, n, ans[i] + 1, n, ans[i]);
update(1, 1, n, ans[i], ans[i], inf);
}
FOR (i, 1, n) 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 long int N = 1e9 + 7;
long long int n;
vector<long long int> s(200004), p(200004), lazy(800004);
vector<pair<long long int, long long int> > tree(800004);
bool mycomp(pair<long long int, long long int> u,
pair<long long int, long long int> v) {
if (!u.first && !v.first) {
return u.second > v.second;
}
if (!u.first) {
return 1;
}
if (!v.first) {
return 0;
}
return 1;
}
void build(long long int node, long long int l, long long int r) {
if (l == r) {
tree[node] = {s[l], l};
return;
}
long long int mid = (l + r) / 2;
build(2 * node, l, mid);
build(2 * node + 1, mid + 1, r);
if (mycomp(tree[2 * node + 1], tree[2 * node])) {
tree[node] = tree[2 * node + 1];
} else {
tree[node] = tree[2 * node];
}
}
void push(long long int node) {
tree[2 * node].first += lazy[node];
lazy[2 * node] += lazy[node];
tree[2 * node + 1].first += lazy[node];
lazy[2 * node + 1] += lazy[node];
lazy[node] = 0;
}
void update(long long int node, long long int l, long long int r,
long long int st, long long int en) {
if (l > en || r < st) {
return;
}
if (l == r) {
tree[node].first -= 1;
lazy[node] -= 1;
return;
}
push(node);
long long int mid = (l + r) / 2;
update(2 * node, l, mid, st, en);
update(2 * node + 1, mid + 1, r, st, en);
if (mycomp(tree[2 * node + 1], tree[2 * node])) {
tree[node] = tree[2 * node + 1];
} else {
tree[node] = tree[2 * node];
}
}
pair<long long int, long long int> query(long long int node, long long int l,
long long int r, long long int st,
long long int en) {
if (l > en || r < st) {
return {-1 * N, 0};
}
if (l >= st && r <= en) {
return tree[node];
}
push(node);
long long int mid = (l + r) / 2;
pair<long long int, long long int> r1 = query(2 * node, l, mid, st, en);
pair<long long int, long long int> r2 =
query(2 * node + 1, mid + 1, r, st, en);
if (mycomp(r1, r2)) {
return r1;
}
return r2;
}
void solve() {
cin >> n;
for (long long int i = 1; i <= n; i++) {
cin >> s[i];
}
build(1, 1, n);
long long int i = 1;
while (i <= n) {
pair<long long int, long long int> u = query(1, 1, n, 1, n);
p[u.second] = i;
i++;
update(1, 1, n, u.second, n);
}
for (long long int i = 1; i <= n; i++) {
cout << p[i] << " ";
}
cout << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t = 1;
while (t--) {
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
|
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)));
long[] min = new long[1234567];
long[] add = new long[1234567];
long[] s = new long[1234567];
void push(int x) {
if (add[x] != 0) {
min[2*x] += add[x];
add[2*x] += add[x];
min[2*x+1] += add[x];
add[2*x+1] += add[x];
add[x] = 0;
}
}
void pull(int x) {
min[x] = Math.min(min[2*x], min[2*x+1]);
}
void build(int x, int ll, int rr) {
if (rr < ll) return;
if (ll == rr) {
min[x] = s[ll];
return;
}
int mid = (ll+rr)/2;
build(2*x, ll, mid);
build(2*x+1, mid+1, rr);
pull(x);
}
void upd(int x, int ll, int rr, int i, int j, long d) {
if (j < ll || rr < i) return;
if (i <= ll && rr <= j) {
add[x] += d;
min[x] += d;
return;
}
push(x);
int mid = (ll + rr) / 2;
upd(2*x, ll, mid, i, j, d);
upd(2*x+1, mid+1, rr, i, j, d);
pull(x);
}
int zero(int x, int ll, int rr) {
if (rr < ll || min[x] + add[x] > 0) return -1;
if (ll == rr) return ll;
push(x);
int mid = (ll + rr) / 2;
int ans = zero(2*x+1, mid + 1, rr);
if (ans < 0) ans = zero(2*x, ll, mid);
pull(x);
return ans;
}
void start() {
int n = readInt();
for (int i = 1; i <= n; i++) {
s[i] = readLong();
}
build(1, 1, n);
int[] ans = new int[n+1];
for (int i = 1; i <= n; i++) {
int j = zero(1, 1, n);
ans[j] = i;
upd(1, 1, n, j, j, 1_000_000_000_000_000L);
upd(1, 1, n, j+1, n, -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
|
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[] minus;
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] + minus[2 * node + 1] == arr[b] + minus[2 * node + 2]) {
if (a > b) {
return a;
}
return b;
}
if (arr[a] + minus[2 * node + 1] < arr[b] + minus[2 * node + 2]) {
return a;
}
return b;
}
void updateValue(int ss, int se,
int index, long value, int node, int act) {
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, act);
minus[2 * node + 2] -= act;
} else
updateValue(mid + 1, se, index, value, 2 * node + 2, act);
if (arr[st[2 * node + 1]] + minus[2 * node + 1] == arr[st[2 * node + 2]] + minus[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]] + minus[2 * node + 1] < arr[st[2 * node + 2]] + minus[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];
minus = new long[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;
updateValue(0, n - 1, m, (long) 1e18, 0, act);
act++;
}
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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
map<long long, vector<long long>> mp;
long long n, i, k = 1;
cin >> n;
long long A[n], ans[n];
for (i = 0; i < n; i++) cin >> A[i];
for (i = n - 1; i >= 0; i--) {
mp[A[i]].push_back(i);
}
for (auto x : mp) {
for (auto j : x.second) {
ans[j] = k;
k++;
}
}
for (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;
long long tr[3 * 200000 + 2], lazy[3 * 200000 + 2], a[200000 + 2],
id[3 * 200000 + 2];
void build(long long pos, long long st, long long ed) {
if (st == ed) {
tr[pos] = a[st];
id[pos] = st;
return;
}
long long mid = st + ((ed - st) >> 1);
build(pos << 1, st, mid);
build(pos << 1 | 1, mid + 1, ed);
id[pos] = (tr[pos << 1] < tr[pos << 1 | 1]) ? id[pos << 1] : id[pos << 1 | 1];
tr[pos] = min(tr[pos << 1], tr[pos << 1 | 1]);
}
void propagate(long long pos, long long st, long long ed) {
tr[pos] += (ed - st + 1) * lazy[pos];
if (st != ed) {
lazy[2 * pos] += lazy[pos];
lazy[2 * pos + 1] += lazy[pos];
}
lazy[pos] = 0;
}
void range_update(long long pos, long long st, long long ed, long long l,
long long r, long long val) {
if (lazy[pos] != 0) propagate(pos, st, ed);
if (st > r || ed < l) return;
if (st >= l && ed <= r) {
tr[pos] += (ed - st + 1) * val;
if (st != ed) {
lazy[2 * pos] += val;
lazy[2 * pos + 1] += val;
}
return;
}
long long mid = st + ((ed - st) >> 1);
range_update(pos << 1, st, mid, l, r, val);
range_update(pos << 1 | 1, mid + 1, ed, l, r, val);
id[pos] = (tr[pos << 1] < tr[pos << 1 | 1]) ? id[pos << 1] : id[pos << 1 | 1];
tr[pos] = min(tr[pos << 1], tr[pos << 1 | 1]);
}
long long range_query(long long pos, long long st, long long ed, long long l,
long long r) {
if (st > r || ed < l) return 0;
if (lazy[pos] != 0) propagate(pos, st, ed);
if (st >= l && ed <= r) {
return id[pos];
}
long long mid = st + ((ed - st) >> 1);
long long q1 = range_query(pos << 1, st, mid, l, r);
long long q2 = range_query(pos << 1 | 1, mid + 1, ed, l, r);
return min(q1, q2);
}
long long b[200000 + 2];
int main() {
long long i, j, k, p, q, n, tc, m, val;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
build(1, 1, n);
k = 0;
for (i = 1; i <= n; i++) {
p = range_query(1, 1, n, 1, n);
b[p] = ++k;
range_update(1, 1, n, p + 1, n, -k);
range_update(1, 1, n, p, p, n * n);
}
for (i = 1; i <= n; i++) {
cout << b[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
|
python3
|
import sys
input = sys.stdin.readline
nn = 3
bit=[0]*(2**nn+1)
def addbit(i, x):
while i <= 2**nn:
bit[i] += x
i += i & (-i)
def getsum(i):
ret = 0
while i != 0:
ret += bit[i]
i -= i&(-i)
return ret
def searchbit(x):
l, sl = 0, 0
d = 2**(nn-1)
while d:
m = l + d
sm = sl + bit[m]
if sm <= x:
l, sl = m, sm
d //= 2
return l + 1
n = int(input())
l = list(map(int, input().split()))
for i in range(1, n + 1):
addbit(i, i)
ans = [0 for _ in range(n)]
for i in range(n - 1, -1, -1):
a = searchbit(l[i])
addbit(a, -a)
ans[i] = a
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
|
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 = n + 1; i <= M; i++) tree[i + M - 1].first = INF;
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
|
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;
long long 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 val, 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] -= val;
if (st < en) {
lz[2 * node + 1] += val;
lz[2 * node + 2] += val;
}
return;
}
upd(l, r, val, 2 * node + 1, st, m);
upd(l, r, val, 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);
assert(ans >= 0 && ans <= n - 1);
w[ans] = go;
if (ans + 1 < n) upd(ans + 1, n - 1, go, 0, 0, n - 1);
go++;
pupd(ans, INF, 0, 0, n - 1);
}
for (i = 0; i < n; i++) cout << w[i] << " \n"[i == n - 1];
}
int main() {
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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long int fen[200005];
void update(int x, int v) {
for (int i = x; i < 200005; i += i & (-i)) {
fen[i] += v;
}
}
long long int get(int x) {
if (x == 0) return 0;
long long int sum = 0;
for (int i = x; i > 0; i -= i & (-i)) {
sum += fen[i];
}
return sum;
}
int main() {
int n, i, j, t;
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
int arr[n + 1], ans[n + 1];
for (i = 1; i < n + 1; i++) {
cin >> arr[i];
update(i, i);
}
for (i = n; i >= 1; i--) {
int l = 1, r = n + 1, mid;
while (r - l > 1) {
mid = (l + r) / 2;
long long int t = get(mid - 1);
if (t < arr[i]) {
l = mid + 1;
} else if (t == arr[i]) {
l = mid;
} else
r = mid;
}
ans[i] = l;
update(l, -l);
}
for (i = 1; i < n + 1; 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
|
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception r) {
r.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());//converts string to integer
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception r) {
r.printStackTrace();
}
return str;
}
}
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
//public static long mod = (long) (1e9+7);
public final static int MAX = (int)1e5;
// static Scanner sc = new Scanner(System.in);
static List<Integer>[] edge;
public static void main(String[] args) {
FastReader sc = new FastReader();
int n = sc.nextInt();
long[] s = new long[n];
TreeSet<Long> set = new TreeSet<>();
Map<Long,Integer> hash = new HashMap<>();
for(int i=0;i<n;++i) {
s[i] = sc.nextLong();
set.add(s[i]);
}
int k = 0;
for(long x : set) hash.put(x,k++);
TreeSet<Integer>[] sets = new TreeSet[hash.size()];
for(int i=0;i<sets.length;++i) sets[i] = new TreeSet<>();
for(int i=0;i<s.length;++i) {
sets[hash.get(s[i])].add(i);
}
k = 1;
int[] ans = new int[n];
for(Long key : hash.keySet()) {
int i = hash.get(key);
for(int idx : sets[i].descendingSet()) {
ans[idx] = k++;
}
}
for(int x : ans) out.print(x + " ");
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 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 << 1].tag;
node[no << 1].val += node[no << 1].tag;
node[no << 1 | 1].tag += node[no << 1 | 1].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
|
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.util.Comparator;
import java.io.InputStreamReader;
import java.util.ArrayList;
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 {
public void solve(int testNumber, inputClass sc, PrintWriter out) {
int n = sc.nextInt();
ArrayList<DRestorePermutation.Pair> tab = new ArrayList<>();
for (int i = 0; i < n; i++) {
tab.add(new DRestorePermutation.Pair(sc.nextInt(), i));
}
tab.sort(new Comparator<DRestorePermutation.Pair>() {
public int compare(DRestorePermutation.Pair a, DRestorePermutation.Pair b) {
if (a.x == b.x) {
return Integer.compare(b.y, a.y);
}
return Integer.compare(a.x, b.x);
}
});
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
ans[tab.get(i).y] = i + 1;
}
for (int i : ans) {
out.print(i + " ");
}
}
static class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
}
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());
}
}
}
|
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() {
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
|
cpp
|
#include <bits/stdc++.h>
const int N = 200005;
const long long INF = 5 * 1e10;
using namespace std;
int n, rs[N], mk[N * 2];
long long a[N], t[N * 2];
void bt(int u, int l, int r) {
if (l == r) {
t[u] = a[l];
return;
}
int mid = (l + r) / 2;
bt(u * 2, l, mid);
bt(u * 2 + 1, mid + 1, r);
t[u] = min(t[u * 2], t[u * 2 + 1]);
}
void pdw(int u) {
if (mk[u]) {
t[u * 2] += mk[u];
mk[u * 2] += mk[u];
t[u * 2 + 1] += mk[u];
mk[u * 2 + 1] += mk[u];
mk[u] = 0;
}
}
int fp(int u, int l, int r) {
if (l == r) return l;
pdw(u);
int mid = (l + r) / 2;
if (t[2 * u + 1] == 0)
return fp(u * 2 + 1, mid + 1, r);
else
return fp(u * 2, l, mid);
}
void upd(int u, int l, int r, int ul, int ur, long long c) {
if (l == ul && r == ur) {
t[u] += c;
mk[u] += c;
return;
}
pdw(u);
int mid = (l + r) / 2;
if (ur <= mid)
upd(u * 2, l, mid, ul, ur, c);
else if (ul > mid)
upd(u * 2 + 1, mid + 1, r, ul, ur, c);
else {
upd(u * 2, l, mid, ul, mid, c);
upd(u * 2 + 1, mid + 1, r, mid + 1, ur, c);
}
t[u] = min(t[u * 2], t[u * 2 + 1]);
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
bt(1, 1, n);
for (int i = 1; i <= n; ++i) {
int p = fp(1, 1, n);
rs[p] = i;
upd(1, 1, n, p, p, INF);
if (p != n) upd(1, 1, n, p + 1, n, -i);
}
for (int i = 1; i <= n; ++i) cout << rs[i] << 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>
#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]);
}
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 = -tree[1].second;
cout << pom << ' ';
add(pom, pom, INF);
add(pom + 1, n, -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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
inline long long add(long long a, long long b) {
a += b;
if (a >= mod) return a - mod;
return a;
}
inline long long mul(long long a, long long b) {
a *= b;
if (a >= mod) return a % mod;
return a;
}
inline long long power(long long a, long long b) {
long long p = 1;
while (b) {
if (b & 1) p = mul(p, a);
a = mul(a, a);
b /= 2;
}
return p;
}
const int max_n = 2e5 + 5;
class SegmentTree {
private:
int idx[5 * max_n];
int tree[5 * max_n];
int lazy[5 * max_n];
void build(int node, int start, int end) {
if (start == end) {
idx[node] = start;
tree[node] = A[start];
} else {
int mid = (start + end) / 2;
build(2 * node, start, mid);
build(2 * node + 1, mid + 1, end);
tree[node] = min(tree[2 * node], tree[2 * node + 1]);
if (tree[node] == tree[2 * node + 1]) {
idx[node] = idx[2 * node + 1];
} else {
idx[node] = idx[2 * node];
}
}
}
void updateRange(int node, int start, int end, int l, int r, long long val) {
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;
}
if (start > end || start > r || end < l) return;
if (start >= l && end <= r) {
tree[node] += val;
if (start != end) {
lazy[node * 2] += val;
lazy[node * 2 + 1] += val;
}
return;
}
int mid = (start + end) / 2;
updateRange(node * 2, start, mid, l, r, val);
updateRange(node * 2 + 1, mid + 1, end, l, r, val);
tree[node] = min(tree[node * 2], tree[node * 2 + 1]);
if (tree[node] == tree[2 * node + 1]) {
idx[node] = idx[2 * node + 1];
} else {
idx[node] = idx[2 * node];
}
}
pair<int, int> queryRange(int node, int start, int end, int l, int r) {
if (start > end || start > r || end < l) return {(int)1e9, -1};
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;
}
if (start >= l && end <= r) return {tree[node], idx[node]};
int mid = (start + end) / 2;
auto p1 = queryRange(node * 2, start, mid, l, r);
auto p2 = queryRange(node * 2 + 1, mid + 1, end, l, r);
if (p1.first >= p2.first) return p2;
return p1;
}
public:
int n;
vector<int> A;
SegmentTree() {}
SegmentTree(const vector<int> &v) {
A = v;
n = A.size();
memset(lazy, 0, sizeof lazy);
build(1, 0, n - 1);
}
void update(int l, int r, long long val = -1) {
updateRange(1, 0, n - 1, l, r, val);
}
int query(int l, int r) { return queryRange(1, 0, n - 1, l, r).second; }
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
SegmentTree st(a);
int b[n];
for (int i = 1; i <= n; i++) {
int idx = st.query(0, n - 1);
b[idx] = i;
st.update(idx, idx, 1e9);
st.update(idx + 1, n - 1, -i);
}
for (int i = 0; i < n; i++) {
cout << 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;
int n;
int z[200020];
long long a[200020];
long long c[200020];
void R(int x, int y) {
for (; x <= n; x += x & -x) {
c[x] += y;
}
}
int A(int x) {
int re = 0;
for (int i = 1 << 20; i > 0; i >>= 1) {
if (re + i <= n && c[re + i] <= x) {
x -= c[re += i];
}
}
return re + 1;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
R(i, i);
}
for (int i = n; i > 0; i--) {
z[i] = A(a[i]);
R(z[i], -z[i]);
}
for (int i = 1; i <= n; i++) {
printf("%d%c", z[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;
inline int read() {
char ch = getchar();
int x = 0, f = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int maxn = 2e5 + 100;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
long long a[maxn], sum[maxn << 4], c[maxn], d[maxn];
int b[maxn];
void build(int rt, int l, int r) {
if (l == r) {
sum[rt] = a[l];
return;
}
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void update(int o, int l, int r, int k) {
if (l == r) {
sum[o] = 0;
return;
}
int mid = l + ((r - l) >> 1);
if (k <= mid)
update(o << 1, l, mid, k);
else
update(o << 1 | 1, mid + 1, r, k);
sum[o] = sum[o << 1] + sum[o << 1 | 1];
}
int query(int rt, int l, int r, long long k) {
if (l == r) {
return l;
}
int mid = l + ((r - l) >> 1);
if (sum[rt << 1] >= k)
return query(rt << 1, l, mid, k);
else
return query(rt << 1 | 1, mid + 1, r, k - sum[rt << 1]);
}
int main() {
int n;
scanf("%d", &n);
long long minn = 1;
for (int i = 1; i <= n; i++) {
a[i] = i;
b[i] = 1;
scanf("%lld", &c[i]);
}
build(1, 1, n);
for (int i = n; i >= 1; i--) {
if (c[i] == 0) {
while (!b[minn]) minn++;
b[minn]--;
update(1, 1, n, minn);
d[i] = minn;
} else {
int k = query(1, 1, n, c[i]) + 1;
d[i] = k;
update(1, 1, n, k - 1);
}
}
for (int i = 1; i <= n; i++) printf("%d ", d[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
|
python2
|
def bsearch(k,tree,n):
low = 1
high = n
cnt = 0
while low <= high:
mid = (low+high)/2
tmp1 = sum(tree,mid-1)
tmp2 = sum(tree,mid)
s = mid*(mid-1)/2
if k+tmp2 == s:
break
elif k+tmp1 <s:
high = mid-1
else:
low = mid +1
return mid
def lowbit(x):
return x&-x
def add(t,i,v):
while i<len(t):
t[i]+=v
i += lowbit(i)
def sum(t,i):
ans = 0
while i>0:
ans += t[i]
i -= lowbit(i)
return ans
def main():
n = int(input())
arr = readnumbers()
tree = [0]*(n+1)
ans = []
dic = [0]*(n+1)
for i in range(n-1):
v = bsearch(arr[n-1-i],tree,n)
dic[v] = 1
ans.append(v)
add(tree,v,v)
for k,v in enumerate(dic[1:]):
if v == 0:
ans.append(k+1)
break
print " ".join(map(str,ans[::-1]))
######## Python 2 and 3 footer by Pajenegod and c1729
# Note because cf runs old PyPy3 version which doesn't have the sped up
# unicode strings, PyPy3 strings will many times be slower than pypy2.
# There is a way to get around this by using binary strings in PyPy3
# but its syntax is different which makes it kind of a mess to use.
# So on cf, use PyPy2 for best string performance.
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
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)
input = lambda: sys.stdin.readline().rstrip('\r\n')
# Cout implemented in Python
import sys
class ostream:
def __lshift__(self,a):
sys.stdout.write(str(a))
return self
cout = ostream()
endl = '\n'
# Read all remaining integers in stdin, type is given by optional argument, this is fast
def readnumbers(zero = 0):
conv = ord if py2 else lambda x:x
A = []; numb = zero; sign = 1; i = 0; s = sys.stdin.buffer.read()
try:
while True:
if s[i] >= b'0' [0]:
numb = 10 * numb + conv(s[i]) - 48
elif s[i] == b'-' [0]: sign = -1
elif s[i] != b'\r' [0]:
A.append(sign*numb)
numb = zero; sign = 1
i += 1
except:pass
if s and s[-1] >= b'0' [0]:
A.append(sign*numb)
return A
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;
const int64_t MOD = 1e9 + 7;
const int64_t N = 2e5 + 5;
int64_t arr[N];
int64_t st[8 * N];
int64_t n;
vector<int64_t> lazy(8 * N);
void build(int64_t v, int64_t tl, int64_t tr) {
if (tl == tr)
st[v] = arr[tl];
else {
int64_t tm = (tl + tr) / 2;
build(2 * v, tl, tm);
build(2 * v + 1, tm + 1, tr);
st[v] = min(st[v * 2], st[v * 2 + 1]);
}
}
void update(int64_t v, int64_t tl, int64_t tr, int64_t l, int64_t r,
int64_t add) {
if (l > r) return;
if (lazy[v]) {
st[v] += lazy[v];
lazy[2 * v] = lazy[v];
lazy[2 * v + 1] = lazy[v];
lazy[v] = 0;
}
if (l == tl && r == tr) {
st[v] += add;
if (tl != tr) {
lazy[2 * v] += add;
lazy[2 * v + 1] += add;
}
} else {
int64_t tm = (tl + tr) / 2;
update(v * 2, tl, tm, l, min(r, tm), add);
update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, add);
}
}
int64_t getMin(int64_t v, int64_t tl, int64_t tr, int64_t l, int64_t r) {
if (l > r) return 1e15;
if (lazy[v]) {
st[v] += lazy[v];
lazy[2 * v] = lazy[v];
lazy[2 * v + 1] = lazy[v];
lazy[v] = 0;
}
if (tl == l && tr == r) return st[v];
int64_t tm = (tl + tr) / 2;
return min(getMin(v * 2, tl, tm, l, min(r, tm)),
getMin(v * 2 + 1, tm + 1, tr, max(tm + 1, l), r));
}
int64_t getLastZero(int64_t tl, int64_t tr) {
if (tl == tr) return tl;
int64_t tm = (tl + tr) / 2;
if (tr >= n) {
return -2;
}
if (tl < 0) return -1;
if (getMin(1, 0, n - 1, tm + 1, tr) == 0) {
return getLastZero(tm + 1, tr);
} else
return getLastZero(tl, tm);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int64_t i = 0; i < n; i++) cin >> arr[i];
build(1, 0, n - 1);
int64_t ans[n];
int64_t ntbs = 1;
while (ntbs <= n) {
int64_t ind = getLastZero(0, n - 1);
ans[ind] = ntbs;
update(1, 0, n - 1, ind + 1, n - 1, -ntbs);
update(1, 0, n - 1, ind, ind, 1e11);
ntbs++;
}
for (int64_t 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;
inline int read() {
int x = 0, f = 1, c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f ^= 1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
return f ? x : -x;
}
struct BIT {
long long sum[200005];
void upd(int x, int v) {
for (; x < 200005; x += x & -x) sum[x] += v;
}
long long qry(int x) {
long long ans = 0;
for (; x; x -= x & -x) ans += sum[x];
return ans;
}
} bit;
int n;
long long s[200005];
int ans[200005];
int main() {
n = read();
for (int i = 1; i <= n; ++i) scanf("%lld", s + i);
for (int i = 1; i <= n; ++i) bit.upd(i, i);
for (int i = n; i; --i) {
int lb = 1, rb = n;
while (lb < rb) {
int md = (lb + rb + 1) >> 1;
if (bit.qry(md - 1) <= s[i])
lb = md;
else
rb = md - 1;
}
ans[i] = lb;
bit.upd(ans[i], -ans[i]);
}
for (int i = 1; i <= n; ++i) printf("%lld%c", ans[i], i == n ? 10 : 32);
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,-ind);
}
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 len = 2e5 + 9;
const long long mod = 998244353;
int a[len];
int b[len];
int n;
long long c[len];
void add(int i, int v) {
for (; i <= n; i += i & -i) c[i] += v;
}
long long sum(int i) {
long long ans = 0;
for (; i; i -= i & -i) ans += c[i];
return ans;
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) add(i, i);
for (int i = n; i >= 1; --i) {
int l = 1, r = n + 1, mid, ggb;
while (l < r) {
mid = (l + r) / 2;
if (sum(mid) > a[i]) {
ggb = mid;
r = mid;
} else
l = mid + 1;
}
b[i] = ggb;
add(ggb, -ggb);
}
for (int i = 1; i <= n; ++i) cout << b[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 long long MAXN = 5e5 + 5;
long long seg[6 * MAXN];
long long lazy[6 * MAXN];
long long arr[6 * MAXN];
long long ans[6 * MAXN];
void build(long long curr, long long l, long long r) {
if (l == r) {
seg[curr] = arr[l];
return;
}
long long mid = (l + r) / 2;
build(2 * curr, l, mid);
build(2 * curr + 1, mid + 1, r);
seg[curr] = min(seg[2 * curr], seg[2 * curr + 1]);
}
long long query(long long curr, long long l, long long r) {
if (lazy[curr]) {
if (r != l) {
seg[curr * 2] += lazy[curr];
lazy[curr * 2] += lazy[curr];
seg[2 * curr + 1] += lazy[curr];
lazy[curr * 2 + 1] += lazy[curr];
}
lazy[curr] = 0;
}
if (l == r) {
return r;
}
long long mid = (l + r) / 2;
long long res = 0;
if (seg[2 * curr + 1] == 0) {
res = query(2 * curr + 1, mid + 1, r);
} else {
res = query(2 * curr, l, mid);
}
seg[curr] = min(seg[2 * curr], seg[2 * curr + 1]);
return res;
}
void update(long long curr, long long l, long long r, long long tl,
long long tr, long long val) {
if (lazy[curr]) {
if (r != l) {
seg[curr * 2] += lazy[curr];
lazy[curr * 2] += lazy[curr];
seg[2 * curr + 1] += lazy[curr];
lazy[curr * 2 + 1] += lazy[curr];
}
lazy[curr] = 0;
}
if (l > tr || r < tl) {
return;
}
if (l >= tl && r <= tr) {
lazy[curr] += val;
seg[curr] += val;
if (l != r) {
seg[curr * 2] += lazy[curr];
lazy[curr * 2] += lazy[curr];
seg[2 * curr + 1] += lazy[curr];
lazy[2 * curr + 1] += lazy[curr];
}
lazy[curr] = 0;
return;
}
long long mid = (l + r) / 2;
update(2 * curr, l, mid, tl, tr, val);
update(2 * curr + 1, mid + 1, r, tl, tr, val);
seg[curr] = min(seg[2 * curr], seg[2 * curr + 1]);
}
int main() {
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> arr[i];
}
build(1, 1, n);
for (long long i = 1; i <= n; i++) {
long long pos = query(1, 1, n);
ans[pos] = i;
update(1, 1, n, pos + 1, n, -i);
update(1, 1, n, pos, pos, 1e9);
}
for (long long i = 1; i <= n; i++) {
cout << ans[i] << endl;
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.