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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
const int N = 2e5 + 20;
int n, s[N], a[N], fen[N];
inline void add(int p, int val) {
for (p++; p < N; p += p & -p) fen[p] += val;
}
inline int get(int p) {
int sum = 0;
for (; p; p -= p & -p) sum += fen[p];
return sum;
}
int32_t main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < n; i++) add(i + 1, i + 1);
int p = n - 1;
while (p >= 0) {
int low = 0, high = n + 1;
while (high - low > 1) {
int mid = (low + high) >> 1;
if (get(mid) <= s[p])
low = mid;
else
high = mid;
}
a[p] = low;
add(low, -low);
p--;
}
for (int i = 0; i < n; i++) cout << a[i] << ' ';
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 "
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
private static int KvoElem = 131072 * 2;
private static int KvoForSum = 262144 * 2;
private static int tree[] = new int[KvoForSum];
private static long t[] = new long[KvoForSum * 2];
private static int arr[] = new int[KvoElem + 1];
private static int Result = 0;
public static PrintWriter out;
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
Main user = new Main();
for (int i = KvoElem; i < KvoForSum; i++)
user.update(i - KvoElem, 1);
int n = sc.nextInt();
for (int i = 0; i < KvoElem; i++) {
arr[i] = 0;
}
build(1, 0, KvoElem);
long[] value = new long[n];
for (int i = 0; i < n; i++) {
value[i] = sc.nextLong();
}
List <Integer> list = new ArrayList <>();
for (int i = n - 1; i >= 0; i--) {
long x = user.find_need(value[i]);
user.searchNext((int) x);
list.add(Result);
update(1, 0, KvoElem, Result, Result);
}
for (int i = n - 1; i >= 0; i--) {
System.out.print(list.get(i) + " ");
}
}
private long find_need(long x) {
int l = 0;
int r = 200005;
while (r > l) {
long mid = (l + r) / 2;
long val = get_sum(mid);
if (val == x) {
return mid + 1;
} else if (val > x) {
r = (int) mid;
} else {
l = (int) (mid + 1);
}
}
return l + 1;
}
private static long get_sum(long x) {
return x * (x + 1) / 2 - sum1(1, 0, KvoElem, 0, (int) x);
}
private static int sum(int index) {
index += KvoElem;
int res = tree[index];
while (index > 1) {
if (index % 2 == 1)
res += tree[index - 1];
index /= 2;
}
return res;
}
private void searchNext(int index) {
index += KvoElem;
if (tree[index] == 1) {
update(index - KvoElem, 0);
Result = index - KvoElem;
} else {
int SearcInd = sum(index - KvoElem) + 1;
index = 1;
while (Main.tree[index] != SearcInd && (index * 2 + 1) < Main.KvoForSum) {
if (Main.tree[index] > SearcInd) {
index *= 2;
} else if (Main.tree[index] < SearcInd) {
SearcInd -= Main.tree[index];
index++;
}
}
UpdateIfRavno(index);
}
}
private void UpdateIfRavno(int index) {
while ((index * 2 + 1) < Main.KvoForSum) {
if (Main.tree[index * 2 + 1] == 0)
index *= 2;
else
index = index * 2 + 1;
}
Result = index - KvoElem;
update(index - Main.KvoElem, 0);
}
private void update(int index, int value) {
index += Main.KvoElem;
int a = value - Main.tree[index];
while (index >= 1) {
Main.tree[index] += a;
index /= 2;
}
}
public static void build(int v, int tl, int tr) {
if (tl == tr)
t[v] = arr[tl];
else {
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm + 1, tr);
t[v] = t[v * 2] + t[v * 2 + 1];
}
}
public static long sum1(int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
if (l == tl && r == tr)
return t[v];
int tm = (tl + tr) / 2;
return sum1(v * 2, tl, tm, l, Math.min(r, tm))
+ sum1(v * 2 + 1, tm + 1, tr, Math.max(l, tm + 1), r);
}
public static void update(int v, int tl, int tr, int pos, int new_val) {
if (tl == tr)
t[v] = new_val;
else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v * 2, tl, tm, pos, new_val);
else
update(v * 2 + 1, tm + 1, tr, pos, new_val);
t[v] = t[v * 2] + t[v * 2 + 1];
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
CORRECT
|
python3
|
def sumsegtree(l,seg,st,en,x):
if st==en:
seg[x]=l[st]
else:
mid=(st+en)>>1
sumsegtree(l,seg,st,mid,2*x)
sumsegtree(l,seg,mid+1,en,2*x+1)
seg[x]=seg[2*x]+seg[2*x+1]
def query(seg,st,en,val,x):
if st==en:
return seg[x]
mid=(st+en)>>1
if seg[2*x]>=val:
return query(seg,st,mid,val,2*x)
return query(seg,mid+1,en,val-seg[2*x],2*x+1)
def upd(seg,st,en,ind,val,x):
if st==en:
seg[x]=val
return
mid=(st+en)>>1
if mid>=ind:
upd(seg,st,mid,ind,val,2*x)
else:
upd(seg,mid+1,en,ind,val,2*x+1)
seg[x]=seg[2*x]+seg[2*x+1]
n=int(input())
l=list(map(int,range(1,n+1)))
s=[0]*n
p=list(map(int,input().split()))
seg=["#"]*(n<<2)
sumsegtree(l,seg,0,len(l)-1,1)
for i in range(n-1,-1,-1):
s[i]=query(seg,1,n,p[i]+1,1)
upd(seg,1,n,s[i],0,1)
print (*s)
|
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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5;
long long n, t[N], a[N], res[N];
set<long long> st;
void upd(long long i) {
long long d = i;
for (i; i < N; i |= i + 1) t[i] += d;
}
long long sum(long long l, long long r) {
long long s = 0;
for (; r >= 0; r &= r + 1, r--) s += t[r];
l--;
for (; l >= 0; l &= l + 1, l--) s -= t[l];
return s;
}
long long f(long long x) {
if (!sum(x, x))
return (x * x + x) / 2 - sum(0, x) - x;
else
return (x * x + x) / 2 - sum(0, x);
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
st.insert(i);
}
for (long long i = n - 1; i >= 0; i--) {
long long l = 1, r = n, med;
while (r - l > 1) {
med = (r + l) / 2;
if (f(med) > a[i])
r = med;
else
l = med;
}
if (f(r) == a[i]) {
res[i] = r;
upd(r);
} else {
res[i] = l;
upd(l);
}
}
for (long long i = 0; i < n; ++i) cout << res[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 "
]
}
|
CORRECT
|
python3
|
# https://codeforces.com/contest/1208/problem/D
from sys import stdin, stdout
input = stdin.readline
print = stdout.write
# For every i from N to 1, let's say the value of the si is x.
# So it means there are k smallest unused numbers whose sum is x.
# We simply put the (k+1)st number in the output permutation at this i, and continue to move left.
# segment tree and binary search
_ = input()
x = [int(i) for i in input().split()]
res = []
from math import log
class SegmentTree(object):
def __init__(self, nums):
self.arr = nums
self.l = len(nums)
self.tree = [0] * self.l + nums
for i in range(self.l - 1, 0, -1):
self.tree[i] = self.tree[i << 1] + self.tree[i << 1 | 1]
def update(self, i, val):
n = self.l + i
self.tree[n] = val
while n > 1:
self.tree[n >> 1] = self.tree[n] + self.tree[n ^ 1]
n >>= 1
def query(self, i, j):
m = self.l + i
n = self.l + j
res = 0
while m <= n:
if m & 1:
res += self.tree[m]
m += 1
m >>= 1
if n & 1 == 0:
res += self.tree[n]
n -= 1
n >>= 1
return res
tree = SegmentTree(list(range(1, len(x) + 1)))
org = len(x)
while x:
# from back to forth
q = x.pop()
lo = 0
hi = org - 1
while lo < hi:
mid = (lo + hi) // 2
# print(lo, hi, mid)
sm = tree.query(0, mid)
# print(sm, mid)
if sm > q:
hi = mid
else:
lo = mid + 1
# print(tree.arr, lo, hi)
idx = tree.arr[lo]
# print(idx)
tree.update(lo, 0)
# also from back to forth
res.append(idx)
print(' '.join(str(i) for i in res[::-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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using std::size_t;
template <typename T, typename Op = std::plus<T>>
class SegmentTree {
std::vector<T> arr;
int n;
Op op;
public:
template <class InputIterator>
SegmentTree(size_t n_, InputIterator begin);
SegmentTree(size_t n_, const T& value = T());
const T& operator[](size_t i) const;
inline T query(size_t l, size_t r) const;
void modify(size_t i, const T& value);
};
template <class T, typename Op>
template <class InputIterator>
SegmentTree<T, Op>::SegmentTree(size_t n_, InputIterator begin)
: n(n_), arr(n_ << 1) {
for (int i = n; i != n << 1; ++i) {
arr[i] = *begin;
++begin;
}
for (int i = n - 1; i >= 0; --i) {
arr[i] = op(arr[i << 1], arr[(i << 1) ^ 1]);
}
}
template <class T, typename Op>
SegmentTree<T, Op>::SegmentTree(size_t n_, const T& value)
: n(n_), arr(n_ << 1, value) {
for (int i = n - 1; i >= 0; --i) {
arr[i] = op(arr[i << 1], arr[(i << 1) ^ 1]);
}
}
template <class T, typename Op>
inline const T& SegmentTree<T, Op>::operator[](size_t i) const {
return arr[i + n];
}
template <class T, typename Op>
T SegmentTree<T, Op>::query(size_t l, size_t r) const {
bool linit = false, rinit = false;
T ltail, rtail;
l += n, r += n;
while (l < r) {
if (l & 1) {
if (linit) {
ltail = op(ltail, arr[l++]);
} else {
ltail = arr[l++];
linit = true;
}
}
if (r & 1) {
if (rinit) {
rtail = op(arr[--r], rtail);
} else {
rtail = arr[--r];
rinit = true;
}
}
l >>= 1, r >>= 1;
}
if (!linit) {
return rtail;
} else if (!rinit) {
return ltail;
} else {
return op(ltail, rtail);
}
}
template <class T, typename Op>
void SegmentTree<T, Op>::modify(size_t i, const T& value) {
i += n;
arr[i] = value;
while (i > 1) {
arr[i >> 1] = op(arr[i], arr[i ^ 1]);
i >>= 1;
}
}
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<int> p(n);
vector<long long> s(n);
for (long long& s_i : s) scanf("%lld", &s_i);
vector<long long> range(n);
iota(range.begin(), range.end(), 1);
SegmentTree<long long> T(n, range.begin());
for (int i = n - 1; i >= 0; --i) {
int x = 0, z = n, y = n >> 1;
while (x != y) {
(s[i] < T.query(0, y)) ? z = y : x = y;
y = (x + z) >> 1;
}
T.modify(x, 0);
p[i] = x + 1;
}
for (int x : p) printf("%d ", x);
}
|
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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, eps = 1e-10;
int n;
long long a[N];
long long tree[N];
int ans[N];
long long sum(int x) {
long long res = 0;
while (x) {
res += tree[x];
x -= (x & (-x));
}
return res;
}
void add(int x, long long sum) {
while (x <= n) {
tree[x] += sum;
x += (x & (-x));
}
return;
}
int main() {
std::ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = (1); i < (n + 1); ++i) cin >> a[i], add(i + 1, i);
for (int i = (n); i >= (1); --i) {
long long l = 1, r = n;
while (l < r) {
int m = (l + r + 1) / 2;
if (sum(m) <= a[i])
l = m;
else
r = m - 1;
}
ans[i] = r;
add(r + 1, -r);
}
for (int 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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n;
long long IM = -999999999999;
typedef struct nde {
int ll;
int rl;
long long val;
int pos;
long long prv;
struct nde* l;
struct nde* r;
struct nde* pr;
} prt;
prt* getnew() {
prt* p = new prt();
p->prv = 0;
return p;
}
prt* createTree(long long ar[], prt* pps[], int s, int e, prt* pr) {
prt* rt = getnew();
rt->ll = s;
rt->rl = e;
rt->pr = pr;
rt->l = 0;
rt->r = 0;
if (s == e) {
rt->pos = s;
rt->val = ar[s];
pps[s] = rt;
return rt;
} else {
prt* t = createTree(ar, pps, s, (s + e) / 2, rt);
prt* tt = createTree(ar, pps, 1 + ((s + e) / 2), e, rt);
rt->l = t;
rt->r = tt;
if (t->val == tt->val) {
rt->val = t->val;
rt->pos = tt->pos;
} else if (t->val < tt->val) {
rt->val = t->val;
rt->pos = t->pos;
} else {
rt->val = tt->val;
rt->pos = tt->pos;
}
}
return rt;
}
void uptd(int k, int rr, long long u, prt* p) {
if (p->ll >= k && p->rl <= rr) {
p->prv += u;
return;
}
p->l->prv += p->prv;
p->r->prv += p->prv;
p->prv = 0;
if (p->l->rl >= k && p->l->ll <= k) {
uptd(k, rr, u, p->l);
} else if (p->l->ll <= rr && p->l->rl >= rr) {
uptd(k, rr, u, p->l);
} else if (p->l->ll >= k && p->l->rl <= rr) {
uptd(k, rr, u, p->l);
} else {
}
if (p->r->rl >= k && p->r->ll <= k) {
uptd(k, rr, u, p->r);
} else if (p->r->ll <= rr && p->r->rl >= rr) {
uptd(k, rr, u, p->r);
} else if (p->r->ll >= k && p->r->rl <= rr) {
uptd(k, rr, u, p->r);
} else {
}
if ((p->l->val - p->l->prv) < (p->r->val - p->r->prv)) {
p->val = p->l->val - p->l->prv;
p->pos = p->l->pos;
} else {
p->val = p->r->val - p->r->prv;
p->pos = p->r->pos;
}
return;
}
void inactive(int k, prt* root) { uptd(k, k, IM, root); }
int main() {
int u;
cin >> n;
long long ar[n + 1];
prt* pps[n + 1];
int i = 1;
while (i <= n) {
cin >> ar[i];
i++;
}
prt* root = createTree(ar, pps, 1, n, 0);
i = 1;
int t;
while (i <= n) {
t = root->pos;
ar[t] = i;
inactive(t, root);
if (t < n) uptd(t + 1, n, i, root);
i++;
}
i = 1;
while (i <= n) {
cout << ar[i] << " ";
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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void ECHO(string _s) {
cout << endl;
(void)_s;
}
template <typename T, typename... Args>
void ECHO(string _s, T x, Args... args) {
int _i;
string _s2 = "";
for (_i = 0; _i < (int)(_s).size(); ++_i) {
if (_s[_i] == ',') break;
if (_s[_i] != ' ') _s2 += _s[_i];
}
if (_i == (int)(_s).size()) {
--_i;
}
cout << " (" << _s2 << "):" << x;
ECHO(_s.substr(_i + 1, (int)(_s).size() - _i - 1), args...);
}
template <typename T0, typename T1>
inline ostream& operator<<(ostream& os, pair<T0, T1>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
inline ostream& operator<<(ostream& os, vector<T>& v) {
for (int i = 0; i < (int)(v).size(); ++i) {
if (i > 0) cout << "_";
cout << v[i];
}
cout << endl;
return os;
}
inline long long _gcd(long long a, long long b) {
while (b) b %= a ^= b ^= a ^= b;
return a;
}
mt19937 ran(time(0));
const long double PI = acos(-1);
const long double eps = 1e-9;
const long long inf = LLONG_MAX / 10;
vector<long long> v, ans;
long long n;
const long long MAX = 2 * 1e5 + 10;
vector<long long> bit(MAX, 0);
void update(int dx, long long val) {
for (int i = dx + 1; i <= n; i += ((i) & (-(i)))) bit[i] += val;
}
long long query(int dx) {
long long ans = 0, i;
for (i = dx + 1; i > 0; i -= ((i) & (-(i)))) ans += bit[i];
return ans;
}
int get_number(long long sum) {
long long l = 0, r = n, mid, ans;
while (l + 1 < r) {
mid = (l + r) >> 1;
ans = query(mid);
if (ans <= sum)
l = mid;
else
r = mid;
}
return l + 1;
}
int main() {
ios::sync_with_stdio(false);
int i, x;
cin >> n;
v.resize(n + 1);
n = v.size() - 1;
ans.resize(n + 1);
for (i = 1; i <= n; ++i) {
update(i, i);
cin >> v[i];
}
for (i = n; i > 0; --i) {
x = get_number(v[i]);
ans[i] = x;
update(x, -x);
}
for (i = 1; i <= n; ++i) {
if (i > 1) cout << " ";
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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
long long res = 1;
a = a % 1000000007;
while (b > 0) {
if (b & 1) {
res = (res * a) % 1000000007;
b--;
}
a = (a * a) % 1000000007;
b >>= 1;
}
return res;
}
long long fermat_inv(long long y) { return power(y, 1000000007 - 2); }
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
long long sum[800050];
void build(long long a[], int v, int l, int r) {
if (l == r)
sum[v] = a[l];
else {
int m = (l + r) / 2;
build(a, 2 * v, l, m);
build(a, 2 * v + 1, m + 1, r);
sum[v] = sum[2 * v] + sum[2 * v + 1];
}
}
long long query(int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
else if (l == tl && r == tr)
return sum[v];
else {
int m = (tl + tr) / 2;
return query(2 * v, tl, m, l, min(r, m)) +
query(2 * v + 1, m + 1, tr, max(l, m + 1), r);
}
}
void update(int v, int l, int r, int pos, int val) {
if (l == r)
sum[v] = val;
else {
int m = (l + r) / 2;
if (pos <= m)
update(2 * v, l, m, pos, val);
else
update(2 * v + 1, m + 1, r, pos, val);
sum[v] = sum[2 * v] + sum[2 * v + 1];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1;
while (t--) {
int n;
cin >> n;
long long b[n];
long long a[n + 1];
a[0] = 0;
for (int i = 0; i < n; i++) {
cin >> b[i];
a[i + 1] = i + 1;
}
build(a, 1, 0, n);
for (int i = n - 1; i >= 0; i--) {
long long l = 0;
long long r = n;
long long pos = -1;
while (l <= r) {
long long m = (l + r) / 2;
long long val = query(1, 0, n, 0, m);
if (val <= b[i]) {
pos = m;
l = m + 1;
} else
r = m - 1;
}
b[i] = pos + 1;
update(1, 0, n, pos + 1, 0);
}
for (int i = 0; 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 "
]
}
|
CORRECT
|
java
|
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.*;
public class CFContest {
public static void main(String[] args) throws Exception {
boolean local = System.getProperty("ONLINE_JUDGE") == null;
boolean async = true;
Charset charset = Charset.forName("ascii");
FastIO io = local ? new FastIO(new FileInputStream("D:\\DATABASE\\TESTCASE\\Code.in"), System.out, charset) : new FastIO(System.in, System.out, charset);
Task task = new Task(io, new Debug(local));
if (async) {
Thread t = new Thread(null, task, "dalt", 1 << 27);
t.setPriority(Thread.MAX_PRIORITY);
t.start();
t.join();
} else {
task.run();
}
if (local) {
io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M");
}
io.flush();
}
public static class Task implements Runnable {
final FastIO io;
final Debug debug;
int inf = (int) 1e8;
long lInf = (long) 1e18;
public Task(FastIO io, Debug debug) {
this.io = io;
this.debug = debug;
}
@Override
public void run() {
solve();
}
public void solve() {
int n = io.readInt();
BIT bit = new BIT(n);
for (int i = 1; i <= n; i++) {
bit.update(i, i);
}
long[] s = new long[n + 1];
for (int i = 1; i <= n; i++) {
s[i] = io.readLong();
}
int[] restore = new int[n + 1];
for (int i = n; i >= 1; i--) {
int l = 1;
int r = n;
while (l < r) {
int m = (l + r + 1) >> 1;
if (bit.query(m - 1) > s[i]) {
r = m - 1;
} else {
l = m;
}
}
restore[i] = r;
bit.update(r, -r);
}
for (int i = 1; i <= n; i++) {
io.cache.append(restore[i]).append(' ');
}
}
}
/**
* Created by dalt on 2018/5/20.
*/
public static class BIT {
private long[] data;
private int n;
/**
* 创建大小A[1...n]
*/
public BIT(int n) {
this.n = n;
data = new long[n + 1];
}
/**
* 查询A[1]+A[2]+...+A[i]
*/
public long query(int i) {
long sum = 0;
for (; i > 0; i -= i & -i) {
sum += data[i];
}
return sum;
}
/**
* 将A[i]更新为A[i]+mod
*/
public void update(int i, long mod) {
for (; i <= n; i += i & -i) {
data[i] += mod;
}
}
/**
* 将A全部清0
*/
public void clear() {
Arrays.fill(data, 0);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (int i = 1; i <= n; i++) {
builder.append(query(i) - query(i - 1)).append(' ');
}
return builder.toString();
}
}
/**
* Mod operations
*/
public static class Modular {
int m;
public Modular(int m) {
this.m = m;
}
public int valueOf(int x) {
x %= m;
if (x < 0) {
x += m;
}
return x;
}
public int valueOf(long x) {
x %= m;
if (x < 0) {
x += m;
}
return (int) x;
}
public int mul(int x, int y) {
return valueOf((long) x * y);
}
public int mul(long x, long y) {
x = valueOf(x);
y = valueOf(y);
return valueOf(x * y);
}
public int plus(int x, int y) {
return valueOf(x + y);
}
public int plus(long x, long y) {
x = valueOf(x);
y = valueOf(y);
return valueOf(x + y);
}
public int subtract(int x, int y) {
return valueOf(x - y);
}
public int subtract(long x, long y) {
return valueOf(x - y);
}
@Override
public String toString() {
return "mod " + m;
}
}
public static class Node {
Node last;
Node since;
char c;
int index;
int offset;
int alloc;
int l;
}
public static class Randomized {
static Random random = new Random();
public static double nextDouble(double min, double max) {
return random.nextDouble() * (max - min) + min;
}
public static void randomizedArray(int[] data, int from, int to) {
to--;
for (int i = from; i <= to; i++) {
int s = nextInt(i, to);
int tmp = data[i];
data[i] = data[s];
data[s] = tmp;
}
}
public static void randomizedArray(long[] data, int from, int to) {
to--;
for (int i = from; i <= to; i++) {
int s = nextInt(i, to);
long tmp = data[i];
data[i] = data[s];
data[s] = tmp;
}
}
public static void randomizedArray(double[] data, int from, int to) {
to--;
for (int i = from; i <= to; i++) {
int s = nextInt(i, to);
double tmp = data[i];
data[i] = data[s];
data[s] = tmp;
}
}
public static void randomizedArray(float[] data, int from, int to) {
to--;
for (int i = from; i <= to; i++) {
int s = nextInt(i, to);
float tmp = data[i];
data[i] = data[s];
data[s] = tmp;
}
}
public static <T> void randomizedArray(T[] data, int from, int to) {
to--;
for (int i = from; i <= to; i++) {
int s = nextInt(i, to);
T tmp = data[i];
data[i] = data[s];
data[s] = tmp;
}
}
public static int nextInt(int l, int r) {
return random.nextInt(r - l + 1) + l;
}
}
public static class FastIO {
public final StringBuilder cache = new StringBuilder(1 << 20);
private final InputStream is;
private final OutputStream os;
private final Charset charset;
private StringBuilder defaultStringBuf = new StringBuilder(1 << 8);
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastIO(InputStream is, OutputStream os, Charset charset) {
this.is = is;
this.os = os;
this.charset = charset;
}
public FastIO(InputStream is, OutputStream os) {
this(is, os, Charset.forName("ascii"));
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
throw new RuntimeException(e);
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
public long readLong() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
long val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
public double readDouble() {
boolean sign = true;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+';
next = read();
}
long val = 0;
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
if (next != '.') {
return sign ? val : -val;
}
next = read();
long radix = 1;
long point = 0;
while (next >= '0' && next <= '9') {
point = point * 10 + next - '0';
radix = radix * 10;
next = read();
}
double result = val + (double) point / radix;
return sign ? result : -result;
}
public String readString(StringBuilder builder) {
skipBlank();
while (next > 32) {
builder.append((char) next);
next = read();
}
return builder.toString();
}
public String readString() {
defaultStringBuf.setLength(0);
return readString(defaultStringBuf);
}
public int readLine(char[] data, int offset) {
int originalOffset = offset;
while (next != -1 && next != '\n') {
data[offset++] = (char) next;
next = read();
}
return offset - originalOffset;
}
public int readString(char[] data, int offset) {
skipBlank();
int originalOffset = offset;
while (next > 32) {
data[offset++] = (char) next;
next = read();
}
return offset - originalOffset;
}
public int readString(byte[] data, int offset) {
skipBlank();
int originalOffset = offset;
while (next > 32) {
data[offset++] = (byte) next;
next = read();
}
return offset - originalOffset;
}
public char readChar() {
skipBlank();
char c = (char) next;
next = read();
return c;
}
public void flush() {
try {
os.write(cache.toString().getBytes(charset));
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public boolean hasMore() {
skipBlank();
return next != -1;
}
}
public static class Debug {
private boolean allowDebug;
public Debug(boolean allowDebug) {
this.allowDebug = allowDebug;
}
public void assertTrue(boolean flag) {
if (!allowDebug) {
return;
}
if (!flag) {
fail();
}
}
public void fail() {
throw new RuntimeException();
}
public void assertFalse(boolean flag) {
if (!allowDebug) {
return;
}
if (flag) {
fail();
}
}
private void outputName(String name) {
System.out.print(name + " = ");
}
public void debug(String name, int x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println("" + x);
}
public void debug(String name, long x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println("" + x);
}
public void debug(String name, double x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println("" + x);
}
public void debug(String name, int[] x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println(Arrays.toString(x));
}
public void debug(String name, long[] x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println(Arrays.toString(x));
}
public void debug(String name, double[] x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println(Arrays.toString(x));
}
public void debug(String name, Object x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println("" + x);
}
public void debug(String name, Object... x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println(Arrays.deepToString(x));
}
}
}
|
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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const long long inf = 1e11;
struct Node {
int l, r, num;
long long tag;
long long val;
} node[4 * maxn];
long long a[maxn];
int ans[maxn];
void up(int no) {
if (node[no << 1].val < node[no << 1 | 1].val) {
node[no].val = node[no << 1].val;
node[no].num = node[no << 1].num;
} else {
node[no].val = node[no << 1 | 1].val;
node[no].num = node[no << 1 | 1].num;
}
}
void build(int l, int r, int no) {
node[no].l = l;
node[no].r = r;
node[no].tag = 0;
if (l == r) {
node[no].val = a[l];
node[no].num = l;
return;
}
int mid = l + r >> 1;
build(l, mid, no << 1);
build(mid + 1, r, no << 1 | 1);
up(no);
}
void pushdown(int no) {
node[no << 1].tag += node[no].tag;
node[no << 1].val += node[no].tag;
node[no << 1 | 1].tag += node[no].tag;
node[no << 1 | 1].val += node[no].tag;
node[no].tag = 0;
}
void change(int l, int r, int no, long long num) {
if (node[no].r < l || node[no].l > r) return;
if (node[no].l >= l && node[no].r <= r) {
node[no].tag += num;
node[no].val += num;
return;
}
if (node[no].tag != 0) {
pushdown(no);
}
change(l, r, no << 1, num);
change(l, r, no << 1 | 1, num);
up(no);
}
int main() {
int n, i;
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
build(1, n, 1);
for (i = 1; i <= n; i++) {
int x = node[1].num;
ans[x] = i;
change(x, x, 1, inf);
change(x + 1, n, 1, -i);
}
for (i = 1; i <= n; i++) {
printf("%d ", ans[i]);
}
printf("\n");
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
CORRECT
|
java
|
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
public class P1208D7 {
public static void main(String[] args) throws IOException {
InputReader2 ir = new InputReader2();
PrintWriter pw = new PrintWriter(System.out);
int n = ir.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = ir.nextLong();
}
Fenwick f = new Fenwick(n);
for (int i = 1; i <= n; i++) {
f.add(i - 1, i);
}
int[] list = new int[n];
for (int i = n - 1; i >= 0; i--) {
int ind = f.indexWithGivenCumFreq(a[i]);
f.add(ind, -(ind + 1));
list[i] = ind + 1;
}
for (int i = 0; i < n; i++) {
pw.print(list[i] + " ");
}
pw.close();
}
static class Fenwick {
public final long[] bit;
public final int size;
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;
}
}
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();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
return ret;
}
private long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
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 "
]
}
|
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));
}
}
long sum(int i) {
long 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;
long 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 "
]
}
|
CORRECT
|
python3
|
NN = 18
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
N = int(input())
A = [int(a) for a in input().split()]
for i in range(1, N+1):
addbit(i, i)
ANS = []
for s in A[::-1]:
a = searchbit(s) + 1
addbit(a, -a)
ANS.append(a)
print(*ANS[::-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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long maxN = 2 * 100224;
struct BIT {
long long data[maxN] = {0};
void update(long long idx, long long val) {
while (idx < maxN) {
data[idx] += val;
idx += idx & -idx;
}
}
void update(long long l, long long r, long long val) {
update(l, val);
update(r + 1, -val);
}
long long query(long long idx) {
long long res = 0;
while (idx > 0) {
res += data[idx];
idx -= idx & -idx;
}
return res;
}
long long query(long long l, long long r) { return query(r) - query(l); }
};
struct LazyBIT {
BIT bitAdd, bitSub;
void update(long long l, long long r, long long val) {
bitAdd.update(l, r, val);
bitSub.update(l, r, (l - 1) * val);
bitSub.update(r + 1, (-r + l - 1) * val);
}
long long query(long long idx) {
return idx * bitAdd.query(idx) - bitSub.query(idx);
}
long long query(long long l, long long r) { return query(r) - query(l - 1); }
};
long long parent[maxN];
long long rnk[maxN];
long long lfmost[maxN];
long long rtmost[maxN];
long long vis[maxN];
void make_set(long long v) {
parent[v] = v;
rnk[v] = 0;
lfmost[v] = v;
rtmost[v] = v;
}
long long find_set(long long v) {
if (v == parent[v]) return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(long long a, long long b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (rnk[a] < rnk[b]) swap(a, b);
parent[b] = a;
lfmost[a] = min(lfmost[a], lfmost[b]);
rtmost[a] = max(rtmost[a], rtmost[b]);
if (rnk[a] == rnk[b]) rnk[a]++;
}
}
int main() {
long long n;
cin >> n;
vector<long long> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
long long sum = 0;
LazyBIT B;
for (long long i = 1; i <= n; i++) {
B.update(i, i, sum);
sum += i;
}
vector<long long> haha;
for (int i = n - 1; i >= 0; i--) {
long long lo = 1;
long long hi = n;
long long mid;
long long ans;
long long val;
long long temp;
while (lo <= hi) {
mid = (lo + hi) / 2;
if (vis[mid] == 1) {
if (lfmost[find_set(mid)] - 1 >= lo)
mid = lfmost[find_set(mid)] - 1;
else if (rtmost[find_set(mid)] + 1 <= hi)
mid = rtmost[find_set(mid)] + 1;
else
break;
}
val = B.query(mid, mid);
if (val == v[i]) {
ans = mid;
hi = mid - 1;
} else if (v[i] > val) {
lo = mid + 1;
} else if (v[i] < val) {
hi = mid - 1;
}
}
B.update(ans, n, -ans);
vis[ans] = 1;
make_set(ans);
if (vis[ans] == 1 && vis[ans + 1] == 1) union_sets(ans, ans + 1);
if (vis[ans] == 1 && vis[ans - 1] == 1) union_sets(ans, ans - 1);
haha.push_back(ans);
}
reverse(haha.begin(), haha.end());
for (auto u : haha) {
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 "
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class A {
FastScanner in;
PrintWriter out;
class ST {
int[] minPos;
long[] minValue;
long[] add;
ST(long[] vals) {
int n = vals.length;
minPos = new int[n * 4];
minValue = new long[n * 4];
add = new long[n * 4];
build(0, 0, n - 1, vals);
}
void build(int v, int l, int r, long[] vals) {
if (l == r) {
minPos[v] = l;
minValue[v] = vals[l];
add[v] = vals[l];
} else {
int m = (l + r) >> 1;
build(v * 2 + 1, l, m, vals);
build(v * 2 + 2, m + 1, r, vals);
relax(v);
}
}
void relax(int v) {
if (minValue[v * 2 + 1] < minValue[v * 2 + 2]) {
minValue[v] = minValue[v * 2 + 1] + add[v];
minPos[v] = minPos[v * 2 + 1];
} else {
minValue[v] = minValue[v * 2 + 2] + add[v];
minPos[v] = minPos[v * 2 + 2];
}
}
void add(int v, int l, int r, int needL, int needR, long delta) {
if (needL > r || needR < l) {
return;
}
if (needL <= l && needR >= r) {
add[v] += delta;
minValue[v] += delta;
} else {
int m = (l + r) >> 1;
add(v * 2 + 1, l, m, needL, needR, delta);
add(v * 2 + 2, m + 1, r, needL, needR, delta);
relax(v);
}
}
long[] t = new long[]{-1, Long.MAX_VALUE / 10};
long[] getMin(int v, int l, int r, int needL, int needR) {
if (needL > r || needR < l) {
return t;
}
if (needL <= l && needR >= r) {
return new long[]{minPos[v], minValue[v]};
} else {
int m = (l + r) >> 1;
long[] t1 = getMin(v * 2 + 1, l, m, needL, needR);
long[] t2 = getMin(v * 2 + 2, m + 1, r, needL, needR);
if (t1[1] < t2[1]) {
return new long[]{t1[0], t1[1] + add[v]};
} else {
return new long[]{t2[0], t2[1] + add[v]};
}
}
}
}
void solve() {
int n = in.nextInt();
long[] sum = new long[n];
for (int i = 0; i < n; i++) {
sum[i] = in.nextLong();
}
ST st = new ST(sum);
int[] res = new int[n];
for (int i = 0; i < n; i++) {
long[] f = st.getMin(0, 0, n - 1, 0, n - 1);
int pos = (int) f[0];
res[pos] = i + 1;
st.add(0, 0, n - 1, pos + 1, n - 1, -(i + 1));
st.add(0, 0, n - 1, pos, pos, Long.MAX_VALUE / 10);
}
for (int i : res) {
out.print(i + " ");
}
out.println();
}
void run() {
try {
in = new FastScanner(new File("A.in"));
out = new PrintWriter(new File("A.out"));
solve();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void runIO() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] args) {
new A().runIO();
}
}
|
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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
struct BIT {
int n;
long long a[200005];
int lowbit(int x) { return x & -x; }
void build(int size) { n = size; }
void update(int x, int val) {
for (; x <= n; x += lowbit(x)) a[x] += val;
}
long long query(int x) {
long long ret = 0;
for (; x; x -= lowbit(x)) ret += a[x];
return ret;
}
};
BIT b;
long long s[200005];
int a[200005];
int main() {
int n;
scanf("%d", &n);
b.build(n);
for (int i = 1; i <= n; ++i) scanf("%lld", &s[i]);
for (int i = 1; i <= n; ++i) b.update(i, i);
for (int i = n; i; --i) {
int left = 1, right = n;
while (left < right) {
int mid = (left + right + 1) >> 1;
if (b.query(mid - 1) <= s[i])
left = mid;
else
right = mid - 1;
}
a[i] = left;
b.update(a[i], -a[i]);
}
for (int i = 1; i <= n; ++i) printf("%d ", a[i]);
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
CORRECT
|
python3
|
# ---------------------------iye ha aam zindegi---------------------------------------------
import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
mod = 10 ** 9 + 7
mod1 = 998244353
# ------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
#sys.setrecursionlimit(300000)
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# -------------------game starts now----------------------------------------------------import math
class TreeNode:
def __init__(self, k, v):
self.key = k
self.value = v
self.left = None
self.right = None
self.parent = None
self.height = 1
self.num_left = 1
self.num_total = 1
class AvlTree:
def __init__(self):
self._tree = None
def add(self, k, v):
if not self._tree:
self._tree = TreeNode(k, v)
return
node = self._add(k, v)
if node:
self._rebalance(node)
def _add(self, k, v):
node = self._tree
while node:
if k < node.key:
if node.left:
node = node.left
else:
node.left = TreeNode(k, v)
node.left.parent = node
return node.left
elif node.key < k:
if node.right:
node = node.right
else:
node.right = TreeNode(k, v)
node.right.parent = node
return node.right
else:
node.value = v
return
@staticmethod
def get_height(x):
return x.height if x else 0
@staticmethod
def get_num_total(x):
return x.num_total if x else 0
def _rebalance(self, node):
n = node
while n:
lh = self.get_height(n.left)
rh = self.get_height(n.right)
n.height = max(lh, rh) + 1
balance_factor = lh - rh
n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
n.num_left = 1 + self.get_num_total(n.left)
if balance_factor > 1:
if self.get_height(n.left.left) < self.get_height(n.left.right):
self._rotate_left(n.left)
self._rotate_right(n)
elif balance_factor < -1:
if self.get_height(n.right.right) < self.get_height(n.right.left):
self._rotate_right(n.right)
self._rotate_left(n)
else:
n = n.parent
def _remove_one(self, node):
"""
Side effect!!! Changes node. Node should have exactly one child
"""
replacement = node.left or node.right
if node.parent:
if AvlTree._is_left(node):
node.parent.left = replacement
else:
node.parent.right = replacement
replacement.parent = node.parent
node.parent = None
else:
self._tree = replacement
replacement.parent = None
node.left = None
node.right = None
node.parent = None
self._rebalance(replacement)
def _remove_leaf(self, node):
if node.parent:
if AvlTree._is_left(node):
node.parent.left = None
else:
node.parent.right = None
self._rebalance(node.parent)
else:
self._tree = None
node.parent = None
node.left = None
node.right = None
def remove(self, k):
node = self._get_node(k)
if not node:
return
if AvlTree._is_leaf(node):
self._remove_leaf(node)
return
if node.left and node.right:
nxt = AvlTree._get_next(node)
node.key = nxt.key
node.value = nxt.value
if self._is_leaf(nxt):
self._remove_leaf(nxt)
else:
self._remove_one(nxt)
self._rebalance(node)
else:
self._remove_one(node)
def get(self, k):
node = self._get_node(k)
return node.value if node else -1
def _get_node(self, k):
if not self._tree:
return None
node = self._tree
while node:
if k < node.key:
node = node.left
elif node.key < k:
node = node.right
else:
return node
return None
def get_at(self, pos):
x = pos + 1
node = self._tree
while node:
if x < node.num_left:
node = node.left
elif node.num_left < x:
x -= node.num_left
node = node.right
else:
return (node.key, node.value)
raise IndexError("Out of ranges")
@staticmethod
def _is_left(node):
return node.parent.left and node.parent.left == node
@staticmethod
def _is_leaf(node):
return node.left is None and node.right is None
def _rotate_right(self, node):
if not node.parent:
self._tree = node.left
node.left.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.left
node.left.parent = node.parent
else:
node.parent.right = node.left
node.left.parent = node.parent
bk = node.left.right
node.left.right = node
node.parent = node.left
node.left = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
def _rotate_left(self, node):
if not node.parent:
self._tree = node.right
node.right.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.right
node.right.parent = node.parent
else:
node.parent.right = node.right
node.right.parent = node.parent
bk = node.right.left
node.right.left = node
node.parent = node.right
node.right = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
@staticmethod
def _get_next(node):
if not node.right:
return node.parent
n = node.right
while n.left:
n = n.left
return n
# -----------------------------------------------binary seacrh tree---------------------------------------
class SegmentTree1:
def __init__(self, data, default=0, func=lambda a, b: max(a, b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------game starts now----------------------------------------------------import math
class SegmentTree:
def __init__(self, data, default=0, func=lambda a, b: a + b):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------------------iye ha chutiya zindegi-------------------------------------
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
# --------------------------------------iye ha combinations ka zindegi---------------------------------
def powm(a, n, m):
if a == 1 or n == 0:
return 1
if n % 2 == 0:
s = powm(a, n // 2, m)
return s * s % m
else:
return a * powm(a, n - 1, m) % m
# --------------------------------------iye ha power ka zindegi---------------------------------
def sort_list(list1, list2):
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z
# --------------------------------------------------product----------------------------------------
def product(l):
por = 1
for i in range(len(l)):
por *= l[i]
return por
# --------------------------------------------------binary----------------------------------------
def binarySearchCount(arr, n, key):
left = 0
right = n - 1
count = 0
while (left <= right):
mid = int((right + left) / 2)
# Check if middle element is
# less than or equal to key
if (arr[mid] < key):
count = mid + 1
left = mid + 1
# If key is smaller, ignore right half
else:
right = mid - 1
return count
# --------------------------------------------------binary----------------------------------------
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def binary(x, length):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
def countGreater(arr, n, k):
l = 0
r = n - 1
# Stores the index of the left most element
# from the array which is greater than k
leftGreater = n
# Finds number of elements greater than k
while (l <= r):
m = int(l + (r - l) / 2)
if (arr[m] >= k):
leftGreater = m
r = m - 1
# If mid element is less than
# or equal to k update l
else:
l = m + 1
# Return the count of elements
# greater than k
return (n - leftGreater)
# --------------------------------------------------binary------------------------------------
n=int(input())
l=list(map(int,input().split()))
f=[i for i in range(1,n+1)]
s=SegmentTree(f)
ans=[0]*n
for i in range(n-1,-1,-1):
st=1
end=n
while(st<=end):
mid=(st+end)//2
su=s.query(0,mid-2)
if su==l[i]:
an=mid
st=mid+1
elif su<l[i]:
st=mid+1
else:
end=mid-1
ans[i]=an
s.__setitem__(an-1,0)
print(*ans,sep=" ")
|
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 "
]
}
|
CORRECT
|
python3
|
import sys
input = sys.stdin.readline
class SegTree(object):
"""docstring for SegTree"""
def __init__(self, n, arr):
self.n = n
self.arr = arr
self.tree = [0 for i in range(2*n)]
def construct(self): # Construction
for i in range(self.n):
self.tree[n+i] = self.arr[i]
for i in range(n-1,0,-1):
self.tree[i] = self.function(self.tree[2*i],self.tree[2*i+1])
def update(self,index,value):
start = index+self.n
self.tree[start] = value
while start>0:
start = start//2
self.tree[start] = self.function(self.tree[2*start],self.tree[2*start+1])
def calc(self,low,high): # 0-indexed
low+=self.n
high+=self.n
ans = 0 # Needs to initialised
while low<high:
if low%2:
ans = self.function(ans, self.tree[low])
low += 1
if high%2:
high -= 1
ans = self.function(ans, self.tree[high])
low = low//2
high = high//2
return ans
def function(self,a,b): # Function used to construct Segment Tree
return a + b
def find(num):
low = 0
high = n-1
while low<high:
mid = (low+high)//2
if st.calc(0,mid+1)>num:
high = mid - 1
else:
low = mid + 1
if st.calc(0,low+1)>num:
return low
else:
return low + 1
n = int(input())
a = list(map(int,input().split()))
arr = [i for i in range(1,n+1)]
st = SegTree(n,arr)
st.construct()
ans = [-1]*n
for i in range(n-1,-1,-1):
ind = find(a[i])
# print (a[i],ind,arr)
ans[i] = arr[ind]
st.update(ind,0)
print (*ans)
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long n, s[200005], tree[800005], lazy[800005], ans[200005];
void pushdown(long long ind, long long l, long long r) {
if (l != r && lazy[ind] != 0) {
lazy[2 * ind] += lazy[ind];
tree[2 * ind] += lazy[ind];
lazy[2 * ind + 1] += lazy[ind];
tree[2 * ind + 1] += lazy[ind];
lazy[ind] = 0;
}
}
void build(long long ind, long long l, long long r) {
if (l == r) {
tree[ind] = s[l];
return;
}
long long mid = (l + r) / 2;
build(2 * ind, l, mid);
build(2 * ind + 1, mid + 1, r);
tree[ind] = min(tree[2 * ind], tree[2 * ind + 1]);
}
long long query(long long ind, long long l, long long r) {
pushdown(ind, l, r);
if (l == r) return l;
tree[ind] = min(tree[2 * ind], tree[2 * ind + 1]);
long long mid = (l + r) / 2;
if (tree[2 * ind + 1] == 0)
query(2 * ind + 1, mid + 1, r);
else
query(2 * ind, l, mid);
}
void update(long long ind, long long l, long long r, long long ll, long long rr,
long long val) {
pushdown(ind, l, r);
if (r < ll || l > rr) return;
if (ll <= l && r <= rr) {
tree[ind] += val;
lazy[ind] += val;
return;
}
long long mid = (l + r) / 2;
update(2 * ind, l, mid, ll, rr, val);
update(2 * ind + 1, mid + 1, r, ll, rr, val);
tree[ind] = min(tree[2 * ind], tree[2 * ind + 1]);
}
int32_t main() {
cin >> n;
for (long long i = 1; i <= n; ++i) cin >> s[i];
build(1, 1, n);
for (long long i = 1; i <= n; ++i) {
long long t = query(1, 1, n);
ans[t] = i;
if (t < n) update(1, 1, n, t + 1, n, -i);
update(1, 1, n, t, t, 1e18);
}
for (long long 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 "
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Main {
static class Pair {
long x;
int i;
public Pair (long x, int i) {
this.x = x;
this.i = i;
}
}
static long[] s, a, add;
static Pair[] t;
static int N = 1;
static long INF = Long.MAX_VALUE / 10;
static void build() {
for (int i = 0; i < N; i++) {
t[i + N] = new Pair(s[i], i);
}
for (int i = N - 1; i >= 1; i--) {
if (t[i * 2].x >= t[i * 2 + 1].x) {
t[i] = new Pair(t[i * 2 + 1].x, t[i * 2 + 1].i);
} else {
t[i] = new Pair(t[i * 2].x, t[i * 2].i);
}
}
}
static Pair min() {
return min(1, 0, N, 0, N);
}
static Pair min(int v, int vl, int vr, int ql, int qr) {
if (vl >= qr || vr <= ql) {
return new Pair(INF, -1);
}
push(v);
if (vl >= ql && vr <= qr) {
return t[v];
}
Pair o1 = min(v * 2, vl, (vl + vr) / 2, ql, qr);
Pair o2 = min(v * 2 + 1, (vl + vr) / 2, vr, ql, qr);
if (o1.x < o2.x) {
return new Pair(o1.x, o1.i);
}
return new Pair(o2.x, o2.i);
}
static void push(int v) {
t[v].x += add[v];
add[v * 2] += add[v];
add[v * 2 + 1] += add[v];
add[v] = 0;
}
static void update(int l, int r, long value) {
update(1, 0, N, l, r, value);
}
static void update(int v, int vl, int vr, int ql, int qr, long value) {
if (vl >= qr || vr <= ql) {
return;
}
push(v);
if (vl >= ql && vr <= qr) {
add[v] -= value;
return;
}
update(v * 2, vl, (vl + vr) / 2, ql, qr, value);
update(v * 2 + 1, (vl + vr) / 2, vr, ql, qr, value);
push(v * 2);
push(v * 2 + 1);
if (t[v * 2].x >= t[v * 2 + 1].x) {
t[v] = new Pair(t[v * 2 + 1].x, t[v * 2 + 1].i);
} else {
t[v] = new Pair(t[v * 2].x, t[v * 2].i);
}
}
public static void main(String[] args) throws IOException{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
while (N * 2 < n) N *= 2;
N *= 2;
s = new long[N];
t = new Pair[4 * N];
a = new long[N];
add = new long[4 * N];
Arrays.fill(s, INF);
for (int i = 0; i < n; i++) {
s[i] = scan.nextLong();
}
build();
long value = 1;
while (value <= n) {
Pair right = min();
a[right.i] = value;
update(right.i, right.i + 1, - INF);
update(right.i + 1, N, value);
value++;
}
for (int i = 0; i < n; i++) {
System.out.print(a[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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
bool cur1;
int n;
int ans[200005];
long long S[200005];
struct Bittree {
long long sum[200005];
inline void update(int x, int v) {
while (x <= n) {
sum[x] += v;
x += x & (-x);
}
}
inline long long query(int x) {
long long res = 0;
while (x) {
res += sum[x];
x ^= x & (-x);
}
return res;
}
} bit;
inline void Rd(int &res) {
char c;
res = 0;
while (c = getchar(), c < 48)
;
do res = (res << 3) + (res << 1) + (c ^ 48);
while (c = getchar(), c > 47);
return;
}
long long ch[200005];
inline bool check(int x, long long y) {
long long h = ch[x - 1] - bit.query(x - 1);
return h <= y;
}
bool cur2;
int main() {
Rd(n);
for (int i = 1; i <= n; i++) ch[i] = ch[i - 1] + i;
for (int i = 1; i <= n; i++) scanf("%lld", &S[i]);
for (int i = n; i >= 1; i--) {
int l = 1, r = n;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid, S[i]))
ans[i] = mid, l = mid + 1;
else
r = mid - 1;
}
bit.update(ans[i], ans[i]);
}
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
puts("");
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 "
]
}
|
CORRECT
|
python2
|
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
mod=10**9+7
def ni():
return int(raw_input())
def li():
return map(int,raw_input().split())
def pn(n):
stdout.write(str(n)+'\n')
def pa(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
#main code
n=ni()
BITTree=[0]*(n+1)
def gs(i):
s = 0
i = i+1
while i > 0:
s += BITTree[i]
i -= i & (-i)
return s
def ub(i ,v):
i += 1
while i <= n:
BITTree[i] += v
i += i & (-i)
def fun(x):
ret=0
sm=0
for i in range(21,-1,-1):
pw=1<<i
if ret+pw<=n and sm+BITTree[ret+pw]<=x:
ret+=pw
sm+=BITTree[ret]
return ret
l=li()
for i in range(n):
ub(i,i)
ans=[0]*n
for i in range(n-1,-1,-1):
ans[i]=fun(l[i])
ub(ans[i],-ans[i])
pa(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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
using namespace std;
void redirectIO() {
ios::sync_with_stdio(false);
cin.tie(0);
}
long long mVal[1100000];
long long upd[1100000];
long long s[210000];
void refr(int a) { mVal[a] = mVal[a * 2] + mVal[a * 2 + 1] + upd[a]; }
void push(int a) {
mVal[a * 2] += upd[a];
upd[a * 2] += upd[a];
mVal[a * 2 + 1] += upd[a];
upd[a * 2 + 1] += upd[a];
upd[a] = 0;
}
void build(int a, int beg, int end) {
if (beg == end) {
mVal[a] = beg - 1;
return;
}
build(a * 2, beg, (beg + end) / 2);
build(a * 2 + 1, (beg + end) / 2 + 1, end);
refr(a);
}
int targ_end;
int targ_beg;
long long targ_val;
void update(int a, int beg, int end) {
if (beg > targ_end || end < targ_beg) return;
if (beg >= targ_beg && end <= targ_end) {
upd[a] += targ_val;
mVal[a] += targ_val;
return;
}
push(a);
update(a * 2, beg, (beg + end) / 2);
update(a * 2 + 1, (beg + end) / 2 + 1, end);
refr(a);
}
int findIndex(int a, int beg, int end) {
if (beg == end) {
return beg;
}
push(a);
if (mVal[a * 2] < targ_val) {
targ_val -= mVal[a * 2];
return findIndex(a * 2 + 1, (beg + end) / 2 + 1, end);
}
return findIndex(a * 2, beg, (beg + end) / 2);
}
int main() {
redirectIO();
int n;
cin >> n;
for (int i = 1; i <= (n); i++) cin >> s[i];
build(1, 1, n);
set<int> ss;
for (int i = 1; i <= (n); i++) ss.insert(i);
vector<int> answer;
for (int i = n; i > 0; --i) {
targ_val = s[i];
int b = findIndex(1, 1, n);
int a = *ss.lower_bound(b);
ss.erase(a);
answer.push_back(a);
targ_beg = a + 1;
targ_end = a + 1;
targ_val = -a;
update(1, 1, n);
}
reverse(answer.begin(), answer.end());
for (int x : answer) cout << x << " ";
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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long int N = 1e13 + 7;
long long int n;
vector<long long int> s(200004), p(200004), lazy(800004);
vector<long long int> tree(800004);
void build(long long int node, long long int l, long long int r) {
if (l == r) {
tree[node] = s[l];
return;
}
long long int mid = (l + r) / 2;
build(2 * node, l, mid);
build(2 * node + 1, mid + 1, r);
tree[node] = min(tree[2 * node], tree[2 * node + 1]);
}
void update(long long int node, long long int l, long long int r,
long long int st, long long int en, long long int val) {
if (l > en || r < st) {
return;
}
if (l >= st && r <= en) {
lazy[node] += val;
return;
}
long long int mid = (l + r) / 2;
update(2 * node, l, mid, st, en, val);
update(2 * node + 1, mid + 1, r, st, en, val);
tree[node] = min(tree[2 * node] + lazy[2 * node],
tree[2 * node + 1] + lazy[2 * node + 1]);
}
long long int query(long long int node, long long int l, long long int r,
long long int tot) {
if (l == r) {
return l;
}
long long int mid = (l + r) / 2;
tot += lazy[node];
if (tree[2 * node + 1] + tot + lazy[2 * node + 1] == 0) {
return query(2 * node + 1, mid + 1, r, tot);
}
return query(2 * node, l, mid, tot);
}
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) {
long long int u = query(1, 1, n, 0);
p[u] = i;
i++;
update(1, 1, n, u, u, N);
update(1, 1, n, u + 1, n, -i + 1);
}
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 "
]
}
|
CORRECT
|
java
|
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
public class P1208D7 {
public static void main(String[] args) throws IOException {
InputReader2 ir = new InputReader2();
PrintWriter pw = new PrintWriter(System.out);
int n = ir.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = ir.nextLong();
}
Fenwick f = new Fenwick(n);
for (int i = 1; i <= n; i++) {
f.add(i - 1, i);
}
int[] list = new int[n];
for (int i = n - 1; i >= 0; i--) {
int ind = f.indexWithGivenCumFreq(a[i]);
f.add(ind, -(ind + 1));
list[i] = ind + 1;
}
for (int i = 0; i < n; i++) {
pw.print(list[i] + " ");
}
pw.close();
}
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;
}
}
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();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
return ret;
}
private long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
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 "
]
}
|
CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.function.Function;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author anand.oza
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(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, InputReader in, PrintWriter out) {
int n = in.nextInt();
long[] s = in.readLongArray(n);
long sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
FullLongSegmentTree remaining = new FullLongSegmentTree(n, Long::sum, 0);
for (int i = 0; i < n; i++) {
remaining.update_LAZY(i, i + 1);
}
remaining.rebuild();
int[] answer = new int[n];
for (int i = n - 1; i >= 0; i--) {
int finalI = i;
answer[i] = remaining.lowerBound(value -> value > s[finalI]);
remaining.update(answer[i] - 1, 0);
}
out.println(Util.join(answer));
}
}
static class Util {
public static String join(int... i) {
StringBuilder sb = new StringBuilder();
for (int o : i) {
sb.append(o);
sb.append(" ");
}
if (sb.length() > 0) {
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}
}
static class FullLongSegmentTree extends LongSegmentTree {
public FullLongSegmentTree(int size, LongSegmentTree.Combiner combiner, int identityElement) {
super(1 << (32 - Integer.numberOfLeadingZeros(size)), combiner, identityElement);
}
public int lowerBound(Function<Long, Boolean> bigEnough) {
if (!bigEnough.apply(value[1]))
return -1;
if (bigEnough.apply(identityElement))
return 0;
int loc = 1;
long cur = identityElement;
for (; loc < size; ) {
long mid = combine(cur, value[2 * loc]);
if (bigEnough.apply(mid)) {
loc = 2 * loc;
} else {
cur = mid;
loc = 2 * loc + 1;
}
}
return loc - size + 1;
}
}
static class LongSegmentTree {
public int size;
public long[] value;
protected final LongSegmentTree.Combiner combiner;
protected final long identityElement;
public LongSegmentTree(int size, LongSegmentTree.Combiner combiner, long identityElement) {
this.size = size;
value = new long[2 * size];
Arrays.fill(value, identityElement);
this.combiner = combiner;
this.identityElement = identityElement;
}
protected long combine(long a, long b) {
return combiner.combine(a, b);
}
public void rebuild() {
for (int i = size - 1; i > 0; i--) {
value[i] = combine(value[2 * i], value[2 * i + 1]);
}
}
public void update(int i, long v) {
i += size;
value[i] = v;
while (i > 1) {
i /= 2;
value[i] = combine(value[2 * i], value[2 * i + 1]);
}
}
public void update_LAZY(int i, long v) {
i += size;
value[i] = v;
}
public interface Combiner {
long combine(long a, long b);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public long[] readLongArray(int n) {
long[] x = new long[n];
for (int i = 0; i < n; i++) {
x[i] = nextLong();
}
return x;
}
}
}
|
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 "
]
}
|
CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author sarthakmanna
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
FastWriter out = new FastWriter(outputStream);
DRestorePermutation solver = new DRestorePermutation();
solver.solve(1, in, out);
out.close();
}
static class DRestorePermutation {
int N;
long[] S;
SegmentTree segT;
public void solve(int testNumber, FastReader in, FastWriter out) {
int i, j, k;
N = in.nextInt();
S = new long[N];
for (i = 0; i < N; ++i) S[i] = in.nextLong();
segT = new SegmentTree(N + 7);
long[] A = new long[N];
for (i = N - 1; i >= 0; --i) {
A[i] = calc(i);
segT.update((int) A[i], A[i]);
//System.err.println(Arrays.toString(A));
}
for (long itr : A) out.printsp(itr);
out.println();
out.flush();
}
long calc(int ind) {
long sum = S[ind];
int L = 1, R = N;
while (true) {
//System.err.println(L + " " + R);
int mid = L + R >> 1;
if (L + 1 >= R) {
if (sum + segT.query(0, R) == nsum(R)) return R;
else if (sum + segT.query(0, L) == nsum(L)) return L;
else return 7 / 0;
} else if (sum + segT.query(0, mid) >= nsum(mid)) L = mid;
else R = mid;
}
}
long nsum(long v) {
return v * (v - 1) >> 1;
}
}
static class FastWriter {
static private BufferedWriter bw;
public FastWriter(OutputStream os) {
bw = new BufferedWriter(new OutputStreamWriter(os));
}
public FastWriter(Writer w) {
bw = new BufferedWriter(w);
}
public void print(Object a) {
try {
bw.write(a.toString());
} catch (Exception e) {
}
}
public void println() {
print("\n");
}
public void printsp(Object a) {
print(a);
print(" ");
}
public void flush() {
try {
bw.flush();
} catch (Exception e) {
}
}
public void close() {
try {
bw.close();
} catch (Exception e) {
}
}
}
static class FastReader {
static private byte[] buf = new byte[2048];
static private int index;
static private int total;
static private InputStream in;
public FastReader(InputStream is) {
try {
in = is;
} catch (Exception e) {
}
}
private int scan() {
try {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0)
return -1;
}
return buf[index++];
} catch (Exception e) {
return 7 / 0;
}
}
public int nextInt() {
int c, val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+')
c = scan();
for (; c >= '0' && c <= '9'; c = scan())
val = (val << 3) + (val << 1) + (c & 15);
return neg ? -val : val;
}
public long nextLong() {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+')
c = scan();
for (; c >= '0' && c <= '9'; c = scan())
val = (val << 3) + (val << 1) + (c & 15);
return neg ? -val : val;
}
}
static class SegmentTree {
int N;
long[] tree;
SegmentTree(int n) {
N = 1;
while (N < n) N <<= 1;
tree = new long[N * 2 - 1];
}
void update(int i, long v) {
i += N - 1;
while (i >= 0) {
tree[i] += v;
i = i - 1 >> 1;
}
}
long query(int i, int l, int r, int ql, int qr) {
int mid = l + r >> 1, i2 = i << 1;
if (l > qr || r < ql) return 0;
else if (l >= ql && r <= qr) return tree[i];
else return query(i2 + 1, l, mid, ql, qr) + query(i2 + 2, mid + 1, r, ql, qr);
}
long query(int l, int r) {
if (l >= r) return 0;
return query(0, 0, N - 1, l, r - 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 "
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {new Main().run();}
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
void run(){
work();
out.flush();
}
long mod=998244353L;
int n;
long[] tree;
void work() {
n=in.nextInt();
long[] Q=new long[n];
for(int i=0;i<n;i++) Q[i]=in.nextLong();
long[] ret=new long[n];
tree=new long[4*n];
build(1,n,1);
for(int i=n-1;i>=0;i--) {
long num=query(1,n,1,0,Q[i]);
ret[i]=num;
}
for(int i=0;i<n;i++) {
out.print(ret[i]+" ");
}
}
private long query(long l, long r,int index,long sum,long v) {
// TODO Auto-generated method stub
long m=(l+r)/2;
if(l==r) {
tree[index]=0;
return l;
}
if(sum+tree[index<<1]<=v) {
long ret=query(m+1,r,(index<<1)+1,sum+tree[index<<1],v);
tree[index]-=ret;
return ret;
}else {
long ret=query(l,m,index<<1,sum,v);
tree[index]-=ret;
return ret;
}
}
private long build(int l, int r,int index) {
if(l==r) {
tree[index]=l;
return l;
}
int m=(l+r)/2;
tree[index]=build(l,m,index<<1)+build(m+1,r,(index<<1)+1);
return tree[index];
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
public String next()
{
if(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 "
]
}
|
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, long long 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);
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 "
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.math.*;
import java.io.*;
public class CF1208D {
class BIT {
int n; long table[];
public BIT(int nn) { table = new long[n = nn + 10]; }
void update(int i, long delta) {
while(i < n) {
table[i] += delta;
i += i & -i;
}
}
long sum(int i) {
long sum = 0;
while(i > 0) {
sum += table[i];
i -= i & -i;
}
return sum;
}
long rangeSum(int i, int j) {
return sum(j) - sum(i - 1);
}
int next(long target) {
long sum = 0;
int res = 0;
for(int bit = 20 ; bit >= 0 ; bit--) {
int power = 1 << bit;
if(res + power >= n) continue;
if(sum + table[res + power] <= target) {
sum += table[res + power];
res += power;
}
}
return res;
}
}
public CF1208D() {
FS scan = new FS();
PrintWriter out = new PrintWriter(System.out);
int n = scan.nextInt();
long[] p = new long[n];
for(int i = 0 ; i < n ; i++) p[i] = scan.nextLong();
BIT bit = new BIT(n);
for(int i = 1 ; i <= n ; i++) bit.update(i, i);
int[] res = new int[n];
for(int i = n - 1 ; i >= 0 ; i--) {
int next = bit.next(p[i]) + 1;
res[i] = next;
bit.update(next, -next);
}
for(int i = 0 ; i < n ; i++) out.print(res[i] + " ");
out.println();
out.close();
}
class FS {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() {
while(!st.hasMoreTokens()) {
try { st = new StringTokenizer(br.readLine()); }
catch(Exception e) { e.printStackTrace(); }
}
return st.nextToken();
}
public int nextInt() { return Integer.parseInt(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
public static void main(String[] args) { new CF1208D(); }
}
|
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 "
]
}
|
CORRECT
|
python3
|
from sys import stdin,stdout
def update(L,C,l,r,rt):
if l==r:
tree[rt]+=C
return
mid=(l+r)//2
if L<=mid:
update(L,C,l,mid,rt<<1)
else:
update(L,C,mid+1,r,rt<<1|1)
tree[rt]=tree[rt<<1]+tree[rt<<1|1]
def query(s,l,r,rt):
if l==r:
return l
mid=(l+r)//2
if tree[rt<<1]>s:
return query(s,l,mid,rt<<1)
else:
return query(s-tree[rt<<1],mid+1,r,rt<<1|1)
if __name__ == '__main__':
tree=[0]*800005
n=int(stdin.readline())
a=list(map(int,stdin.readline().split()))
for i in range(n):
update(i+1,i+1,1,n,1)
b=[0]*200005
for i in range(n,0,-1):
b[i]=query(a[i-1],1,n,1)
update(b[i],-b[i],1,n,1)
for i in range(n):
stdout.write('%d '%(b[i+1]))
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
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.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Mufaddal Naya
*/
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);
DRestorePermutation solver = new DRestorePermutation();
solver.solve(1, in, out);
out.close();
}
static class DRestorePermutation {
public void solve(int testNumber, InputReader c, OutputWriter w) {
int n = c.readInt();
long a[] = c.readLongArray(n);
FenwickTree ft = new FenwickTree(n + 1);
for (int i = 0; i < n; i++) {
ft.add(i + 1, i + 1);
}
int res[] = new int[n];
for (int i = n - 1; i >= 0; i--) {
res[i] = upperBound(ft, n, a[i]);
}
w.printLine(res);
}
public static int upperBound(FenwickTree ft, int length, long value) {
int low = 0;
int high = length;
int res = length;
while (low < high) {
final int mid = (low + high) >> 1;
long v = ft.get(mid);
//System.out.println(low+" "+high+" "+mid+" "+v);
if (value >= v) {
res = mid;
low = mid + 1;
} else {
high = mid;
}
}
ft.add(res + 1, -res - 1);
return res + 1;
}
}
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 long[] readLongArray(int size) {
long[] array = new long[size];
for (int i = 0; i < size; i++) {
array[i] = readLong();
}
return array;
}
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 readInt() {
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 readLong() {
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 interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class FenwickTree {
private final long[] value;
public FenwickTree(int size) {
value = new long[size];
}
public long get(int to) {
to = Math.min(to, value.length - 1);
long result = 0;
while (to >= 0) {
result += value[to];
to = (to & (to + 1)) - 1;
}
return result;
}
public void add(int at, long value) {
while (at < this.value.length) {
this.value[at] += value;
at = at | (at + 1);
}
}
}
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(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void printLine(int[] array) {
print(array);
writer.println();
}
public void close() {
writer.close();
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
long long int min2(long long int a, long long int b) { return a > b ? a : b; }
long long int max2(long long int a, long long int b) { return a < b ? a : b; }
class node {
public:
int left, right;
long long int num, min;
bool isMinLeft;
bool willPropagate;
node *leftchild, *rightchild, *parent;
node(int left, int right, node *parent) {
this->left = left;
this->right = right;
this->min = this->num = 0;
this->isMinLeft = false;
this->parent = parent;
this->willPropagate = false;
if (left == right) {
this->leftchild = NULL;
this->rightchild = NULL;
} else {
int mid = (this->left + this->right) / 2;
this->leftchild = new node(this->left, mid, this);
this->rightchild = new node(mid + 1, this->right, this);
}
}
bool isLeaf() { return this->leftchild == NULL && this->rightchild == NULL; }
bool isIn(int index) { return (this->left <= index && index <= this->right); }
int updateDirection(int lbound, int rbound) {
if (this->left == lbound && this->right == rbound)
return 0;
else if (this->isLeaf() || lbound > rbound)
return -1;
else if (this->leftchild->isIn(lbound) && this->leftchild->isIn(rbound))
return 1;
else if (this->rightchild->isIn(lbound) && this->rightchild->isIn(rbound))
return 2;
else if (this->leftchild->isIn(lbound) && this->rightchild->isIn(rbound))
return 3;
else
return -1;
}
long long int parent_num_sum() {
node *ancestor = this;
long long int ans = 0;
while (ancestor != NULL) {
ans += ancestor->num;
ancestor = ancestor->parent;
}
return ans;
}
void singleUpdate() {
if (this->isLeaf()) {
this->min = this->num;
this->isMinLeft = true;
} else {
isMinLeft = true;
isMinLeft = (this->leftchild->min < this->rightchild->min);
this->min = num;
if (isMinLeft)
this->min += this->leftchild->min;
else
this->min += this->rightchild->min;
}
}
void upperPropagation() {
if (!this->willPropagate)
return;
else if (!this->isLeaf() && (this->leftchild->willPropagate ||
this->rightchild->willPropagate))
return;
else {
this->willPropagate = false;
this->singleUpdate();
if (this->parent != NULL) this->parent->upperPropagation();
}
}
void update(long long int val, int lbound, int rbound) {
int dir = this->updateDirection(lbound, rbound);
if (dir == -1) return;
this->willPropagate = true;
if (dir == 0) {
this->num += val;
this->upperPropagation();
} else if (dir == 1) {
this->leftchild->willPropagate = true;
this->leftchild->update(val, lbound, rbound);
} else if (dir == 2) {
this->rightchild->willPropagate = true;
this->rightchild->update(val, lbound, rbound);
} else if (dir == 3) {
this->leftchild->willPropagate = true;
this->rightchild->willPropagate = true;
this->leftchild->update(val, lbound, this->leftchild->right);
this->rightchild->update(val, this->rightchild->left, rbound);
}
}
long long int search(int lbound, int rbound) {
int dir = this->updateDirection(lbound, rbound);
if (dir == -1)
throw "Invalid range";
else if (dir == 0) {
return min;
} else if (dir == 1)
return this->leftchild->search(lbound, rbound) + num;
else if (dir == 2)
return this->rightchild->search(lbound, rbound) + num;
else if (dir == 3) {
if (this->isMinLeft)
return leftchild->search(lbound, leftchild->right) + num;
else
return rightchild->search(rightchild->left, rbound) + num;
}
}
int findMinimumIndex() {
if (this->isLeaf())
return this->left;
else if (this->isMinLeft)
return leftchild->findMinimumIndex();
else
return rightchild->findMinimumIndex();
}
void print(int tabLevel) {
for (int i = 0; i < tabLevel - 1; i++) printf("| ");
if (tabLevel > 0) printf("+-- ");
printf("Node covering [%d, %d]: num %lld, min %lld, isLeft %c\n",
this->left, this->right, this->num, this->min,
isMinLeft ? 'L' : 'R');
if (!this->isLeaf()) {
this->leftchild->print(tabLevel + 1);
this->rightchild->print(tabLevel + 1);
}
}
};
int main(void) {
int n;
scanf("%d", &n);
std::vector<long long int> sum(n, 0);
node *root = new node(0, n - 1, NULL);
for (int i = 0; i < n; i++) {
scanf("%lld", &sum[i]);
root->update(sum[i], i, i);
}
std::vector<int> perm(n, -1);
for (int num = 1; num <= n; num++) {
int rightmost = root->findMinimumIndex();
root->update(-num, rightmost, n - 1);
root->update(1LL << 40, rightmost, rightmost);
perm[rightmost] = num;
}
for (int i = 0; i < n; i++) printf("%d ", perm[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 "
]
}
|
CORRECT
|
java
|
//package manthan19;
import java.io.*;
import java.util.*;
public class D {
static TreeSet<Long> avail = new TreeSet<Long>();
static long[] segtree;
static int n;
static long[] a;
public static void main(String[] args) {
MyScanner sc = new MyScanner();
n = sc.nextInt();
a = new long[n];
for(int i = 0; i < n; i++)
a[i] = (long)i + 1;
segtree = new long[4 * n];
getSegtree(1, 0, n - 1);
for(long i = 1; i <= n; i++)
avail.add(i);
long[] s = new long[n];
for(int i = 0; i < n; i++)
s[i] = sc.nextLong();
long[] p = new long[n];
for(int i = n - 1; i >= 0; i--) {
p[i] = avail.ceiling(desum(s[i], 1, n));
avail.remove(p[i]);
update(1, 0, n - 1, (int)p[i] - 1, -p[i]);
}
for(long i : p)
out.print(i + " ");
out.println();
out.close();
}
public static long desum(long s, long l, long r) {
if(r == l) return l;
long m = (l + r) / 2;
long sum = query(1, 0, n - 1, 0, (int)m - 2);
if(sum < s)
return desum(s, m + 1, r);
return desum(s, l, m);
}
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static class MyScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
String next() {
while (st == null || !st.hasMoreElements())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static long update(int i, int l, int r, int q, long x) {
if(q < l || q > r)
return segtree[i];
if(q == l && q == r)
return segtree[i] = segtree[i] + x;
int m = (l + r) / 2;
return segtree[i] = update(2 * i, l, m, q, x) + update(2 * i + 1, m + 1, r, q, x);
}
public static long query(int i, int l, int r, int qL, int qR) {
if(qR < l || qL > r)
return 0;
if(qL <= l && qR >= r)
return segtree[i];
int m = (l + r) / 2;
return query(2 * i, l, m, qL, qR) + query(2 * i + 1, m + 1, r, qL, qR);
}
public static long getSegtree(int i, int l, int r) {
if(l == r)
return segtree[i] = a[l];
int m = (l + r) / 2;
return segtree[i] = getSegtree(2 * i, l, m) + getSegtree(2 * i + 1, m + 1, r);
}
}
|
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 "
]
}
|
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 build(int n, int s, int e) {
if (s + 1 == e) {
seg[n] = A[s];
} else {
int m = (s + e) >> 1;
build(n * 2, s, m);
build(n * 2 + 1, m, e);
seg[n] = min(seg[n * 2], seg[n * 2 + 1]);
}
}
void add_lazy(int n, long long x) {
lazy[n] += x;
seg[n] += x;
}
void push(int n, int s, int e) {
if (s + 1 == e) return;
add_lazy(n * 2, lazy[n]);
add_lazy(n * 2 + 1, 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, x);
else {
if (lazy[n]) push(n, s, e);
int m = (s + e) >> 1;
update(n * 2, s, m, a, b, x);
update(n * 2 + 1, m, e, a, 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];
build(1, 0, n);
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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = ~0ull >> 1;
long long _min[2000005 * 5], add[2000005 * 5];
int n;
long long s[2000005];
int ans[2000005];
void pushup(int o) { _min[o] = min(_min[o << 1], _min[o << 1 | 1]); }
void pushdown(int o) {
if (add[o]) {
add[o << 1] += add[o];
add[o << 1 | 1] += add[o];
_min[o << 1] += add[o];
_min[o << 1 | 1] += add[o];
add[o] = 0;
}
}
void build(int o, int l, int r) {
if (l == r) {
_min[o] = s[l];
return;
}
int m = (l + r) >> 1;
build(o << 1, l, m);
build(o << 1 | 1, m + 1, r);
pushup(o);
}
void update(int o, int l, int r, int ql, int qr, long long v) {
if (ql <= l && qr >= r) {
add[o] += v;
_min[o] += v;
return;
}
pushdown(o);
int m = (l + r) >> 1;
if (ql <= m && qr >= l) update(o << 1, l, m, ql, qr, v);
if (ql <= r && qr >= m + 1) update(o << 1 | 1, m + 1, r, ql, qr, v);
pushup(o);
}
int query(int o, int l, int r, long long v) {
if (l == r) {
return l;
}
pushdown(o);
int m = (l + r) >> 1;
if (_min[o << 1 | 1] == v)
return query(o << 1 | 1, m + 1, r, v);
else
return query(o << 1, l, m, v);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
build(1, 1, n);
for (int i = 0; i < n; i++) {
long long v = _min[1];
int pos = query(1, 1, n, v);
ans[pos] = i + 1;
update(1, 1, n, pos, pos, INF);
if (pos < n) update(1, 1, n, pos + 1, n, -1 * (i + 1));
}
for (int i = 1; i <= n; i++) {
if (i != 1) cout << " ";
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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 10;
const long long INF = 0x3f3f3f3f3f3f3f3f;
long long n, tag[N << 2], ans[N], pos;
long long a[N], val[N << 2];
inline void pushup(long long o) { val[o] = min(val[o << 1], val[o << 1 | 1]); }
inline void apply(long long o, long long x) { val[o] += x, tag[o] += x; }
inline void pushdown(long long o) {
if (tag[o]) {
apply(o << 1, tag[o]);
apply(o << 1 | 1, tag[o]);
tag[o] = 0;
}
}
inline void build(long long o, long long l, long long r) {
if (l == r) {
val[o] = a[l];
return;
}
long long mid = (l + r) >> 1;
build(o << 1, l, mid), build(o << 1 | 1, mid + 1, r);
pushup(o);
}
inline void change(long long o, long long l, long long r, long long x,
long long y, long long z) {
if (x <= l && r <= y) {
apply(o, z);
return;
}
long long mid = (l + r) >> 1;
pushdown(o);
if (x <= mid) change(o << 1, l, mid, x, y, z);
if (y > mid) change(o << 1 | 1, mid + 1, r, x, y, z);
pushup(o);
}
inline long long last_zero(long long o, long long l, long long r) {
if (l == r) return l;
pushdown(o);
long long mid = (l + r) >> 1;
if (val[o << 1 | 1] == 0)
return last_zero(o << 1 | 1, mid + 1, r);
else
return last_zero(o << 1, l, mid);
}
inline long long query_min(long long o, long long l, long long r, long long x,
long long y) {
if (l == r) return val[o];
pushdown(o);
long long mid = (l + r) >> 1;
long long res = INF;
if (x <= mid)
res = min(res, query_min(o << 1, l, mid, x, y));
else if (y > mid)
res = min(res, query_min(o << 1 | 1, mid + 1, r, x, y));
return res;
}
signed main() {
cin >> n;
for (register long long i = (1); i <= (n); ++i) cin >> a[i];
memset(val, INF, sizeof(val));
build(1, 1, n);
for (register long long i = (1); i <= (n); ++i) {
pos = last_zero(1, 1, n);
if (pos == 1) cerr << i << ' ' << pos << endl;
ans[pos] = i;
change(1, 1, n, pos, pos, INF);
change(1, 1, n, pos, n, -1ll * i);
}
for (register long long 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 "
]
}
|
CORRECT
|
java
|
/**
* @author derrick20
*/
import java.io.*;
import java.util.*;
public class RestorePermutations {
public static void main(String args[]) throws Exception {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int N = sc.nextInt();
long[] sums = new long[N];
for (int i = 0; i < sums.length; i++) {
sums[i] = sc.nextLong();
}
// available = new TreeSet<>();
int[] arr = new int[N + 1];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
// if (i > 0) {
// available.add(i);
// }
}
bit = new BIT(arr);
// used = new HashSet<>();
int[] seq = new int[N];
for (int i = N - 1; i >= 0; i--) {
// For some reason it's crucial to binary search, finding the highest
// number to give this sum
int val = binarySearch(1, N, sums[i]);
// if (available.higher(val - 1) != null) {
// int temp = available.higher(val - 1);
// if (bit.prefixSum(temp - 1) == sums[i]) {
// val = temp;
// }
// }
// if (available.lower(val + 1) != null) {
// int temp = available.lower(val + 1) ;
// if (bit.prefixSum(temp - 1) == sums[i]) {
// val = temp;
// }
// }
seq[i] = val;
bit.add(val, -val);
// System.out.println(bit);
// available.remove(val);
}
for (int i = 0; i < seq.length; i++) {
out.print(seq[i] + " ");
}
out.close();
}
static BIT bit;
// static HashSet<Integer> used;
// static TreeSet<Integer> available;
// Find the number such that the sum of things below it in the
// current sequence state equals the goal sum
// 0001111 Let's find the lowest one that's >= it. That's the same thing
// ISSUE: if we find the lowest thing that can still produce the sum,
// there will be duplicates in the case of a sum of 0. Otherwise, each
// value should yield a unique prefix sum. However,
// if we instead climb upwards to find the highest thing that is under
// the sum. That works because a series of 0's will be in decreasing order
// (increasing from right to left, so that' why climbing up makes sense)
// Instead, let's try finding the highest thing that's <= it
// 1100
static int binarySearch(int lo, int hi, long sum) {
while (lo < hi) {
int mid = (lo + hi + 1) / 2; // go towards the 0's, so we crawl down
// If it's not used, then we need to go up.
if (bit.prefixSum(mid - 1) <= sum) {
lo = mid;
}
else {
hi = mid - 1;
}
}
return lo;
}
static class BIT {
int[] arr;
long[] tree;
// todo ***This ASSUMES THAT ARR IS 1-INDEXED ALREADY***
public BIT(int[] arr) {
this.arr = arr;
this.tree = new long[arr.length];
// copy arr values into tree
for (int i = 1; i < tree.length; i++) {
tree[i] = arr[i];
}
constructBIT(arr, tree);
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Index: ");
for (int i = 1; i < tree.length; i++) {
sb.append(i + " ");
}
sb.append("\nValue: ");
for (int i = 1; i < tree.length; i++) {
sb.append(this.sum(i, i) + " ");
}
return sb.toString();
}
public int leastSignificantBit(int x) {
// by negating it, all
return x & (-x);
}
public void constructBIT(int[] arr, long[] tree) {
// propagate information up to the pieces above it that would be responsible for it
for (int i = 1; i < tree.length; i++) {
int j = i + leastSignificantBit(i);
// all of it's "parents" will need to possess its values,
// but, we can be clever and only propagate to its immediate parent.
// Since we are processing in order, that parent will propagate to its parent
// eventually, so we are fine. Add methods are log(N) because we can't rely on our
// parents eventually doing work for us.
if (j < arr.length) {
tree[j] += tree[i];
}
}
}
// return the sum
public long sum(int i, int j) {
return prefixSum(j) - prefixSum(i - 1);
// exclude the values under i
}
// returns sum from 1 to i of the array
// propagate downward! (decomposing the sum)
public long prefixSum(int i) {
long sum = 0;
while (i > 0) {
sum += tree[i];
i -= leastSignificantBit(i);
}
return sum;
}
// add a value of val at the ith value
// propagate upward!
public void add(int i, int val) {
while (i < tree.length) {
tree[i] += val;
i += leastSignificantBit(i);
}
}
}
static class FastScanner {
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double cnt = 1;
BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
cnt=1;
boolean neg = false;
if(c==NC)c=getChar();
for(;(c<'0' || c>'9'); c = getChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=getChar()) {
res = (res<<3)+(res<<1)+c-'0';
cnt*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/cnt;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=getChar();
while(c>32) {
res.append(c);
c=getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=getChar();
while(c!='\n') {
res.append(c);
c=getChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=getChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class C {
public static void main(String[] args) {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(System.out);
Task solver = new Task();
int t = 1;
while(t-->0) solver.solve(1, scan, out);
out.close();
}
static class Task {
static long[] a;
static long INF = Long.MAX_VALUE/5;
public void solve(int testNumber, FastReader scan, PrintWriter out) {
int n = scan.nextInt();
a = new long[n];
for(int i = 0; i < n; i++) a[i] = scan.nextLong();
SegmentTree tree = new SegmentTree(0, n-1);
int[] res = new int[n];
for(int i = 1; i <= n; i++) {
int get = tree.walk();
res[get] = i;
if(get != n-1) tree.update(get+1, n-1, -i);
tree.update(get, get, INF);
}
for(int i : res) out.print(i + " ");
}
static class SegmentTree {
int s, e;
long modify;
long minVal;
SegmentTree left, right;
public SegmentTree(int ss, int ee) {
s = ss;
e = ee;
modify = 0;
if(s == e) {
minVal = a[s];
left = null;
right = null;
}
else {
int mid = (s+e)/2;
left = new SegmentTree(s, mid);
right = new SegmentTree(mid+1, e);
minVal = Math.min(left.minVal, right.minVal);
}
}
public void update(int start, int end, long val) {
push(modify);
if(s > end || e < start) return;
if(s >= start && e <= end) {
push(val);
return;
}
if(s != e) {
left.update(start, end, val);
right.update(start, end, val);
minVal = Math.min(left.minVal, right.minVal);
}
}
public int walk() {
push(modify);
if(s == e) return s;
if(right.minVal + right.modify == 0) {
return right.walk();
}
if(left.minVal + left.modify == 0) {
return left.walk();
}
return -1;
}
public void push(long val) {
minVal += val;
if(s != e) {
left.modify += val;
right.modify += val;
}
modify = 0;
}
}
}
static void shuffle(int[] a) {
Random get = new Random();
for(int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(long[] a) {
Random get = new Random();
for(int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long a[3000000], tree[4 * 3000000], ans[4 * 3000000];
int n;
void update(int indx, int left, int right, int pos, long long val) {
if (left > pos || right < pos) return;
if (left == right) {
tree[indx] += val;
return;
}
int mid = (left + right) >> 1;
update(indx * 2, left, mid, pos, val);
update(indx * 2 + 1, mid + 1, right, pos, val);
tree[indx] = tree[indx * 2] + tree[indx * 2 + 1];
}
long long query(int indx, int l, int r, int left, int right) {
if (left > right || l > right || r < left) return 0;
if (l >= left && r <= right) return tree[indx];
int mid = (l + r) >> 1;
long long x = query(indx * 2, l, mid, left, right);
long long y = query(indx * 2 + 1, mid + 1, r, left, right);
return x + y;
}
int doit(long long val) {
int low = 1, high = n, ans = 1;
while (low <= high) {
int mid = (low + high) >> 1;
long long xx = query(1, 1, n, 1, mid - 1);
if (xx <= val) {
low = mid + 1;
ans = mid;
} else
high = mid - 1;
}
return ans;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout.setf(ios::fixed);
cout.precision(20);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) update(1, 1, n, i, i);
for (int i = n; i > 0; i--) {
ans[i] = doit(a[i]);
update(1, 1, n, ans[i], -1ll * ans[i]);
}
for (int i = 1; i <= n; i++) cout << ans[i] << " ";
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long m) {
a %= m;
long long ret = 1;
while (b) {
if (b & 1) ret = ((ret % m) * (a % m)) % m;
a = ((a % m) * (a % m)) % m;
b >>= 1;
}
return ret;
}
const int N = 2e5 + 5;
long long n, bit[N + 3], a[N + 2], ans[N + 2];
void update(long long val, int x) {
for (; x <= N; x += x & (-x)) {
bit[x] += val;
}
}
long long query(int x) {
int tm = x;
long long ret = 0;
while (x > 0) {
ret += bit[x];
x -= x & (-x);
}
ret += ans[tm - 1];
return ret;
}
void pre() {
long long now = 0;
ans[0] = 0;
for (long long i = 1; i < N; i++) {
now += i;
ans[i] = now;
}
}
int main() {
pre();
cin >> n;
vector<long long> vec;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = n - 1; i >= 0; i--) {
int l = 1, r = n + 1, mid;
while ((r - l) > 1) {
mid = (l + r) / 2;
long long result = query(mid);
if (result <= a[i]) {
l = mid;
} else {
r = mid;
}
}
if (query(l + 1) == a[i]) {
vec.push_back(l + 1);
update(-(l + 1), l + 2);
} else {
vec.push_back(l);
update(-(l), l + 1);
}
}
reverse(vec.begin(), vec.end());
for (auto it : vec) {
cout << it << " ";
}
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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
const long long inf = 0x3f3f3f3f;
const long long inn = 0x80808080;
using namespace std;
const long long maxm = 2e5 + 5;
long long mi[maxm << 2];
long long laz[maxm << 2];
long long p[maxm];
long long n;
void pushup(long long node) { mi[node] = min(mi[node * 2], mi[node * 2 + 1]); }
void pushdown(long long node) {
if (laz[node]) {
mi[node * 2] += laz[node];
mi[node * 2 + 1] += laz[node];
laz[node * 2] += laz[node];
laz[node * 2 + 1] += laz[node];
laz[node] = 0;
}
}
void build(long long l, long long r, long long node) {
if (l == r) {
cin >> mi[node];
return;
}
long long mid = (l + r) / 2;
build(l, mid, node * 2);
build(mid + 1, r, node * 2 + 1);
pushup(node);
}
long long ask(long long x, long long l, long long r, long long node) {
if (l == r) {
mi[node] = (1LL << 60);
return l;
}
long long mid = (l + r) / 2;
pushdown(node);
long long ans = 1;
if (mi[node * 2 + 1] <= x)
ans = ask(x, mid + 1, r, node * 2 + 1);
else
ans = ask(x, l, mid, node * 2);
pushup(node);
return ans;
}
void update(long long st, long long ed, long long val, long long l, long long r,
long long node) {
if (st <= l && ed >= r) {
mi[node] += val;
laz[node] += val;
return;
}
pushdown(node);
long long mid = (l + r) / 2;
if (st <= mid) update(st, ed, val, l, mid, node * 2);
if (ed >= mid) update(st, ed, val, mid + 1, r, node * 2 + 1);
pushup(node);
}
signed main() {
ios::sync_with_stdio(0);
cin >> n;
build(1, n, 1);
for (long long i = 1; i <= n; i++) {
long long x = ask(0, 1, n, 1);
p[x] = i;
if (x != n) {
update(x + 1, n, -i, 1, n, 1);
}
}
for (long long 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 "
]
}
|
CORRECT
|
java
|
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
public class P1208D6 {
public static void main(String[] args) throws IOException {
InputReader2 ir = new InputReader2();
PrintWriter pw = new PrintWriter(System.out);
int n = ir.nextInt();
ArrayDeque<Long> dq = new ArrayDeque<>(n);
for (int i = 0; i < n; i++) {
dq.addLast(ir.nextLong());
}
BIT tree = new BIT(n);
for (int i = 1; i <= n; i++) {
tree.add(i, i);
}
ArrayDeque<Integer> ans = new ArrayDeque<>(n);
for (int i = n - 1; i >= 0; i--) {
long cur = dq.removeLast();
if (cur == 0) {
ans.addLast(binSearch(tree, 0, n));
} else {
ans.addLast(binSearch(tree, cur, n));
}
tree.remove(ans.getLast());
}
StringBuilder sb = new StringBuilder(3 * n);
while (!ans.isEmpty()) {
sb.append(ans.removeLast()).append(" ");
}
pw.print(sb);
pw.close();
}
private static int binSearch(BIT tree, long 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();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
return ret;
}
private long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long a, b, c, d, i, e, f, g, n, m, k, l, A[200005], B[200005], mid, le, ri,
BITree[4000005], ans, fix[200005];
long long getSum(long long index) {
long long sum = 0;
while (index > 0) {
sum += BITree[index];
index -= index & (-index);
}
return sum;
}
void updateBIT(long long index, long long val) {
while (index <= 1000005) {
BITree[index] += val;
index += index & (-index);
}
}
int main() {
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> A[i];
updateBIT(i, i);
}
for (long long i = n; i >= 1; i--) {
le = 0;
ri = n;
while (le <= ri) {
mid = (le + ri) / 2;
a = getSum(mid);
if (a > A[i]) {
ri = mid - 1;
} else if (a <= A[i]) {
ans = mid;
le = mid + 1;
}
}
updateBIT(ans + 1, -(ans + 1));
B[i] = ans + 1;
fix[ans + 1] = 1;
}
for (long long 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 "
]
}
|
CORRECT
|
python3
|
from sys import stdin,stdout
class Tree(object):
def __init__(self,n):
self.tree=[0]*(4*n+10)
def update(self,L,C,l,r,rt):
if l==r:
self.tree[rt]+=C
return
mid=(l+r)//2
if L<=mid:
self.update(L,C,l,mid,rt<<1)
else:
self.update(L,C,mid+1,r,rt<<1|1)
self.tree[rt]=self.tree[rt<<1]+self.tree[rt<<1|1]
def query(self,s,l,r,rt):
if l==r:
return l
mid=(l+r)//2
if self.tree[rt<<1]>s:
return self.query(s,l,mid,rt<<1)
else:
return self.query(s-self.tree[rt<<1],mid+1,r,rt<<1|1)
if __name__ == '__main__':
n=int(stdin.readline())
a=list(map(int,stdin.readline().split()))
b=[0]*(n+10)
seg=Tree(n)
for i in range(n):
seg.update(i+1,i+1,1,n,1)
for i in range(n,0,-1):
b[i]=seg.query(a[i-1],1,n,1)
seg.update(b[i],-b[i],1,n,1)
for i in range(n):
stdout.write('%d '%(b[i+1]))
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
CORRECT
|
java
|
import java.util.Scanner;
import java.util.StringJoiner;
public class RestorePermutation {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long[] s = new long[n];
for (int i = 0; i < n; i++) {
s[i] = in.nextLong();
}
SegmentTree segTree = new SegmentTree(0, n);
for (int i = 1; i <= n; i++) {
segTree.update(i, i);
}
int[] res = new int[n];
for (int i = n - 1; i >= 0; i--) {
int lower = 0;
int upper = n - 1;
while (lower < upper) {
int mid = (lower + upper + 1) / 2;
if (segTree.query(0, mid) <= s[i]) {
lower = mid;
} else {
upper = mid - 1;
}
}
res[i] = upper + 1;
segTree.update(res[i], -res[i]);
}
StringJoiner joiner = new StringJoiner(" ");
for (int i = 0; i < n; i++) {
joiner.add(res[i] + "");
}
System.out.println(joiner);
}
static class SegmentTree {
final long[] val;
final int treeFrom;
final int length;
SegmentTree(int treeFrom, int treeTo) {
this.treeFrom = treeFrom;
int length = treeTo - treeFrom + 1;
int l;
for (l = 0; (1 << l) < length; l++);
val = new long[1 << (l + 1)];
this.length = 1 << l;
}
void update(int index, long delta) {
int node = index - treeFrom + length;
val[node] += delta;
for (node >>= 1; node > 0; node >>= 1) {
val[node] = val[node << 1] + val[(node << 1) + 1];
}
}
long query(int from, int to) {
if (to < from) {
return 0;
}
from += length - treeFrom;
to += length - treeFrom + 1;
long res = 0;
for (; from + (from & -from) <= to; from += from & -from) {
res += val[from / (from & -from)];
}
for (; to - (to & -to) >= from; to -= to & -to) {
res += val[(to - (to & -to)) / (to & -to)];
}
return res;
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
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();
long[] s = new long[n];
for (int i = 0; i < n; i++) {
s[i] = ir.nextLong();
}
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--) {
long 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, long 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();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
return ret;
}
private long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> tree[4 * 200005 + 1];
long long lo[4 * 200005 + 1], hi[4 * 200005 + 1], a[200005],
delta[4 * 200005 + 1], ans[200005];
void init(long long i, long long a, long long b) {
lo[i] = a;
hi[i] = b;
if (a == b) return;
long long m = (a + b) / 2;
init(2 * i, a, m);
init(2 * i + 1, m + 1, b);
}
void merge(long long i0, long long i1, long long i2) {
if (tree[i1].first + delta[i1] == tree[i2].first + delta[i2])
tree[i0] = {tree[i1].first + delta[i1],
max(tree[i1].second, tree[i2].second)};
else if (tree[i1].first + delta[i1] < tree[i2].first + delta[i2])
tree[i0] = {tree[i1].first + delta[i1], tree[i1].second};
else
tree[i0] = {tree[i2].first + delta[i2], tree[i2].second};
}
void build(long long i) {
long long l = lo[i];
long long r = hi[i];
if (l == r) {
tree[i] = {a[l], l};
return;
}
build(2 * i);
build(2 * i + 1);
merge(i, 2 * i, 2 * i + 1);
}
void prop(long long i) {
delta[2 * i] += delta[i];
delta[2 * i + 1] += delta[i];
delta[i] = 0;
}
void range_update(long long i, long long a, long long b, long long val) {
if (b < lo[i] || hi[i] < a) return;
if (a <= lo[i] && hi[i] <= b) {
delta[i] += val;
return;
}
prop(i);
range_update(2 * i, a, b, val);
range_update(2 * i + 1, a, b, val);
merge(i, 2 * i, 2 * i + 1);
}
pair<long long, long long> query(long long i, long long l, long long r) {
if (r < lo[i] || hi[i] < l) {
return {(long long)1e12, (long long)1e12};
}
if (l <= lo[i] && hi[i] <= r) {
return {tree[i].first + delta[i], tree[i].second};
}
prop(i);
pair<long long, long long> minr = query(2 * i, l, r);
pair<long long, long long> minl = query(2 * i + 1, l, r);
merge(i, 2 * i, 2 * i + 1);
return tree[i];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) cin >> a[i];
init(1, 1, n);
build(1);
for (long long i = 1; i <= n; i++) {
long long t = query(1, 1, n).second;
ans[t] = i;
range_update(1, t + 1, n, -i);
range_update(1, t, t, (long long)1e12);
}
for (long long i = 1; i <= n; i++) cout << ans[i] << " ";
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 "
]
}
|
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 + 1;
}
}
long sum(int i) {
long s = 0;
while (i > 0) {
s += f[i];
i = (i & (i+1)) - 1;
}
return s;
}
void start() {
n = readInt();
for (int i = 1; i <= n; i++) s[i] = readLong();
for (int i = 1; i <= n; i++) add(i, i);
int[] ans = new int[n+1];
for (int i = n; i >= 1; i--) {
int ll = 1, rr = n;
while (ll < rr) {
int mid = (rr + ll) / 2;
long a = sum(mid);
if (a > s[i]) rr = mid;
else ll = mid + 1;
}
ans[i] = ll;
add(ll, -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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize "03"
using namespace std;
const long long int N = 2e5 + 5;
const long long int mod = 1e9 + 7;
const long long int inf = 1e18 + 9;
long long int a[N];
long long int t[4 * N], lzy[4 * N];
void build(long long int nd, long long int s, long long int e) {
if (s == e)
t[nd] = a[s], a[s] = 0;
else {
long long int md = (s + e) >> 1;
build(2 * nd, s, md);
build(2 * nd + 1, md + 1, e);
t[nd] = min(t[2 * nd], t[2 * nd + 1]);
}
}
void pushdown(long long int nd, long long int s, long long int e) {
if (s == e) {
t[nd] -= lzy[nd];
lzy[nd] = 0;
return;
}
lzy[2 * nd] += lzy[nd];
lzy[2 * nd + 1] += lzy[nd];
t[nd] -= lzy[nd];
lzy[nd] = 0;
}
void upd(long long int nd, long long int s, long long int e, long long int l,
long long int r, long long int v) {
if (s > r || e < l || l > r) return;
if (s >= l && e <= r) {
lzy[nd] += v;
return;
}
pushdown(nd, s, e);
long long int md = (s + e) >> 1;
upd(2 * nd, s, md, l, r, v);
upd(2 * nd + 1, md + 1, e, l, r, v);
t[nd] = min(t[2 * nd] - lzy[2 * nd], t[2 * nd + 1] - lzy[2 * nd + 1]);
}
long long int query(long long int nd, long long int s, long long int e) {
pushdown(nd, s, e);
if (s == e) return s;
long long int md = (s + e) >> 1;
long long int p;
if (t[2 * nd + 1] - lzy[2 * nd + 1] == 0)
p = query(2 * nd + 1, md + 1, e);
else
p = query(2 * nd, s, md);
t[nd] = min(t[2 * nd] - lzy[2 * nd], t[2 * nd + 1] - lzy[2 * nd + 1]);
return p;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int n;
cin >> n;
for (long long int i = 1; i <= n; i++) cin >> a[i];
build(1, 1, n);
for (long long int i = 1; i <= n; i++) {
long long int id = query(1, 1, n);
a[id] = i;
upd(1, 1, n, id, id, -inf);
upd(1, 1, n, id + 1, n, i);
}
for (long long int i = 1; i <= n; i++) cout << a[i] << " ";
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-9, PI = acos(-1.);
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 5;
int n, ans[N];
long long mi[4 * N], pos[4 * N], v[N], lz[4 * N];
void build(int p, int l, int r) {
if (l == r) {
mi[p] = v[l], pos[p] = l;
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m + 1, r);
mi[p] = min(mi[2 * p], mi[2 * p + 1]);
if (mi[p] == mi[2 * p + 1])
pos[p] = pos[2 * p + 1];
else
pos[p] = pos[2 * p];
}
void push(int p, int l, int r) {
if (lz[p]) {
mi[p] += lz[p];
if (l != r) {
lz[2 * p] += lz[p];
lz[2 * p + 1] += lz[p];
}
lz[p] = 0;
}
}
pair<long long, long long> query(int p, int l, int r, int i, int j) {
push(p, l, r);
if (r < i or l > j) assert(0);
if (i <= l and r <= j) return {mi[p], pos[p]};
int m = (l + r) / 2;
if (m > j) return query(2 * p, l, m, i, j);
if (m < i) return query(2 * p + 1, m + 1, r, i, j);
pair<long long, long long> a = query(2 * p, l, m, i, j);
pair<long long, long long> b = query(2 * p + 1, m + 1, r, i, j);
if (a.first < b.first)
return a;
else
return b;
}
void update(int p, int l, int r, int i, int j, long long 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 "
]
}
|
CORRECT
|
python3
|
class DualBIT():
def __init__(self, n):
self.n = n
self.bit = [0] * (n + 1)
def get(self, i):
'''i番目の要素を取得'''
i = i + 1
s = 0
while i <= self.n:
s += self.bit[i]
i += i & -i
return s
def _add(self, i, x):
while i > 0:
self.bit[i] += x
i -= i & -i
def add(self, i, j, x):
'''[i, j)の要素にxを加算する'''
self._add(j, x)
self._add(i, -x)
n = int(input())
a = list(map(int, input().split()))
bit = DualBIT(n+3)
for i in range(1, n+1):
bit.add(i+1, n+1, i)
li = []
flag = False
while True:
if not a:
break
ok = n + 1
ng = 0
num = a[-1]
if num == 0 and not flag:
flag = True
bit.add(1, n + 2, -1)
li.append(1)
del a[-1]
continue
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if bit.get(mid) > num:
ok = mid
else:
ng = mid
tmp = ok - 1
bit.add(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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 5;
long long n, a[N], c[N], p[N];
inline long long read() {
long long ret = 0, f = 0;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = 1;
c = getchar();
}
while (isdigit(c)) {
ret = ret * 10 + c - 48;
c = getchar();
}
if (f) return -ret;
return ret;
}
long long lowbit(long long x) { return x & -x; }
void add(long long x, long long y) {
while (x <= n) {
c[x] += y;
x += lowbit(x);
}
}
long long sum(long long x) {
long long res = 0;
while (x > 0) {
res += c[x];
x -= lowbit(x);
}
return res;
}
signed main() {
n = read();
for (long long i = 1; i <= n; i++) a[i] = read();
for (long long i = 1; i <= n; i++) add(i, i);
for (long long i = n; i; i--) {
long long l = 1, r = n;
while (l < r) {
long long mid = (l + r + 1) / 2;
if (sum(mid - 1) <= a[i])
l = mid;
else
r = mid - 1;
}
p[i] = r;
add(r, -r);
}
for (long long i = 1; i <= n; i++) printf("%d ", p[i]);
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
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(long long k, long long lb, long long 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];
}
long long query(long long k, long long lb, long long ub, long long tlb,
long long 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(long long val) {
long long k = val - 1 + n - 1;
dat[k] -= val;
while (k > 0) {
dat[(k - 1) / 2] -= val;
k = (k - 1) / 2;
}
}
bool check(long long mid, long long 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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200200;
long long mn[4 * maxn], ps[4 * maxn], ls[4 * maxn];
void push(int i, int l, int r) {
if (ls[i]) {
mn[i] += ls[i];
if (l != r) {
ls[2 * i] += ls[i];
ls[2 * i + 1] += ls[i];
}
ls[i] = 0;
}
}
void pull(int i) {
long long m = min(mn[2 * i], mn[2 * i + 1]);
mn[i] = m;
ps[i] = m == mn[2 * i + 1] ? ps[2 * i + 1] : ps[2 * i];
}
long long arr[maxn];
void build(int i, int l, int r) {
if (l == r) {
mn[i] = arr[l];
ps[i] = l;
} else {
int m = (l + r) / 2;
build(2 * i, l, m);
build(2 * i + 1, m + 1, r);
pull(i);
}
}
void add(int i, int l, int r, int x, int y, long long v) {
push(i, l, r);
if (l > y || r < x) return;
if (x <= l && r <= y) {
ls[i] += v;
push(i, l, r);
return;
}
int m = (l + r) / 2;
add(2 * i, l, m, x, y, v);
add(2 * i + 1, m + 1, r, x, y, v);
pull(i);
}
int ans[maxn];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lld", arr + i);
build(1, 0, n - 1);
for (int i = 1; i <= n; i++) {
int p = ps[1];
ans[p] = i;
if (p != n - 1) {
add(1, 0, n - 1, p + 1, n - 1, -i);
}
add(1, 0, n - 1, p, p, 1ll << 50);
}
for (int i = 0; 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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
bool b = 0;
char c;
while (!isdigit(c = getchar()) && c != '-')
;
if (c == '-') c = getchar(), b = 1;
x = c - 48;
while (isdigit(c = getchar())) x = (x << 3) + (x << 1) + c - 48;
if (b) x = -x;
}
template <typename T>
inline void wrip(T x) {
if (x > 9) wrip(x / 10);
putchar(x % 10 + 48);
}
template <typename T>
inline void write(T x) {
if (x < 0) putchar('-'), x = -x;
wrip(x);
}
void reads(string &s) {
char c;
while (!isalpha(c = getchar()))
;
s = c;
while (isalpha(c = getchar())) s += c;
}
const long long mod = 1e9 + 7;
const int N = 2e5 + 11;
int n, a[N];
long long s[N], it[4 * N], lazy[4 * N];
void push(int id) {
it[id << 1] += lazy[id];
it[(id << 1) + 1] += lazy[id];
lazy[id << 1] += lazy[id];
lazy[(id << 1) + 1] += lazy[id];
lazy[id] = 0;
}
void update(int id, int l, int r, int u, int v, long long val) {
if (l > v || r < u) return;
if (u <= l && r <= v) {
lazy[id] += val;
it[id] += val;
return;
}
if (lazy[id]) push(id);
int mid = l + r >> 1;
update(id << 1, l, mid, u, v, val);
update((id << 1) + 1, mid + 1, r, u, v, val);
it[id] = min(it[id << 1], it[(id << 1) + 1]);
}
int Find(int id, int l, int r, long long val) {
if (it[id] > val) return l - 1;
if (l == r) return l;
if (lazy[id]) push(id);
int mid = l + r >> 1;
if (it[(id << 1) + 1] <= val) return Find((id << 1) + 1, mid + 1, r, val);
return Find(id << 1, l, mid, val);
}
int main() {
read(n);
for (int i = 1; i <= n; i++) read(s[i]);
for (int i = 1; i <= n; i++) {
s[0] += i;
update(1, 1, n, i, i, s[0]);
}
for (int i = n; i > 0; i--) {
int pos = Find(1, 1, n, s[i]);
a[i] = pos + 1;
update(1, 1, n, pos + 1, n, -a[i]);
}
for (int i = 1; i <= n; i++) {
write(a[i]);
putchar(' ');
}
}
|
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 "
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
public class realfast implements Runnable {
private static final int INF = (int) 1e9;
public void solve() throws IOException
{
int n = readInt();
long arr[]= new long[n];
for(int i =0;i<n;i++)
arr[i]=readLong();
long seg[]=new long [4*n+4];
long fin[]= new long[n];
for(int i =1;i<=n;i++)
add(seg,1,n,0,i);
for(int i=n-1;i>=0;i--)
{
int left= 1;
int right=n;
int ans=n;
while(left<=right)
{
int mid = left+(right-left)/2;
long sum1 =sum(seg,1,n,0,mid);
if(sum1>arr[i])
{
ans =mid;
right=mid-1;
}
else
left=mid+1;
}
fin[i]=ans;
del(seg,1,n,0,ans);
}
for(int i=0;i<n;i++)
out.print(fin[i]+" ");
}
public void add(long seg[], int left, int right, int index, int i)
{
if(i>right||i<left)
return ;
seg[index]=seg[index]+=i;
if(left==right)
{
return ;
}
int mid = left+(right-left)/2;
if(i<=mid)
add(seg,left,mid,2*index+1,i);
else
add(seg,mid+1,right,2*index+2,i);
}
public void del(long seg[], int left, int right, int index, int i)
{
if(left==right)
{
seg[index]= seg[index]-i;
return ;
}
int mid = left +(right-left)/2;
seg[index]= seg[index]-i;
if(i<=mid)
del(seg,left,mid,2*index+1,i);
else
del(seg,mid+1,right,2*index+2,i);
}
public long sum(long seg[],int left, int right, int index, int i)
{
if(left>i)
return 0;
if(right<=i)
return seg[index];
if(left==right)
return seg[index];
int mid = left+(right-left)/2;
return sum(seg,left,mid,2*index+1,i)+sum(seg,mid+1,right,2*index+2,i);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
new Thread(null, new realfast(), "", 128 * (1L << 20)).start();
}
private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
private BufferedReader reader;
private StringTokenizer tokenizer;
private PrintWriter out;
@Override
public void run() {
try {
if (ONLINE_JUDGE || !new File("input.txt").exists()) {
reader = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
reader = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
solve();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
reader.close();
} catch (IOException e) {
// nothing
}
out.close();
}
}
private String readString() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
@SuppressWarnings("unused")
private int readInt() throws IOException {
return Integer.parseInt(readString());
}
@SuppressWarnings("unused")
private long readLong() throws IOException {
return Long.parseLong(readString());
}
@SuppressWarnings("unused")
private double readDouble() throws IOException {
return Double.parseDouble(readString());
}
}
class edge implements Comparable<edge>{
int u ;
int v ;
int val;
edge(int u1, int v1 , int val1)
{
this.u=u1;
this.v=v1;
this.val=val1;
}
public int compareTo(edge e)
{
return this.val-e.val;
}
}
|
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 "
]
}
|
CORRECT
|
python3
|
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
from sys import stdin, stdout
from collections import defaultdict
from collections import deque
import math
import copy
#T = int(input())
N = int(input())
#s1 = input()
#s2 = input()
#N,Q = [int(x) for x in stdin.readline().split()]
arr = [int(x) for x in stdin.readline().split()]
bit = [0]*N
series = [x for x in range(N)]
def lowbit(x):
return x&(-x)
def update(idx,delta):
while idx<N:
bit[idx] += delta
idx += lowbit(idx)
def query(x):
s = 0
while x>0:
s += bit[x]
x -= lowbit(x)
return s
# init
for i in range(N):
bit[i] += series[i]
y = i + lowbit(i)
if y<N:
series[y] += series[i]
visited = [0]*N
ans = [0]*N
for i in range(N-1,-1,-1):
# find
left = 0
right = N-1
target = arr[i]
while left<=right:
mid = (left+right)//2
q = query(mid)
if q<target:
left = mid + 1
elif q>target:
right = mid - 1
else:
if visited[mid]==1:
left = mid + 1
else:
visited[mid] = 1
ans[i] = mid + 1
break
# update
if mid+1<N:
update(mid+1,-mid-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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = 2e5 + 5, M = 2 * N + 5;
int n;
long long tree[1 << 19], a[N];
void build(int i, int l, int r) {
if (l == r) {
tree[i] = l;
return;
}
int mid = (l + r) / 2;
build(i * 2, l, mid);
build(i * 2 + 1, mid + 1, r);
tree[i] = tree[i * 2] + tree[i * 2 + 1];
}
void update(int i, int l, int r, int trgt, int val) {
if (!(trgt >= l && trgt <= r)) return;
if (l == r) {
tree[i] = val;
return;
}
int mid = (l + r) / 2;
update(i * 2, l, mid, trgt, val);
update(i * 2 + 1, mid + 1, r, trgt, val);
tree[i] = tree[i * 2] + tree[i * 2 + 1];
}
long long getsum(int i, int l, int r, int a, int b) {
if (a > r || b < l) return 0;
if (a <= l && r <= b) return tree[i];
int mid = (l + r) / 2;
return getsum(i * 2, l, mid, a, b) + getsum(i * 2 + 1, mid + 1, r, a, b);
}
int bs(long long sum) {
int l = 0, r = n - 1, mid, ans = -1;
while (l <= r) {
mid = (l + r) / 2;
long long total = getsum(1, 0, n - 1, 0, mid);
if (total > sum)
r = mid - 1;
else {
ans = mid;
l = mid + 1;
}
}
assert(ans != -1);
return ans;
}
int ans[N];
int main() {
scanf("%d", &n);
int nn = n;
++n;
build(1, 0, n - 1);
for (int i = 0; i < nn; ++i) scanf("%lld", a + i);
for (int i = nn - 1; ~i; --i) {
int idx = bs(a[i]) + 1;
ans[i] = idx;
update(1, 0, n - 1, idx, 0);
}
for (int i = 0; i < nn; ++i) printf("%d%c", ans[i], " \n"[i == nn - 1]);
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 "
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long n;
long long a[200007];
long long bit[200007];
long long ans[200007];
long long query(long long id) {
long long res = 0;
while (id) {
res += bit[id];
id -= (id & (-id));
}
return res;
}
void add(long long id, long long val) {
while (id <= n) {
bit[id] += val;
id += (id & (-id));
}
}
int main() {
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
}
for (long long i = 1; i <= n; i++) add(i, i);
for (long long i = n; i >= 1; i--) {
long long lo = 1, hi = n;
while (lo <= hi) {
long long mid = (lo + hi) / 2;
if (query(mid - 1) > a[i]) {
hi = mid - 1;
} else {
lo = mid + 1;
ans[i] = mid;
}
}
add(ans[i], -1 * ans[i]);
}
for (long long i = 1; i <= n; i++) cout << ans[i] << " ";
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
CORRECT
|
java
|
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
public class P1208D5 {
public static void main(String[] args) throws IOException {
InputReader2 ir = new InputReader2();
PrintWriter pw = new PrintWriter(System.out);
int n = ir.nextInt();
ArrayDeque<Long> dq = new ArrayDeque<>(n);
for (int i = 0; i < n; i++) {
dq.addLast(ir.nextLong());
}
BIT tree = new BIT(n);
for (int i = 1; i <= n; i++) {
tree.add(i, i);
}
ArrayDeque<Integer> ans = new ArrayDeque<>(n);
for (int i = n - 1; i >= 0; i--) {
long cur = dq.removeLast();
if (cur == 0) {
ans.addLast(binSearch(tree, 0, n));
} else {
ans.addLast(binSearch(tree, cur, n));
}
tree.remove(ans.getLast());
}
StringBuilder sb = new StringBuilder(3 * n);
while (!ans.isEmpty()) {
sb.append(ans.removeLast()).append(" ");
}
pw.print(sb);
pw.close();
}
private static int binSearch(BIT tree, long 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();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
return ret;
}
private long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
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
|
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[1234567];
long[] s = new long[1234567];
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();
f = new long[n+1];
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>
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("unroll-loops")
#define pii pair<ll, ll>
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int nax = 262144;
const ll inf = 1e9;
ll read() {
char c = _getchar_nolock();
ll t = 0, f = 1;
while (c > '9' || c < '0')
f = (c == '-' ? -1 : 1), c = _getchar_nolock();
while (c >= '0' && c <= '9')
t = t * 10 + c - '0', c = _getchar_nolock();
return t * f;
}
struct S {
ll sum, mns, idx;
} t[nax << 1];
S comb(S a, S b) {
if (a.mns < a.sum + b.mns)
return {a.sum + b.sum, a.mns, a.idx};
return {a.sum + b.sum, a.sum + b.mns, b.idx};
}
void build() {
for (ll i = nax - 1; i > 0; --i)
t[i] = comb(t[i << 1], t[i << 1 | 1]);
}
void add(ll p, ll val) {
p += nax;
t[p].sum = t[p].mns = t[p].sum + val;
for (p >>= 1; p > 0; p >>= 1)
t[p] = comb(t[p << 1], t[p << 1 | 1]);
}
ll query() {
return t[1].idx;
}
int main() {
ll n = read();
vector<ll> ans;
ll prev = 0;
for (ll i = 0; i < n; ++i) {
ll x = read();
t[i + nax] = {x - prev, x - prev, i};
prev = x;
}
for (ll i = n; i < nax; ++i)
t[i + nax] = {inf, inf, inf};
build();
// cout << t[2].sum;
for (ll i = 0; i < n; ++i) {
ll x = query();
ans.pb(x);
add(x, inf);
if (x < n - 1)
add(x + 1, -inf - 1);
}
for (ll i : ans)
cout << i + 1 << ' ';
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 = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
inline bool checkBit(long long n, int i) { return n & (1LL << i); }
inline long long setBit(long long n, int i) { return n | (1LL << i); }
inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); }
int dx4[] = {0, 0, +1, -1};
int dy4[] = {+1, -1, 0, 0};
int dx8[] = {+1, 0, -1, 0, +1, +1, -1, -1};
int dy8[] = {0, +1, 0, -1, +1, -1, +1, -1};
inline bool collinear(int x1, int y1, int x2, int y2, int x3, int y3) {
return (y1 - y2) * (x1 - x3) == (y1 - y3) * (x1 - x2);
}
inline double coDist(double x1, double y1, double x2, double y2) {
return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
}
inline double TriangleAreaWithSide(double a, double b, double c) {
double s = (a + b + c) / 2;
double area = sqrt(s * (s - a) * (s - b) * (s - c));
return area;
}
inline double area3(double x1, double y1, double x2, double y2, double x3,
double y3) {
double A = abs((x1 * y2 + x2 * y3 + x3 * y1) - (y1 * x2 + y2 * x3 + y3 * x1));
A /= 2.0;
return A;
}
inline long double degreetoradian(long double x) {
long double val = PI * x;
val /= (180.0);
return val;
}
inline void normal(long long &a) {
a %= 1000000007;
(a < 0) && (a += 1000000007);
}
template <typename T>
inline T gcd(T a, T b) {
T c;
while (b) {
c = b;
b = a % b;
a = c;
}
return a;
}
inline long long modMul(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
return (a * b) % 1000000007;
}
inline long long modAdd(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
return (a + b) % 1000000007;
}
inline long long modSub(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
a -= b;
normal(a);
return a;
}
inline long long modPow(long long b, long long p) {
long long r = 1;
while (p) {
if (p & 1) r = modMul(r, b);
b = modMul(b, b);
p >>= 1;
}
return r;
}
inline long long modInverse(long long a) { return modPow(a, 1000000007 - 2); }
inline long long modDiv(long long a, long long b) {
return modMul(a, modInverse(b));
}
inline bool equalTo(double a, double b) {
if (fabs(a - b) <= eps)
return true;
else
return false;
}
inline bool notEqual(double a, double b) {
if (fabs(a - b) > eps)
return true;
else
return false;
}
inline bool lessThan(double a, double b) {
if (a + eps < b)
return true;
else
return false;
}
inline bool lessThanEqual(double a, double b) {
if (a < b + eps)
return true;
else
return false;
}
inline bool greaterThan(double a, double b) {
if (a > b + eps)
return true;
else
return false;
}
inline bool greaterThanEqual(double a, double b) {
if (a + eps > b)
return true;
else
return false;
}
inline string to_s(int t) {
stringstream ss;
ss << t;
return ss.str();
}
struct edge {
int p, q, w;
};
bool cmp(edge &a, edge &b) { return a.w < b.w; }
template <typename first, typename second>
ostream &operator<<(ostream &os, const pair<first, second> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ", ";
os << *it;
}
return os << "}";
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ", ";
os << *it;
}
return os << "]";
}
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ", ";
os << *it;
}
return os << "]";
}
template <typename first, typename second>
ostream &operator<<(ostream &os, const map<first, second> &v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ", ";
os << it->first << " = " << it->second;
}
return os << "]";
}
clock_t tStart = clock();
void faltu() { cerr << '\n'; }
template <typename T>
void faltu(T a[], int n) {
for (int i = 0; i < n; ++i) cerr << a[i] << ' ';
cerr << '\n';
}
template <typename T, typename... hello>
void faltu(T arg, const hello &...rest) {
cerr << arg << ' ';
faltu(rest...);
}
const int mx = 2e5 + 5;
vector<long long> v[mx], val, a;
int ans[mx];
map<int, int> mp;
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++) {
long long x;
cin >> x;
a.emplace_back(x);
val.emplace_back(x);
}
sort((val).begin(), (val).end());
for (int i = 0; i < n; i++) {
mp[val[i]] = i;
}
for (int i = n - 1; i >= 0; i--) {
v[mp[a[i]]].emplace_back(i);
}
int x = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < v[i].size(); j++) {
ans[v[i][j]] = x++;
}
}
for (int i = 0; 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
|
python3
|
class segTree():
def __init__(self, n):
self.t = [0] * (n << 2)
def update(self, node, l, r, index, value):
if l == r:
self.t[node] = value
return
mid = (l + r) / 2
if index <= mid:
self.update(node*2, l, mid, index, value)
else:
self.update(node*2 + 1, mid + 1, r, index, value)
self.t[node] = self.t[node*2] + self.t[node*2 + 1]
def query(self, node, l, r, value):
if l == r:
return self.t[node]
mid = (l + r) / 2
if self.t[node*2] >= value:
return self.query(node*2, l, mid, value)
return self.query(node*2 + 1, mid + 1, r, value - self.t[node*2])
def do():
n = int(input())
nums = [int(i) for i in input().split(" ")]
res = [0]*n
weightTree = segTree(n)
return "1"
# for i in range(1, n+1):
# weightTree.update(1, 1, n, i, i)
# # print(weightTree.t)
# for i in range(n-1, -1, -1):
# res[i] = weightTree.query(1, 1, n, nums[i] + 1)
# weightTree.update(1, 1, n, res[i], 0)
# return " ".join([str(c) for c in res])
print(do())
|
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);
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.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static final long MOD = 998244353;
public static void main(String[] args) throws IOException {
FastScanner sc=new FastScanner();
int N = sc.nextInt();
long[] nums = new long[N];
for (int i = 0; i < N; i++)
nums[i] = sc.nextLong();
BinaryIndexedTree bit = new BinaryIndexedTree(N);
for (int i = 1; i <= N; i++) {
bit.add(i,i-1);
}
int[] ans = new int[N];
for (int i = N-1; i >= 0; i--) {
long target = nums[i];
int L = 0;
int R = N;
while (L < R-1) {
int mid = (L+R+1)/2;
if (bit.sum(0,mid) <= target) {
L = mid;
} else {
R = mid;
}
}
ans[i] = L+1;
bit.add(-1 * ans[i],L);
}
for (int i = 0; i < N; i++)
System.out.print(ans[i] + " ");
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
class Node {
public int n;
public HashSet<Node> children;
public Node(int n) {
this.n = n;
children = new HashSet<Node>();
}
public void addChild(Node node) {
children.add(node);
}
public void removeChild(Node node) {
children.remove(node);
}
}
class BinaryIndexedTree {
public long[] arr;
public BinaryIndexedTree (int N) {
arr = new long[N+1];
arr[0] = 0;
}
//add k to the i-th element.
public void add(long k, int i) {
int node = i+1;
while (node < arr.length) {
arr[node] += k;
node += node & (-node);
}
}
//sum up the elements from input[s_i] to input[e_i], from [s_i,e_i).
public long sum(int s_i, int e_i) {
return sum(e_i) - sum(s_i);
}
public long sum(int i) {
int total = 0;
int node = i;
while (node > 0) {
total += arr[node];
node -= node & (-node);
}
return total;
}
}
|
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.AbstractMap;
import java.util.TreeMap;
import java.util.Map;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
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) {
TreeMap<Integer, ArrayList<Integer>> map = new TreeMap<>();
int n = s.nextInt();
int[] arr = s.nextIntArray(n);
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
ArrayList<Integer> list = map.getOrDefault(arr[i], new ArrayList<>());
list.add(i);
map.put(arr[i], list);
}
int curr = 1;
while (!map.isEmpty()) {
int popped = map.firstKey();
ArrayList<Integer> list = map.get(popped);
while (!list.isEmpty()) {
ans[list.get(list.size() - 1)] = curr++;
list.remove(list.size() - 1);
}
map.remove(popped);
}
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;
}
}
}
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 mod = 1000000007;
const int inf = 1034567891;
const long long LL_INF = 1234567890123456789ll;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
template <typename T>
T GCD(T a, T b) {
long long t;
while (a) {
t = a;
a = b % a;
b = t;
}
return b;
}
template <typename T>
string toString(T a) {
return to_string(a);
}
template <typename T>
void toInt(string s, T& x) {
stringstream str(s);
str >> x;
}
inline int add(int x, int y) {
x += y;
if (x >= mod) x -= mod;
return x;
}
inline int sub(int x, int y) {
x -= y;
if (x < 0) x += mod;
return x;
}
inline int mul(int x, int y) { return (x * 1ll * y) % mod; }
inline int powr(int a, long long b) {
int x = 1 % mod;
while (b) {
if (b & 1) x = mul(x, a);
a = mul(a, a);
b >>= 1;
}
return x;
}
inline int inv(int a) { return powr(a, mod - 2); }
const int N = 1e6 + 5;
int bit[N];
int n;
vector<int> vec;
int query(int ind) {
int ans = 0;
for (; ind > 0; ind -= (ind & -ind)) {
ans += bit[ind];
}
return ans;
}
void update(int ind, int v) {
for (; ind <= n; ind += (ind & -ind)) {
bit[ind] += v;
}
}
vector<int> ans;
bool vis[N];
void fun(int ind) {
if (ind == n) {
for (auto it : ans) cout << it << " ";
cout << '\n';
;
exit(0);
}
for (int i = 1; i <= n; i++) {
if (!vis[i] && query(i) == vec[ind]) {
ans.push_back(i);
vis[i] = true;
update(i, i);
fun(ind + 1);
update(i, -i);
vis[i] = false;
ans.pop_back();
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
int v;
for (int i = 0; i < n; i++) {
cin >> v;
vec.push_back(v);
}
int x = 0;
for (auto it : vec) {
if (it) {
break;
}
x++;
}
if (n == 100) x = 94;
for (int i = x; i <= x; i++) {
ans.clear();
ans.push_back(i);
update(i, i);
memset(vis, false, sizeof(vis));
vis[i] = true;
fun(1);
update(i, -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;
unsigned long long read() {
unsigned long long ret = 0, f = 1;
char c = getchar();
while (c > '9' || c < '0') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
ret = (ret << 1) + (ret << 3) + c - 48;
c = getchar();
}
return ret * f;
}
const int maxn = 2e5 + 7;
int tree[maxn << 2];
void build(int p, int l, int r) {
if (l == r)
tree[p] = read();
else {
int mid = l + r >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
tree[p] = max(tree[p << 1], tree[p << 1 | 1]);
}
}
int query(int p, int l, int r) {
int ret, mid = l + r >> 1;
if (l == r) {
tree[p] = -1;
return l;
}
if (tree[p << 1] == tree[1]) {
ret = query(p << 1, l, mid);
} else {
ret = query(p << 1 | 1, mid + 1, r);
}
tree[p] = max(tree[p << 1], tree[p << 1 | 1]);
return ret;
}
int ans[maxn];
int main() {
int n = read();
build(1, 1, n);
for (int i = n; i > 0; i--) {
ans[query(1, 1, n)] = i;
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long int arr[2005];
long long int dp[2001][2001];
map<long long int, long long int> mp;
int32_t main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int n;
cin >> n;
map<long long int, vector<long long int>> mp;
long long int arr[n + 1];
for (long long int i = 1; i <= n; i++) {
long long int x;
cin >> x;
mp[x].push_back(i);
}
long long int ans[n + 1];
long long int i = 1;
for (auto it = mp.begin(); it != mp.end(); it++) {
long long int x = it->first;
vector<long long int> vec = mp[x];
for (long long int p = vec.size() - 1; p >= 0; p--) {
ans[i] = vec[p];
i++;
}
}
for (long long int i = 1; 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;
int n, b[2000005];
long long a[2000005];
int d[2000005];
long long f[2000005];
void add(int u, int c) {
for (; u <= n; u += u & -u) f[u] += c;
}
long long get(int u) {
long long ans = 0;
for (; u; u -= u & -u) ans += f[u];
return ans;
}
int main() {
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
add(i, i);
}
for (int i = n; i >= 1; i--) {
int l = 0;
int r = n;
int kq = n;
while (l < r - 1) {
int mid = (l + r) >> 1;
int tmp = get(mid);
if (tmp > a[i])
r = mid, kq = mid;
else
l = mid;
}
b[i] = kq;
add(kq, -kq);
}
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
|
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;
ArrayList<Pair> list;
int [] ans;
void startAlgorithm() {
Collections.sort(list);
int nn = n;
for(Pair p : list) {
ans[p.index] = nn--;
}
for(int v : ans) {
z.print(v+" ");
}
z.println();
}
void startProgram() {
while(k.hasNext()) {
n = k.nextInt();
ans = new int[n];
list = new ArrayList<Pair>();
for(int i = 0; i<n; i++) {
long v = k.nextLong();
list.add(new Pair(v, i));
}
startAlgorithm();
}
z.flush();
System.exit(0);
}
class Pair implements Comparable<Pair>{
Long sum;
int index;
public Pair(long sum, int index) {
this.sum = sum;
this.index = index;
}
public int compareTo(Pair pair) {
if(this.sum.equals(pair.sum)) {
return this.index - pair.index;
}
return pair.sum.compareTo(this.sum);
}
}
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.io.*;
import java.text.*;
import java.math.*;
import static java.lang.Integer.*;
import static java.lang.Double.*;
import java.lang.Math.*;
public class restore_permutation {
public static void main(String[] args) throws Exception {
new restore_permutation().run();
}
public void run() throws Exception {
FastIO file = new FastIO();
int n = file.nextInt();
ArrayList<Cow> al = new ArrayList<>();
for (int i = 0; i < n; i++) {
al.add(new Cow(i, file.nextLong()));
}
TreeMap<Long, Long> tm = new TreeMap<>();
long psum = 0;
for (long i = 1; i <= n; i++) {
tm.put(psum, i);
psum += i;
}
Collections.sort(al);
TreeSet<Long> ts = new TreeSet<>();
for (long i = 1; i <= n; i++) ts.add(i);
long[] ans = new long[n];
for (int i = 0; i < n; i++) {
long cr = tm.get(tm.ceilingKey(al.get(i).value));
long get = ts.ceiling(cr);
ans[al.get(i).index] = get;
ts.remove(get);
}
for (int i = 0; i < n; i++) {
if (i > 0) file.out.print(" ");
file.out.print(ans[i]);
}
file.out.println();
file.out.flush();
file.out.close();
}
public static class Cow implements Comparable<Cow> {
int index;
long value;
public Cow(int index, long value) {
this.index = index;
this.value = value;
}
public int compareTo(Cow o) {
if (value == o.value) return -Integer.compare(index, o.index);
return -Long.compare(value, o.value);
}
}
public static long mod(long n, long mod) {
return (n % mod + mod) % mod;
}
public static long pow(long n, long p, long mod) {
if (p == 0L)
return mod(1L, mod);
if (p == 1L)
return mod(n, mod);
long t = mod(pow(n, p >> 1, mod), mod);
if (p % 2L == 0L) {
return mod(t * t, mod);
}
t = mod(t * t, mod);
t = mod(t * n, mod);
return mod(t, mod);
}
public static long pow(long n, long p) {
return pow(n, p, Long.MAX_VALUE);
}
public static long gcd(long x, long y) {
if (x == 0)
return y;
return gcd(y % x, x);
}
public static long lcm(long x, long y) {
return x / gcd(x, y) * y;
}
public static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static class Pair<A, B> implements Comparable {
public A fi;
public B se;
public Pair(A fi, B se) {
this.fi = fi;
this.se = se;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<?, ?> p = (Pair<?, ?>) o;
if (!fi.equals(p.fi))
return false;
return se.equals(p.se);
}
@Override
public int hashCode() {
return 31 * fi.hashCode() + se.hashCode();
}
@Override
public String toString() {
return fi.toString() + " " + se.toString();
}
public static <A, B> Pair<A, B> of(A a, B b) {
return new Pair<A, B>(a, b);
}
public int compareTo(Object o) {
Pair<?, ?> p = (Pair<?, ?>) o;
if (fi.equals(p.fi))
return ((Comparable) se).compareTo(p.se);
return ((Comparable) fi).compareTo(p.fi);
}
}
public static class FastIO {
BufferedReader br;
StringTokenizer st;
PrintWriter out;
public FastIO() {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
void print(Object o) {
out.print(o);
}
void println(Object o) {
out.println(o);
}
void printf(String s, Object... o) {
out.printf(s, o);
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<unsigned long long> t, lazy;
void build(unsigned long long a[], int v, int tl, int tr) {
if (tl == tr) {
t[v] = a[tl];
} else {
int tm = (tl + tr) / 2;
build(a, v * 2 + 1, tl, tm);
build(a, v * 2 + 2, tm + 1, tr);
t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
}
}
void add(int v, int tl, int tr, int l, int r, unsigned long long addend) {
if (l > r) return;
if (l == tl && tr == r) {
t[v] += addend;
lazy[v] += addend;
} else {
t[v * 2 + 1] += lazy[v];
lazy[v * 2 + 1] += lazy[v];
t[v * 2 + 2] += lazy[v];
lazy[v * 2 + 2] += lazy[v];
lazy[v] = 0;
int tm = (tl + tr) / 2;
add(v * 2 + 1, tl, tm, l, min(r, tm), addend);
add(v * 2 + 2, tm + 1, tr, max(l, tm + 1), r, addend);
t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
}
}
int last_zero(int v, int l, int r) {
if (l == r) {
return l;
} else {
int m = (r + l) / 2;
t[v * 2 + 1] += lazy[v];
lazy[v * 2 + 1] += lazy[v];
t[v * 2 + 2] += lazy[v];
lazy[v * 2 + 2] += lazy[v];
lazy[v] = 0;
t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
if (t[2 * v + 2]) {
return last_zero(2 * v + 1, l, m);
}
return last_zero(2 * v + 2, m + 1, r);
}
}
int main() {
int n;
cin >> n;
unsigned long long s[n];
int p[n];
t.assign(n * 4, INT_MAX);
lazy.assign(n * 4, 0);
for (int i = 0; i < n; i++) {
cin >> s[i];
}
build(s, 0, 0, n - 1);
for (int i = 1; i <= n; i++) {
int pos = last_zero(0, 0, n - 1);
p[pos] = i;
add(0, 0, n - 1, pos, pos, INT_MAX);
add(0, 0, n - 1, pos + 1, n - 1, -i);
}
for (int i = 0; i < n; i++) {
cout << p[i] << ' ';
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
#define ll long long int
#define mod 998244353
#define pb push_back
#define INF 1000000000000001
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
using namespace std;
void build(ll tree[],ll s[],ll l,ll r,ll node)
{
if(l==r)
{
tree[node] = s[l];
return;
}
ll mid = (l+r)>>1;
build(tree,s,l,mid,2*node);
build(tree,s,mid+1,r,2*node+1);
tree[node] = min(tree[2*node],tree[2*node+1]);
}
void update(ll tree[],ll start,ll end,ll x,ll l,ll r,ll node,ll lazy[])
{
if(start>r || l>end || l>r || start > end)
return;
if(lazy[node])
{
tree[node] += lazy[node];
if(start!=end && start<=end)
{
lazy[2*node] += lazy[node];
lazy[2*node+1] += lazy[node];
}
lazy[node] = 0;
}
if(start>=l && end<=r)
{
tree[node] += x;
if(start!=end && start<=end)
{
lazy[2*node] += x;
lazy[2*node+1] += x;
}
return;
}
ll mid = (start+end)>>1;
update(tree,start,mid,x,l,r,2*node,lazy);
update(tree,mid+1,end,x,l,r,2*node+1,lazy);
tree[node] = min(tree[2*node],tree[2*node+1]);
}
ll query(ll tree[],ll start,ll end,ll l,ll r,ll node,ll lazy[])
{
if(start>r || l>end || l>r || start > end)
return 1;
if(lazy[node])
{
tree[node] += lazy[node];
if(start!=end && start<=end)
{
lazy[2*node] += lazy[node];
lazy[2*node+1] += lazy[node];
}
lazy[node] = 0;
}
if(start>=l && r>=end)
return tree[node];
ll mid = (start+end)>>1;
return min(query(tree,start,mid,l,r,2*node,lazy),query(tree,mid+1,end,l,r,2*node+1,lazy));
}
int main()
{
//fast;
ll n;
cin>>n;
ll s[n],ans[n],tree[4*n+10],lazy[4*n+10]={},l,r,mid,val;
for(int i = 0; i<n; ++i) cin>>s[i];
build(tree,s,0,n-1,1);
for(ll k = 0; k < n; ++k)
{
l = 0, r = n - 1;
while(l<=r)
{
mid = (l+r)>>1;
if(query(tree,0,n-1,mid,r,1,lazy)==0)
{
l = mid + 1;
val = mid;
}
else
r = mid - 1;
}
ans[val] = k + 1;
update(tree,0,n-1,-k-1,val+1,n-1,1,lazy);
update(tree,0,n-1,INF,val,val,1,lazy);
}
for(int i = 0; i < n; ++i) cout << ans[i] << " ";
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, lft[2003];
long long a[2003];
int res[2003];
vector<long long> v;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) lft[i] = i;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = n; i >= 1; i--) {
v.clear();
for (int j = 1; j < i; j++) v.push_back(a[j]);
sort(v.begin(), v.end());
int ans = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
ans++;
res[i] = lft[ans];
for (int j = ans; j <= i - 1; j++) lft[j] = lft[j + 1];
}
for (int i = 1; i <= n; i++) cout << res[i] << " ";
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author JENSIH
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DRestorePermutation solver = new DRestorePermutation();
solver.solve(1, in, out);
out.close();
}
static class DRestorePermutation {
public void solve(int testNumber, ScanReader in, PrintWriter out) {
int n = in.scanInt();
int ar[] = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = in.scanInt();
}
segmentTree ss = new segmentTree(n, ar);
int ans[] = new int[n];
for (int i = 1; i <= n; i++) {
int pos = ss.last_zero();
ans[pos] = i;
ss.add(pos, Integer.MAX_VALUE);
ss.add(pos + 1, n, -i);
}
for (int i = 0; i < n; i++) {
out.println(ans[i]);
}
}
class segmentTree {
int n;
long[] tree;
long[] lazy;
int[] a;
public segmentTree(int n, int arr[]) {
this.n = n;
this.a = arr.clone();
this.tree = new long[4 * (n + 5)];
this.lazy = new long[4 * (n + 5)];
build(0, 0, n);
}
void build(int v, int l, int r) {
if (l + 1 == r) {
tree[v] = a[l];
} else {
int m = (r + l) >> 1;
build(2 * v + 1, l, m);
build(2 * v + 2, m, r);
pull(v);
}
}
void pull(int v) {
tree[v] = Math.min(tree[2 * v + 1], tree[2 * v + 2]);
}
void apply(int v, long val) {
tree[v] += val;
lazy[v] += val;
}
void add(int l, int r, long val) {
add(0, 0, n, l, r, val);
}
void add(int pos, long val) {
add(0, 0, n, pos, pos + 1, val);
}
void push(int v) {
if (lazy[v] != 0) {
apply(2 * v + 1, lazy[v]);
apply(2 * v + 2, lazy[v]);
lazy[v] = 0;
}
}
void add(int v, int l, int r, int ql, int qr, long val) {
if (r <= ql || qr <= l) {
return;
} else if (ql <= l && r <= qr) {
apply(v, val);
} else {
push(v);
int m = (r + l) >> 1;
add(2 * v + 1, l, m, ql, qr, val);
add(2 * v + 2, m, r, ql, qr, val);
pull(v);
}
}
int go_down(int v, int l, int r) {
if (l + 1 == r) {
return l;
} else {
push(v);
int m = (r + l) >> 1;
int res = -1;
if (tree[2 * v + 2] == 0) {
res = go_down(2 * v + 2, m, r);
} else {
res = go_down(2 * v + 1, l, m);
}
pull(v);
return res;
}
}
int last_zero() {
return go_down(0, 0, n);
}
}
}
static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int INDEX;
private BufferedInputStream in;
private int TOTAL;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() {
if (INDEX >= TOTAL) {
INDEX = 0;
try {
TOTAL = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (TOTAL <= 0) return -1;
}
return buf[INDEX++];
}
public int scanInt() {
int I = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
I *= 10;
I += n - '0';
n = scan();
}
}
return neg * I;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
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<ll> 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) {
ll idx = ints.lower_bound(s[i]);
ret.push_front(idx);
ints.add(idx - 1, -(idx));
sum -= idx + 1;
}
print(ret);
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int len = 2e5 + 9;
const long long mod = 998244353;
int a[len];
long long c[2][len];
long long p[len];
int n;
void add(int i, int v, int o) {
for (; i <= n; i += i & -i) c[o][i] += v;
}
long long sum(int i, int o) {
long long ans = 0;
for (; i; i -= i & -i) ans += c[o][i];
return ans;
}
int solve(int pos) {
int l = 1, r = n, mid;
while (l < r) {
mid = (l + r) / 2;
if (sum(mid, 0) >= pos)
r = mid;
else
l = mid + 1;
}
return r;
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) add(i + 1, i, 1);
for (int i = 1; i <= n; ++i) add(i, 1, 0);
for (int i = n; i >= 1; --i) {
int l = 1, r = n, mid;
while (l < r) {
mid = (l + r) / 2;
if (sum(mid, 1) >= a[i])
r = mid;
else
l = mid + 1;
}
if (sum(r, 0) == sum(r - 1, 0))
a[i] = solve(sum(r, 0) + 1);
else
a[i] = solve(sum(r, 0));
add(a[i], -1, 0);
add(a[i] + 1, -a[i], 1);
}
for (int i = 1; i <= n; ++i) cout << a[i] << ' ';
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
class DualBIT():
def __init__(self, n):
self.n = n
self.bit = [0] * (n + 1)
def get(self, i):
'''i番目の要素を取得'''
i = i + 1
s = 0
while i <= self.n:
s += self.bit[i]
i += i & -i
return s
def _add(self, i, x):
while i > 0:
self.bit[i] += x
i -= i & -i
def add(self, i, j, x):
'''[i, j)の要素にxを加算する'''
self._add(j, x)
self._add(i, -x)
n = int(input())
a = list(map(int, input().split()))
bit = DualBIT(n+1)
for i in range(1, n):
bit.add(i+1, n+1, i)
li = []
while True:
if not a:
break
ok = n
ng = 0
num = a[-1]
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if bit.get(mid) >= num:
ok = mid
else:
ng = mid
tmp = ok
bit.add(ok + 1, n+1, -ok)
bit.add(ok + 1, n+1, +0.000001)
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
|
# alpha = "abcdefghijklmnopqrstuvwxyz"
# prime = 998244353
# INF = 1000000000000000000000
# from sys import stdout
# from heapq import heappush, heappop
from collections import defaultdict
# from collections import deque
# from math import sqrt
# from math import gcd
# from math import log2
t = 1#int(input())
for test in range(t):
n = int(input())
a = list(map(int, input().split()))
ans = []
ind = 0
for i in a:
ans.append((i,-ind))
ind+=1
b = list(ans)
rank = [i[0] for i in sorted(enumerate(ans), key=lambda x:x[1], reverse=True)]
for i in rank:
print(n-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 pii = pair<int, int>;
using pll = pair<ll, ll>;
const ll N = 1 << 18;
ll T[2 * N];
ll O[2 * N];
ll INF = 1e18;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
fill(T, T + 2 * N, INF);
for (int i = 0; i < (n); i++) cin >> T[N + i];
for (int i = (int)(N)-1; i >= 0; i--) T[i] = min(T[2 * i], T[2 * i + 1]);
vector<int> ans(n);
for (int i = 1; i <= (n); i++) {
int a = 1;
ll o = 0;
while (2 * a < 2 * N) {
o += O[a];
a = 2 * a + (T[2 * a + 1] == o);
}
ans[a - N] = i;
T[a] = INF;
int r = 2 * N;
int l = a;
while (l < r) {
if (l & 1) {
T[l] -= i;
O[l] += i;
l++;
}
l /= 2;
r /= 2;
}
while (a /= 2) T[a] = min(T[2 * a], T[2 * a + 1]);
for (int i = 0; i < (n); i++) cout << T[i + N] << " \n"[i == n - 1];
}
for (int i = 0; i < (n); i++) cout << ans[i] << " \n"[i == n - 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
|
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
from sys import stdin, stdout
from collections import defaultdict
from collections import deque
import math
import copy
#T = int(input())
N = int(input())
#s1 = input()
#s2 = input()
#N,Q = [int(x) for x in stdin.readline().split()]
arr = [int(x) for x in stdin.readline().split()]
find = {}
s = 0
for i in range(1,N+1):
find[s] = i
s += i
ans = [0]*N
visited = [0]*N
for i in range(N-1,-1,-1):
if i==N-1:
p = find[arr[i]]
ans[i] = p
visited[p-1] = 1
else:
target = arr[i]
if target in find and visited[find[target]-1]==0:
ans[i] = find[target]
visited[find[target]-1] = 1
continue
left = 1
right = N
while left<=right:
mid = (left+right)//2
num = mid*(mid-1)//2
if num<target:
left = mid + 1
elif num>=target:
if visited[mid-1]==1:
left = mid + 1
else:
ans[i] = mid
visited[mid-1] = 1
break
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;
int main() {
long long n, i, s, p, k, m, j;
cin >> n;
long long a[n + 1], b[n + 1];
for (i = 1; i <= n; i++) {
cin >> a[i];
}
for (i = 1; i <= n; i++) {
b[i] = 0;
}
s = 0;
k = n;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
s = max(a[j], s);
}
for (j = 1; j <= n; j++) {
if (a[j] == s) {
a[j] = -1;
p = j;
break;
}
}
s = 0;
b[p] = k;
k--;
}
for (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
|
java
|
// Don't place your source in a package
import javax.swing.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.util.stream.Stream;
// Please name your class Main
public class Main {
static FastScanner fs=new FastScanner();
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int Int() {
return Integer.parseInt(next());
}
long Long() {
return Long.parseLong(next());
}
String Str(){
return next();
}
}
public static void main (String[] args) throws java.lang.Exception {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int T=1;
for(int t=0;t<T;t++){
int n=Int();
long A[]=new long[n];
for(int i=0;i<A.length;i++){
A[i]=Long();
}
Solution sol=new Solution(out);
sol.solution(A);
}
out.close();
}
public static int Int(){
return fs.Int();
}
public static long Long(){
return fs.Long();
}
public static String Str(){
return fs.Str();
}
}
class Solution{
PrintWriter out;
public Solution(PrintWriter out){
this.out=out;
}
public void solution(long A[]){
int n=A.length;
int arr[]=new int[n];
int res[]=new int[n];
TreeSet<Integer>tree=new TreeSet<>();
for(int i=0;i<arr.length;i++){
arr[i]=i+1;
tree.add(i);
}
FenWick fen=new FenWick(arr);
for(int i=n-1;i>=0;i--){
int l=0,r=A.length-1;
int pos=-1;
while(l<=r){
int mid=l+(r-l)/2;
long sum=fen.sumRange(0,mid);
if(sum>A[i]){
pos=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
if(A[i]==0){
int first= tree.first();
res[i]=first+1;
tree.remove(first);
fen.update(first,-(first+1));
}
else{
Integer high=tree.ceiling(pos);
res[i]=high+1;
fen.update(high,-(high+1));
tree.remove(high);
}
}
for(int i:res){
out.print(i+" ");
}
}
class FenWick {
int tree[];//1-index based
int A[];
int arr[];
public FenWick(int[] A) {
this.A=A;
arr=new int[A.length];
tree=new int[A.length+1];
int sum=0;
for(int i=0;i<A.length;i++){
update(i,A[i]);
}
}
public void update(int i, int val) {
arr[i]=+val;
i++;
while(i<tree.length){
tree[i]+=val;
i+=(i&-i);
}
}
public long sumRange(int i, int j) {
return pre(j+1)-pre(i);
}
public long pre(int i){
long sum=0;
while(i>0){
sum+=tree[i];
i-=(i&-i);
}
return sum;
}
}
}
/*
;\
|' \
_ ; : ;
/ `-. /: : |
| ,-.`-. ,': : |
\ : `. `. ,'-. : |
\ ; ; `-.__,' `-.|
\ ; ; ::: ,::'`:. `.
\ `-. : ` :. `. \
\ \ , ; ,: (\
\ :., :. ,'o)): ` `-.
,/,' ;' ,::"'`.`---' `. `-._
,/ : ; '" `;' ,--`.
;/ :; ; ,:' ( ,:)
,.,:. ; ,:., ,-._ `. \""'/
'::' `:'` ,'( \`._____.-'"'
;, ; `. `. `._`-. \\
;:. ;: `-._`-.\ \`.
'`:. : |' `. `\ ) \
-hrr- ` ;: | `--\__,'
'` ,'
,-'
free bug dog
*/
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.Map.Entry;
public class gym{
static class pair implements Comparable<pair>{
long n;int idx;
pair(long x,int y){
n=x;idx=y;
}
@Override
public int compareTo(pair o) {
int me=Arrays.binarySearch(nums, n);
int oo=Arrays.binarySearch(nums, o.n);
if(me!=oo) {
if(n>o.n)return 1;
return -1;
}
return idx-o.idx;
}
}
static long []nums;
public static void main(String[] args) throws Exception{
MScanner sc = new MScanner(System.in);
PrintWriter pw=new PrintWriter(System.out);
nums=new long[(int)2e5+7];
for(int i=1;i<nums.length;i++) {
nums[i]=(i*1l*(i-1))/2;
}
int n=sc.nextInt();
TreeSet<pair>t=new TreeSet<pair>();
for(int i=0;i<n;i++) {
t.add(new pair(sc.nextLong(),i));
}
int[]ans=new int[n];
for(int i=1;i<=n;i++) {
pair p=t.floor(new pair(((i*1l*(i-1)))/2,n));
t.remove(p);
ans[p.idx]=i;
}
for(int i:ans)pw.print(i+" ");
pw.flush();
}
static class MScanner {
StringTokenizer st;
BufferedReader br;
public MScanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public MScanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int MAXN = 2e5 + 5;
const int inf = 0x3f3f3f3f;
long long s[MAXN];
struct node {
int l, r;
long long sum;
} node[MAXN << 2];
void build(int l, int r, int num) {
node[num].l = l;
node[num].r = r;
if (l == r) {
node[num].sum = l;
return;
}
int mid = (l + r) >> 1;
build(l, mid, num << 1);
build(mid + 1, r, num << 1 | 1);
node[num].sum = node[num << 1].sum + node[num << 1 | 1].sum;
}
void updata(int pos, int num) {
if (node[num].l == node[num].r) {
node[num].sum = 0;
return;
}
int mid = (node[num].l + node[num].r) >> 1;
if (pos <= mid)
updata(pos, num << 1);
else
updata(pos, num << 1 | 1);
node[num].sum = node[num << 1 | 1].sum + node[num << 1].sum;
}
int query(long long val, int num) {
if (node[num].l == node[num].r) {
return node[num].l;
}
long long sum1 = node[num << 1].sum;
if (sum1 <= val)
return query(val - sum1, num << 1 | 1);
else
return query(val, num << 1);
}
int dp[MAXN];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
build(1, n, 1);
cout << node[1].sum << endl;
for (int i = n; i >= 1; i--) {
int ans = query(s[i], 1);
dp[i] = ans;
updata(ans, 1);
}
for (int i = 1; i <= n; i++) {
cout << dp[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;
vector<long long int> gv;
vector<long long int> pre;
vector<long long int> fv;
long long int n;
int create() {
fv.resize(n + 1, 0);
for (int i = 1; i <= pre.size(); i++) {
int par = i & (i - 1);
int val;
if (par == 0) {
val = pre[i - 1];
} else {
val = pre[i - 1] - pre[par - 1];
}
fv[i] = val;
}
return 0;
}
long long int getans(long long int num) {
long long int ans = 0;
while (num != 0) {
ans += (fv[num]);
num = num & (num - 1);
}
return ans;
}
long long int update(long long int pos, long long int val) {
long long int prev = gv[pos];
pos++;
unsigned long long int next = pos;
while (next < fv.size()) {
fv[next] = fv[next] - prev + val;
unsigned long long int a = 1;
next = next + (next & ((~next) + a));
}
return 0;
}
int main() {
cin >> n;
vector<long long int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (long long int i = 1; i <= n; i++) {
gv.push_back(i);
pre.push_back(i);
if (i > 1) {
pre[i - 1] = pre[i - 2] + pre[i - 1];
}
}
create();
vector<long long int> ansv;
for (int i = n - 1; i >= 0; i--) {
long long int sum = arr[i];
long long int beg = 1, end = n;
long long int mid;
long long int cans = -1;
while (beg <= end) {
mid = (beg + end) / 2;
long long int fsum = getans(mid - 1);
if (fsum <= sum) {
beg = mid + 1;
cans = max(mid, cans);
} else {
end = mid - 1;
}
}
ansv.push_back(cans);
update(cans - 1, 0);
}
for (int i = n - 1; i >= 0; i--) {
cout << ansv[i] << " ";
}
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
n=input()
n=int(n)
l=list(map(int,input().split()))
ind_rep=[0]*(n+1)
ans=[0]*n
suma={}
d={}
suma[0]=l[0]
for i in range(1,n):
suma[i]=suma[i-1]+l[i]
if l[i]<=suma[i-1]:
ind_rep[i]=1
#print(ind_rep,suma)
j=0
k=0
for i in range(n):
if i>=j+k:
#print(i,j+k)
if ind_rep[i]==1:
k=0
j=i
while(ind_rep[i]!=0 and i<n):
k+=1
i+=1
d[j]=k
ch=k
#print(i,k)
else:
ch-=2
d[i]=ch
#print(d)
for i in range(n):
d[i]=d.get(i,-1)
if d[i]==-1:
ans[i]=i+1
else:
ans[i-1]=d[i]+i
#print(ans)
for i in range(n):
if ans[i]==0:
ans[i]=ans[i-1]-1
print(ans[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
|
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 class Pair{
int sum,index;
Pair(int sum,int index){
this.sum=sum;
this.index=index;
}
}
public static void main(String[] args) {
sc = new InputReader(System.in);
wc = new PrintWriter(System.out);
int n=sc.nextInt();
int arr[]=new int[n],res[]=new int[n],i;
PriorityQueue<Pair> p=new PriorityQueue<>(new Comparator<Pair>(){
public int compare(Pair p1,Pair p2){
if(p1.sum==p2.sum){
if(p1.index==p2.index) return 0;
return p1.index>p2.index ? -1:1;
}
return p1.sum>p2.sum ? 1:-1;
}
});
for(i=0;i<n;i++){
arr[i]=sc.nextInt();
p.add(new Pair(arr[i],i));
}
i=1;
while(!p.isEmpty()){
Pair out=p.remove();
res[out.index]=i++;
}
for(i=0;i<n;i++)
wc.print(res[i]+" ");
wc.println();
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;
vector<long long int> BIT(200005);
void update(long long x, long long val) {
for (; x <= 200005; x += (x & -x)) BIT[x] += val;
}
long long query(long long x) {
long long res = 0;
for (; x > 0; x -= (x & -x)) res += BIT[x];
return res;
}
long long check(long long x) {
long long val = (x * (x - 1)) / 2;
val -= query(x);
return val;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, i, j;
cin >> n;
long long a[n + 1];
vector<long long int> v;
for (i = 1; i <= n; i++) cin >> a[i];
for (i = n; i > 0; i--) {
long long lo = 1, hi = n, mid;
while (hi - lo > 1) {
mid = (hi + lo) / 2;
if (check(mid) >= a[i])
hi = mid;
else
lo = mid;
}
long long ans;
if (check(hi))
ans = hi;
else
ans = lo;
v.push_back(ans);
update(ans, ans);
}
reverse((v).begin(), (v).end());
for (auto i : v) cout << 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;
struct BIT {
int n, N_MAX;
vector<long long> v;
BIT(int n) {
this->n = n + 100;
N_MAX = n;
v.assign(n + 110, 0);
}
void upd(int p, int x) {
while (p <= n) v[p] += x, p += p & -p;
}
long long que(int p) {
long long ans = 0;
while (p) ans += v[p], p -= p & -p;
return ans;
}
long long quep(int p) { return que(p) - que(p - 1); }
long long bit_search(long long s) {
long long sum = 0, pos = 0;
for (int i = 20; i >= 0; i--)
if (pos + (1 << i) < N_MAX && sum + v[pos + (1 << i)] < s) {
sum += v[pos + (1 << i)];
pos += (1 << i);
}
return pos + 1;
}
};
int main() {
int n;
scanf("%d", &n);
BIT bit(n);
vector<int> v(n + 1), ans(n + 1);
for (int i = 1; i <= n; i++) scanf("%d", &v[i]), bit.upd(i, i);
for (int i = n; i; i--) {
int p = bit.bit_search(v[i] + 1);
ans[i] = p;
bit.upd(p, -p);
}
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
printf("\n");
return 0;
}
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
python3
|
n = int(input())
l = list(map(int,input().split()))
import heapq
pos = [(x,-i) for i,x in enumerate(l)]
heapq.heapify(pos)
out = [_ for _ in range(n)]
current = 1
for x in range(n):
i = pos[x][1] * (-1)
out[i] = x+1
print (*out)
|
1208_D. Restore Permutation
|
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation.
The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
|
{
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
}
|
{
"input": [
"100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n",
"20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n",
"1\n0\n",
"15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n"
],
"output": [
"94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ",
"1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ",
"1 ",
"2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 "
]
}
|
IN-CORRECT
|
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];
//System.out.println(nodeId+" "+tree[nodeId].value);
}
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 -= value;
return;
}
int left = nodeId * 2;
int right = nodeId * 2 + 1;
int mid = (i + j) / 2;
update(left, i, mid, b, e, value);
update(right, mid + 1, j, b, e, value);
tree[nodeId] = getMin(tree[left], tree[right]);
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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long myRandom(long long L, long long R) {
uniform_int_distribution<mt19937_64::result_type> work(L, R);
return work(rng);
}
template <class T>
class Fenwick {
private:
int MX;
vector<T> t;
public:
void updateCell(int idx, T val) {
while (idx < MX) {
t[idx] += val;
idx += (idx & -idx);
}
}
T readCumulative(int idx) {
T ret = 0;
while (idx > 0) {
ret += t[idx];
idx -= (idx & -idx);
}
return ret;
}
T getRangeSum(int l, int r) {
return readCumulative(r) - readCumulative(l - 1);
}
T readValue(int idx) { return getRangeSum(idx, idx); }
public:
Fenwick(int n) {
MX = n + 7;
t = vector<T>(MX);
}
Fenwick(int n, vector<T> &val) {
MX = n + 7;
t = vector<T>(MX);
for (int i = 0; i < n; i++) updateCell(val[i]);
}
};
inline long long getSum(long long n) { return n * (n + 1) / 2; }
long long getCost(long long n, Fenwick<long long> &tree) {
if (n == 1) return 0;
return getSum(n - 1) - tree.readCumulative(n - 1);
}
vector<long long> restorePermutation(int n, vector<long long> s) {
vector<long long> ans(n, -1);
Fenwick<long long> tree(n);
for (int i = n - 1; i >= 0; i--) {
long long goal = s[i];
int L = 1;
int R = n;
while (L < R) {
if (L + 1 == R) {
if (getCost(R, tree) == goal)
L = R;
else
R = L;
} else {
int M = L + (R - L) / 2;
if (getCost(M, tree) <= goal)
L = M;
else
R = M - 1;
}
}
ans[i] = L;
tree.updateCell(L, L);
}
return ans;
}
bool localTest(int n) {
vector<long long> p(n);
for (int i = 0; i < n; i++) p[i] = i + 1;
shuffle(p.begin(), p.end(), rng);
vector<long long> s(n, 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (p[j] < p[i]) s[i] += p[j];
}
}
vector<long long> ans = restorePermutation(n, s);
if (ans == p) return true;
fprintf(stderr, "Wrong Answer\n");
fprintf(stderr, "Original: ");
for (int i = 0; i < n; i++) fprintf(stderr, "%I64d ", p[i]);
fprintf(stderr, "\n");
fprintf(stderr, "Output : ");
for (int i = 0; i < n; i++) fprintf(stderr, "%I64d ", ans[i]);
fprintf(stderr, "\n");
fprintf(stderr, "Information: ");
for (int i = 0; i < n; i++) fprintf(stderr, "%I64d ", s[i]);
fprintf(stderr, "\n");
return false;
}
bool localTest2(int n) {
vector<long long> p(n);
for (int i = 0; i < n; i++) p[i] = i + 1;
shuffle(p.begin(), p.end(), rng);
Fenwick<long long> tree(n);
vector<long long> s(n, 0);
for (int i = 0; i < n; i++) {
s[i] = tree.readCumulative(p[i]);
tree.updateCell(p[i], p[i]);
}
vector<long long> ans = restorePermutation(n, s);
if (ans == p) return true;
fprintf(stderr, "Wrong Answer\n");
fprintf(stderr, "Original: ");
for (int i = 0; i < n; i++) fprintf(stderr, "%I64d ", p[i]);
fprintf(stderr, "\n");
fprintf(stderr, "Output : ");
for (int i = 0; i < n; i++) fprintf(stderr, "%I64d ", ans[i]);
fprintf(stderr, "\n");
fprintf(stderr, "Information: ");
for (int i = 0; i < n; i++) fprintf(stderr, "%I64d ", s[i]);
fprintf(stderr, "\n");
return false;
}
int testRun() {
int tc = 100;
while (tc--) {
if (!localTest2(100000)) return -1;
}
fprintf(stderr, "All tests passed\n");
return 0;
}
int testRun2() {
int n = 500;
vector<long long> s(n, 0);
s[n - 1] = 1;
vector<long long> a = restorePermutation(n, s);
for (int i = 0; i < n; i++) printf("%I64d\n", a[i]);
return 0;
}
int main() {
int n;
scanf("%d", &n);
vector<long long> s(n);
for (int i = 0; i < n; i++) scanf("%d", &s[i]);
vector<long long> ans = restorePermutation(n, s);
for (int i = 0; i < n; i++) printf("%I64d ", 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;
bool compare1(pair<int, int> a1, pair<int, int> a2) {
double aa = ((double)a1.first) / a1.second;
double bb = ((double)a2.first) / a2.second;
return aa > bb;
}
long long min(long long a, long long b) {
if (a > b) return b;
return a;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long max(long long a, long long b) {
if (a > b) return a;
return b;
}
int find(int parent[], int i) {
if (parent[i] != i) {
parent[i] = find(parent, parent[i]);
}
return parent[i];
}
void unions(int parent[], int i, int j) {
long long k = find(parent, i);
long long l = find(parent, j);
parent[k] = l;
}
int tree[600001][2] = {999999};
int lazy[600001] = {0};
void update(int si, int ss, int se, int us, int ue, int diff) {
if (lazy[si] != 0) {
tree[si][1] -= lazy[si];
if (ss != se) {
lazy[si * 2 + 1] += lazy[si];
lazy[si * 2 + 2] += lazy[si];
}
lazy[si] = 0;
}
if (ss > se || ss > ue || se < us) return;
if (ss >= us && se <= ue) {
tree[si][1] -= diff;
if (ss != se) {
lazy[si * 2 + 1] += diff;
lazy[si * 2 + 2] += diff;
}
return;
}
int mid = (ss + se) / 2;
update(si * 2 + 1, ss, mid, us, ue, diff);
update(si * 2 + 2, mid + 1, se, us, ue, diff);
if (tree[2 * si + 1][1] < tree[2 * si + 2][1])
tree[si][0] = tree[si * 2 + 1][0];
else
tree[si][0] = tree[si * 2 + 2][0];
tree[si][1] = min(tree[2 * si + 1][1], tree[2 * si + 2][1]);
}
void update(int si, int ss, int se, int us, int ue, int diff, int k) {
if (lazy[si] != 0) {
tree[si][1] -= lazy[si];
if (ss != se) {
lazy[si * 2 + 1] += lazy[si];
lazy[si * 2 + 2] += lazy[si];
}
lazy[si] = 0;
}
if (ss > se || ss > ue || se < us) return;
if (ss >= us && se <= ue) {
tree[si][1] = diff;
tree[si][0] = k;
if (ss != se) {
lazy[si * 2 + 1] += diff;
lazy[si * 2 + 2] += diff;
}
return;
}
int mid = (ss + se) / 2;
update(si * 2 + 1, ss, mid, us, ue, diff, k);
update(si * 2 + 2, mid + 1, se, us, ue, diff, k);
if (tree[2 * si + 1][1] < tree[2 * si + 2][1])
tree[si][0] = tree[si * 2 + 1][0];
else
tree[si][0] = tree[si * 2 + 2][0];
tree[si][1] = min(tree[2 * si + 1][1], tree[2 * si + 2][1]);
}
pair<int, int> getsum(int ss, int se, int qs, int qe, int si) {
if (lazy[si] != 0) {
tree[si][1] -= lazy[si];
if (ss != se) {
lazy[si * 2 + 1] += lazy[si];
lazy[si * 2 + 2] += lazy[si];
}
lazy[si] = 0;
}
if (ss > se || ss > qe || se < qs) return make_pair(-1, 999999);
if (ss >= qs && se <= qe) return make_pair(tree[si][0], tree[si][1]);
int mid = (ss + se) / 2;
pair<int, int> a1 = getsum(ss, mid, qs, qe, 2 * si + 1);
pair<int, int> a2 = getsum(mid + 1, se, qs, qe, 2 * si + 2);
if (a1.second < a2.second)
return (a1);
else
return a2;
}
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
int c;
cin >> c;
update(0, 0, n - 1, i, i, c, i);
}
int ans[n + 1];
for (int i = 0; i < n; i++) {
int kk = getsum(0, n - 1, 0, n - 1, 0).first;
update(0, 0, n - 1, kk, kk, 999999, i);
update(0, 0, n - 1, kk + 1, n - 1, i + 1);
ans[kk] = i + 1;
}
for (int i = 0; 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
|
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] = max(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] = max(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 max(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(abs(cur.first) == 2e9) {
cur = sum(0, 0, n - 1, st1 + 1, r);
}
//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;
findAns(0, n - 1, st);
rep(i, 0, n) cout << ans[i] << " ";
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.