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; const int MAXN = 2e5 + 5; int n, ans[MAXN], mark[MAXN]; long long s[MAXN], tree[MAXN]; void add(int x, int val) { for (; x <= n; x += x & -x) tree[x] += val; } long long ask(int x) { long long res = 0; for (; x; x &= x - 1) res += tree[x]; return res; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 0; i < n; ++i) cin >> s[i]; for (int i = n - 1; ~i; --i) { int l = 1, r = n, ans = -1; while (l <= r) { int mid = (l + r) >> 1; if (1LL * mid * (mid - 1) / 2 - ask(mid - 1) <= s[i]) { ans = mid; l = mid + 1; } else { r = mid - 1; } } if (ans < 0) exit(-1); ::ans[i] = ans; add(ans, ans); } 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 " ] }
CORRECT
cpp
#include <bits/stdc++.h> std::vector<long long> a; const long long INF = 1000000000000000000LL; int lm = -1; std::vector<long long> ts; long long fill(int l, int r, int i) { if (l == r) { ts[i] = a[l]; return a[l]; } int m = (l + r) / 2; ts[i] = fill(l, m, i * 2) + fill(m + 1, r, i * 2 + 1); return ts[i]; } void init(int n) { ts.assign(4 * n, 0); fill(0, n - 1, 1); } long long getSum(int l, int r, int cl, int cr, int i) { if (l == cl && r == cr) return ts[i]; int cm = (cl + cr) / 2; if (r <= cm) return getSum(l, r, cl, cm, i * 2); if (cm < l) return getSum(l, r, cm + 1, cr, i * 2 + 1); return getSum(l, cm, cl, cm, i * 2) + getSum(cm + 1, r, cm + 1, cr, i * 2 + 1); } void set(int cl, int cr, int i, int ai, int val) { if (cl == ai && cr == ai) { ts[i] = a[ai] = val; return; } int cm = (cl + cr) / 2; if (ai <= cm) set(cl, cm, i * 2, ai, val); else set(cm + 1, cr, i * 2 + 1, ai, val); ts[i] = ts[i * 2] + ts[i * 2 + 1]; } int main() { int n; scanf("%d", &n); std::vector<long long> s(n); for (long long& x : s) scanf("%I64d", &x); a.assign(1 + n, 0); for (int i = 0; i < (int)a.size(); ++i) a[i] = i; init(a.size()); std::vector<int> ans(n); for (int i = n - 1; i >= 0; --i) { int l = 0, r = n + 1; while (r > l + 1) { int m = (l + r) / 2; long long sum = getSum(0, m, 0, n, 1); if (sum > s[i]) r = m; else l = m; } ans[i] = l + 1; set(0, n, 1, l + 1, 0); } for (int x : ans) printf("%d ", 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 A[300001]; long long tree[300001 * 4 + 2], prop[4 * 300001 + 2], ans[300001]; void create(long long node, long long b, long long e) { if (b == e) { tree[node] = A[b]; return; } long long l = 2 * node; long long r = l + 1; long long m = (b + e) / 2; create(l, b, m); create(r, m + 1, e); tree[node] = min(tree[l], tree[r]); } void update(long long node, long long b, long long e, long long i, long long j, long long val) { if (b > j || e < i) return; if (i <= b && e <= j) { if (val == ((long long)1 << 62)) { tree[node] = ((long long)1 << 62); return; } prop[node] += val; tree[node] -= val; return; } long long l = 2 * node; long long r = l + 1; long long m = (b + e) / 2; if (prop[node]) { prop[l] += prop[node]; prop[r] += prop[node]; tree[l] = -prop[node]; tree[r] -= prop[node]; prop[node] = 0; } update(l, b, m, i, j, val); update(r, m + 1, e, i, j, val); tree[node] = min(tree[l], tree[r]); } long long query(long long node, long long b, long long e) { if (b == e) return b; long long l = 2 * node; long long r = l + 1; long long m = (b + e) / 2; if (prop[node]) { prop[l] += prop[node]; prop[r] += prop[node]; tree[l] -= prop[node]; tree[r] -= prop[node]; prop[node] = 0; } if (tree[r] == 0) return query(r, m + 1, e); else return query(l, b, m); } int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); memset(prop, 0, sizeof prop); ; long long n; cin >> n; for (int i = 1; i <= n; i++) cin >> A[i]; create(1, 1, n); for (int i = 1; i <= n; i++) { long long p = query(1, 1, n); ans[p] = i; update(1, 1, n, p, p, ((long long)1 << 62)); update(1, 1, n, p + 1, n, i); } for (int 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
// 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; } } 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 { long tree[];//1-index based int A[]; int arr[]; public FenWick(int[] A) { this.A=A; arr=new int[A.length]; tree=new long[A.length+1]; 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 " ] }
CORRECT
java
/** * Created at 23:31 on 2019-08-25 */ import java.io.*; import java.util.*; import java.util.function.Predicate; public class Main { static FastScanner sc = new FastScanner(); static Output out = new Output(System.out); static final int[] dx = {0, 1, 0, -1}; static final int[] dy = {-1, 0, 1, 0}; static final long MOD = (long) (1e9 + 7); static final long INF = Long.MAX_VALUE / 2; public static class Solver { public Solver() { int N = sc.nextInt(); long[] s = sc.nextLongArray(N, false); long[] temp = new long[N]; for (int i=0; i<N; i++) { temp[i] = i+1; } BinaryIndexedTree bit = new BinaryIndexedTree(temp); long[] ans = new long[N]; for (int i=N-1; i>=0; i--){ long sum = s[i]; int index = BinarySearch.binarySearch(j -> bit.sum(j) > sum, 0, N, true); ans[i] = index; bit.add(index, -index); } out.print(ans, " "); } public static class BinarySearch { /* * [l, r)の区間でchecker.test()を実行する * rightOK ... 右側OK左側NGならtrue, 右側NG左側OKならfalse */ public static long binarySearchLong(Predicate<Long> checker, long l, long r, boolean rightOK) { long ng, ok; if (rightOK) { ok = r-1; ng = l; if (!checker.test(ok)) { return r; } if (checker.test(ng)) { return l; } } else { ok = l; ng = r-1; if (!checker.test(ok)) { return l-1; } if (checker.test(ng)) { return r-1; } } /* ok と ng のどちらが大きいかわからないことを考慮 */ while (Math.abs(ok - ng) > 1) { long mid = (ok + ng) / 2; if (checker.test(mid)) ok = mid; else ng = mid; } return ok; } /* * [l, r)の区間でchecker.test()を実行する */ public static int binarySearch(Predicate<Integer> checker, int l, int r, boolean rightOK) { int ng, ok; if (rightOK) { ok = r-1; ng = l; if (!checker.test(ok)) { return r; } if (checker.test(ng)) { return l; } } else { ok = l; ng = r-1; if (!checker.test(ok)) { return l-1; } if (checker.test(ng)) { return r-1; } } while (Math.abs(ok - ng) > 1) { int mid = (ok + ng) / 2; if (checker.test(mid)) ok = mid; else ng = mid; } return ok; } public static int lowerBound(long[] a, long v) { if (a[0] >= v) return 0; int l = 0, r = a.length; int mid = (r + l) / 2; while (r - l > 1) { if (a[mid] >= v) r = mid; else l = mid; mid = (r + l) / 2; } return r; } public static int upperBound(long[] a, long v) { if (a[0] > v) return 0; int l = 0, r = a.length; int mid = (r + l) / 2; while (r - l > 1) { if (a[mid] > v) r = mid; else l = mid; mid = (r + l) / 2; } return r; } public static int count(long[] a, long v) { int lower = lowerBound(a, v); int upper = upperBound(a, v); return upper - lower; } } public class BinaryIndexedTree { int oneBased = 0; int N; long[] tree; public BinaryIndexedTree(long[] A) { this.N = A.length; tree = new long[N+1]; for (int i=1; i<=N; i++) { add(i, A[i-1]); } } public void add(int i, long x) { i += 1-oneBased; while(i <= N) { tree[i] += x; i += i & (-i); } } public long sum(int i) { i += 1-oneBased; long res = 0; while(i > 0) { res += tree[i]; i -= i & (-i); } return res; } public long sum(int from, int to) { return sum(to) - sum(from); } } } public static void main(String[] args) { new Solver(); out.flush(); } static class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } private void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; } public boolean hasNext() { skipUnprintable(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { return (int) nextLong(); } public int[] nextIntArray(int N, boolean oneBased) { if (oneBased) { int[] array = new int[N + 1]; for (int i = 1; i <= N; i++) { array[i] = sc.nextInt(); } return array; } else { int[] array = new int[N]; for (int i = 0; i < N; i++) { array[i] = sc.nextInt(); } return array; } } public long[] nextLongArray(int N, boolean oneBased) { if (oneBased) { long[] array = new long[N + 1]; for (int i = 1; i <= N; i++) { array[i] = sc.nextLong(); } return array; } else { long[] array = new long[N]; for (int i = 0; i < N; i++) { array[i] = sc.nextLong(); } return array; } } } static class Output extends PrintWriter { public Output(PrintStream ps) { super(ps); } public void print(int[] a, String separator) { for (int i = 0; i < a.length; i++) { if (i == 0) print(a[i]); else print(separator + a[i]); } println(); } public void print(long[] a, String separator) { for (int i = 0; i < a.length; i++) { if (i == 0) print(a[i]); else print(separator + a[i]); } println(); } public void print(String[] a, String separator) { for (int i = 0; i < a.length; i++) { if (i == 0) print(a[i]); else print(separator + a[i]); } println(); } public void print(ArrayList a, String separator) { for (int i = 0; i < a.size(); i++) { if (i == 0) print(a.get(i)); else print(separator + a.get(i)); } println(); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using LL = long long; using PII = pair<int, int>; template <class T> struct Fenwick { vector<T> v; Fenwick(size_t n) : v(n + 1) {} void add(size_t i, T x) { for (++i; i < v.size(); i += i & -i) v[i] += x; } T sum(size_t i) { for (v[0] = T(); i; i -= i & -i) v[0] += v.at(i); return v[0]; } T sum(size_t l, size_t r) { return sum(r) - sum(l); } }; const int N = 2e5 + 5; int n, a[N]; LL s[N]; int main() { cin >> n; Fenwick<LL> fw(N); for (int i = (1); i <= (n); ++i) scanf("%lld", &s[i]), fw.add(i, i); for (int i = n; i >= 1; i--) { int l = 0, r = n; while (l + 1 < r) { int m = (l + r) >> 1; LL sum = fw.sum(m + 1); if (sum <= s[i]) l = m; else r = m; } a[i] = l + 1; fw.add(l + 1, -(l + 1)); } for (int i = 1; i <= n; i++) printf("%d%c", a[i], " \n"[i == n]); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
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 - 1; 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 = 21; i >= 0; i--) if (pos + (1 << i) <= N_MAX && sum + v[pos + (1 << i)] < s) { pos += (1 << i); sum += v[pos]; } return pos + 1; } }; int main() { int n; scanf("%d", &n); BIT bit(n); vector<long long> v(n + 1), ans(n + 1); for (int i = 1; i <= n; i++) scanf("%lld", &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("%lld ", ans[i]); printf("\n"); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void rset(); void init_test(); void solve(); signed main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed; cout.precision(20); init_test(); return 0; } template <typename T> void chmin(T& a, T b) { if (a > b) a = b; } template <typename T> void chmax(T& a, T b) { if (a < b) a = b; } template <typename T> void MACRO_rdv2_Init(long long n, T& t) { t.resize(n); } template <typename First, typename... Rest> void MACRO_rdv2_Init(long long n, First& first, Rest&... rest) { first.resize(n); MACRO_rdv2_Init(n, rest...); } template <typename T> void MACRO_rdv2_Scan(long long p, T& t) { std::cin >> t[p]; } template <typename First, typename... Rest> void MACRO_rdv2_Scan(long long p, First& first, Rest&... rest) { std::cin >> first[p]; MACRO_rdv2_Scan(p, rest...); } template <typename T> void wrv(const vector<T>& v) { for (long long(__ii) = (0); (__ii) < (((long long)v.size())); ++(__ii)) { if (__ii) cout << ' '; cout << v[__ii]; } cout << '\n'; } template <typename T> void wrm(const vector<vector<T>>& v) { for (long long(__ii) = (0); (__ii) < (((long long)v.size())); ++(__ii)) { for (long long(__jj) = (0); (__jj) < (v[__ii].size()); ++(__jj)) { if (__jj) cout << ' '; cout << v[__ii][__jj]; } cout << '\n'; } } template <typename T> void sc(T& x) { cin >> x; } template <typename Head, typename... Tail> void sc(Head& head, Tail&... tail) { cin >> head; sc(tail...); } template <typename T> void wr(const T& x) { cout << x << '\n'; } template <typename Head, typename... Tail> void wr(const Head& head, const Tail&... tail) { cout << head << ' '; wr(tail...); } template <typename T> void wrf(const T& x) { cout << x << endl; } template <typename Head, typename... Tail> void wrf(const Head& head, const Tail&... tail) { cout << head << ' '; wrf(tail...); } template <typename T> void debug_out(const T& x) { cerr << x << '\n'; } template <typename Head, typename... Tail> void debug_out(const Head& head, const Tail&... tail) { cerr << head << ' '; debug_out(tail...); } template <typename... T> void err(const T&... cod) { wr(cod...); exit(0); } const long long N = 2e5 + 10; const long long inf = 1e12; long long st_mn, st_mx; class segtree { private: struct node { long long mn, mx; node(long long mn = inf, long long mx = -inf) : mn(mn), mx(mx){}; node operator+(const node& rhs) const { node ans; ans.mn = min(mn, rhs.mn); ans.mx = max(mx, rhs.mx); return ans; } }; long long n, ql, qr; vector<node> st; vector<long long> lazy; void push(long long sl, long long sr, long long at) { if (lazy[at] == 0) return; st[at].mn += lazy[at]; st[at].mx += lazy[at]; if (sl != sr) { lazy[at << 1] += lazy[at]; lazy[at << 1 | 1] += lazy[at]; } lazy[at] = 0; } void add_recur(long long sl, long long sr, long long at, long long delta) { push(sl, sr, at); if (qr < sl || ql > sr) return; if (ql <= sl && qr >= sr) { lazy[at] += delta; push(sl, sr, at); return; } long long mid = (sl + sr) >> 1; long long le = at << 1, ri = at << 1 | 1; add_recur(sl, mid, le, delta); add_recur(mid + 1, sr, ri, delta); st[at] = st[le] + st[ri]; } node get_recur(long long sl, long long sr, long long at) { push(sl, sr, at); if (qr < sl || ql > sr) return node(); if (ql <= sl && qr >= sr) return st[at]; long long mid = (sl + sr) >> 1; return get_recur(sl, mid, at << 1) + get_recur(mid + 1, sr, at << 1 | 1); } public: segtree() {} segtree(long long _n) { init(_n); } void init(long long _n) { this->n = _n; st.assign(n << 2, node(0, 0)); lazy.assign(n << 2, 0); } void add(long long l, long long r, long long delta) { ql = l, qr = r; if (l > r) return; add_recur(0, n - 1, 1, delta); } void get(long long l, long long r) { ql = l, qr = r; node tmp = get_recur(0, n - 1, 1); st_mn = tmp.mn, st_mx = tmp.mx; } }; long long find_zero(long long n, segtree* st) { long long low = 0, high = n - 1; while (low < high) { long long mid = (low + high + 1) / 2; st->get(mid, high); if (st_mn == 0) low = mid; else high = mid - 1; } return low; } void solve() { long long n; sc(n); vector<long long> a(n); for (long long(__ii) = (0); (__ii) < (n); ++(__ii)) cin >> a[__ii]; segtree* st = new segtree(n); for (long long(i) = (0); (i) < (n); ++(i)) st->add(i, i, a[i]); vector<long long> ans(n); for (long long(i) = (1); (i) < (n + 1); ++(i)) { long long at = find_zero(n, st); ans[at] = i; st->add(at, at, inf); st->add(at + 1, n - 1, -i); } wrv(ans); } void init_test() { long long qq = 1; while (qq--) 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
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, b[N]; long long BIT[N], a[N]; int lowbit(int x) { return x & (-x); } void Add(int x, int y) { while (x <= n) { BIT[x] += y; x += lowbit(x); } } long long Sum(int x) { long long ans = 0; while (x) { ans += BIT[x]; x -= lowbit(x); } return ans; } int main() { scanf("%d", &n); for (register int i = 1; i <= n; i++) { scanf("%I64d", &a[i]); } for (register int i = 1; i <= n; i++) Add(i, i); for (register int i = n; i >= 1; i--) { int l = 1, r = n; while (l <= r) { int mid = l + r >> 1; long long w = Sum(mid - 1); if (w <= a[i]) l = mid + 1; else r = mid - 1; } b[i] = r; Add(r, -r); } for (register int i = 1; i <= n; i++) printf("%d ", b[i]); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long int bit[200005]; int n; void update(int j, int x) { for (; j < 200005; j += j & (-j)) bit[j] += x; } long long int query(int j) { if (j == 0) return 0; long long int x = 0; for (; j > 0; j -= (j) & (-j)) x += bit[j]; return x; } int next_ele(long long int s) { int l, r, m; long long int q; l = 0, r = n; while (l < r - 1) { m = (l + r) / 2; q = query(m); if (q < s) l = m + 1; if (q > s) r = m - 1; if (q == s) l = m; } m = l; if (query(r) == s) m = r; return m + 1; } int compute(long long int s) { int x; s *= 2; x = sqrt(s); return x + 1; } int main() { int i; scanf("%d", &n); vector<long long int> sum(n); for (i = 0; i < n; i++) cin >> sum[i]; vector<int> ans(n); ans[n - 1] = compute(sum[n - 1]); for (i = 1; i < n + 1; i++) update(i, i); update(ans[n - 1], -ans[n - 1]); for (i = n - 2; i >= 0; i--) { ans[i] = next_ele(sum[i]); update(ans[i], -ans[i]); } for (i = 0; i < n; i++) printf("%d ", ans[i]); printf("\n"); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.util.Arrays; import java.util.StringJoiner; import java.util.StringTokenizer; import java.util.function.Function; public class MainD { static int N; static long[] A; public static void main(String[] args) { FastScanner sc = new FastScanner(System.in); N = sc.nextInt(); A = sc.nextLongArray(N); System.out.println(mkString(solve())); } static String mkString(int[] arr) { StringJoiner j = new StringJoiner(" "); for (int a : arr) { j.add(String.valueOf(a)); } return j.toString(); } static int[] solve() { FenwickTree bit = new FenwickTree(N, FenwickTree.SUM); for (int i = 0; i < N; i++) { bit.add(i, i+1); } int[] ans = new int[N]; for (int i = N-1; i >= 0; i--) { int idx = bit.lowerBound(A[i]+1); ans[i] = idx+1; bit.add(ans[i]-1, -ans[i]); // set 0 } return ans; } static class FenwickTree { interface Monoid { long identity(); long apply(long a, long b); long inverse(long a); } static Monoid SUM = new Monoid() { public long identity() { return 0; } public long apply(long a, long b) { return a+b; } public long inverse(long a) { return -a; } }; static Monoid MAX = new Monoid() { public long identity() { return 0; } public long apply(long a, long b) { return Math.max(a, b); } public long inverse(long a) { throw new RuntimeException("no inverse"); } }; int size; long[] bit; Monoid m; long identity; FenwickTree(int size, Monoid m) { this.size = 1; while( this.size < size ) this.size *= 2; this.bit = new long[this.size+1]; this.identity = m.identity(); if( this.identity != 0 ) { Arrays.fill(this.bit, this.identity); } this.m = m; } void add(int i, long v) { i++; // 0 index -> 1 index while( i <= size ) { bit[i] = m.apply(bit[i], v); i += i & -i; } } // [0, r) long query(int r) { // 0 index -> 1 index -> -1 long ret = identity; while(r > 0) { ret = m.apply(ret, bit[r]); r -= r & -r; } return ret; } long query(int l, int r) { return query(r) + m.inverse(query(l)); } int lowerBound(long v) { if( bit[size] < v ) return size; int x = 0; for (int k = size/2; k > 0; k /= 2 ) { if( bit[x + k] < v ) { v -= bit[x+k]; x += k; } } return x; } } static int lowerBound(long[] array, long value) { int low = 0; int high = array.length; int mid; while( low < high ) { mid = ((high - low) >>> 1) + low; // (high + low) / 2 if( array[mid] < value ) { low = mid + 1; } else { high = mid; } } return low; } @SuppressWarnings("unused") static class FastScanner { private BufferedReader reader; private StringTokenizer tokenizer; FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } long nextLong() { return Long.parseLong(next()); } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArray(int n, int delta) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt() + delta; return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } } static <A> void writeLines(A[] as, Function<A, String> f) { PrintWriter pw = new PrintWriter(System.out); for (A a : as) { pw.println(f.apply(a)); } pw.flush(); } static void writeLines(int[] as) { PrintWriter pw = new PrintWriter(System.out); for (int a : as) pw.println(a); pw.flush(); } static void writeLines(long[] as) { PrintWriter pw = new PrintWriter(System.out); for (long a : as) pw.println(a); pw.flush(); } static int max(int... as) { int max = Integer.MIN_VALUE; for (int a : as) max = Math.max(a, max); return max; } static int min(int... as) { int min = Integer.MAX_VALUE; for (int a : as) min = Math.min(a, min); return min; } static void debug(Object... args) { StringJoiner j = new StringJoiner(" "); for (Object arg : args) { if (arg instanceof int[]) j.add(Arrays.toString((int[]) arg)); else if (arg instanceof long[]) j.add(Arrays.toString((long[]) arg)); else if (arg instanceof double[]) j.add(Arrays.toString((double[]) arg)); else if (arg instanceof Object[]) j.add(Arrays.toString((Object[]) arg)); else j.add(arg.toString()); } System.err.println(j.toString()); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
/* If you want to aim high, aim high Don't let that studying and grades consume you Just live life young ****************************** If I'm the sun, you're the moon Because when I go up, you go down ******************************* I'm kinda bad Why am I using lazytree to update range when I can just update using BIT */ import java.util.*; import java.io.*; public class x1208D2 { public static void main(String args[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); long[] arr = new long[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Long.parseLong(st.nextToken()); //ah yes time to change color //find prefixes, update while bin searching HashSet<Integer> bin = new HashSet<Integer>(); for(int i=1; i <= N; i++) bin.add(i); FenwickTree sums = new FenwickTree(N+1); for(int i=1; i <= N; i++) sums.add(i, i); int[] res = new int[N]; for(int dex=N-1; dex >= 0; dex--) { //reverse order checks out int low = 1; int high = N; while(low != high) { int mid = (low+high)/2; long check = sums.query(0, mid-1); if(check == arr[dex] && bin.contains(mid)) { sums.add(mid, -1*mid); res[dex] = mid; bin.remove(mid); break; } else if(check == arr[dex]) low = mid+1; else if(check < arr[dex]) low = mid+1; else high = mid-1; } //guaranteed to have an answer if(res[dex] == 0) { int mid = (low+high)/2; long check = sums.query(0, mid-1); if(check == arr[dex] && bin.contains(mid)) { sums.add(mid, -1*mid); res[dex] = mid; bin.remove(mid); } } } StringBuilder sb = new StringBuilder(); for(int i=0; i < N; i++) sb.append(res[i]+" "); System.out.println(sb.toString()); } } class FenwickTree { //1 indexed public long[] tree; public int size; public FenwickTree(int size) { this.size = size; tree = new long[size+5]; } public void add(int i, int v) { while(i <= size) { tree[i] += v; i += i&-i; } } public long find(int i) { long res = 0L; while(i >= 1) { res += tree[i]; i -= i&-i; } return res; } public long query(int l, int r) { //inclusive range return find(r)-find(l-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
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+1) series = [0] + [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(1,N+1): bit[i] += series[i] y = i + lowbit(i) if y<=N: series[y] += series[i] visited = [0]*(N+1) ans = [0]*N for i in range(N-1,-1,-1): # find left = 1 right = N target = arr[i] while True: L = right - left + 1 num = left - 1 + 2**int(math.log(L,2)) q = bit[num] #print(num,q,target,left,right) if q<target: target -= q left = num + 1 elif q>target: right = num - 1 else: if visited[num]==1: target -= q left = num + 1 else: visited[num] = 1 ans[i] = num break # update update(num+1,-num) 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
python3
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
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n; long long sum[N], ans[N], cur; long long seg[1 << 20]; set<int> nw; void update(int i, int v, int ni = 0, int ns = 0, int ne = n) { if (ns > i || ne < i || ns > ne) return; if (ns == ne && ns == i) { seg[ni] += v; return; } if (ns >= ne) return; int l = ni * 2 + 1, r = l + 1, mid = (ns + ne) / 2; update(i, v, l, ns, mid); update(i, v, r, mid + 1, ne); seg[ni] = seg[l] + seg[r]; } long long query(int qs, int qe, int ni = 0, int ns = 0, int ne = n) { if (ns > qe || ne < qs || ns > ne) return 0; if (ns >= qs && ne <= qe) return seg[ni]; int l = ni * 2 + 1, r = l + 1, mid = (ns + ne) / 2; return query(qs, qe, l, ns, mid) + query(qs, qe, r, mid + 1, ne); } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%lld", sum + i); for (int i = 1; i <= n; ++i) nw.insert(i); for (int i = n - 1; i >= 0; --i) { int st = 0, en = n - 1; while (st < en) { int mid = (st + en + 1) / 2; long long s = 1ll * mid * (mid + 1) / 2 - query(0, mid); if (s > sum[i]) en = mid - 1; else st = mid; } int x = *nw.lower_bound(st + 1); nw.erase(x); ans[i] = x; update(x, x); } for (int i = 0; i < n; ++i) printf("%lld ", ans[i]); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.awt.*; import java.math.BigInteger; import java.util.*; public class Ada{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); SegmentTree s=new SegmentTree(n); for (int i=0;i<n;i++){ s.increment(i,i,sc.nextLong()); } int ans[]=new int[n]; for (int i=1;i<=n;i++){ s.minimum(0,n-1); int idx=s.idx[1]; ans[idx]=i; s.increment(idx+1,n-1,-i); s.increment(idx,idx,Long.MAX_VALUE); } StringBuilder sb=new StringBuilder(); for (int i=0;i<n;i++){ sb.append(ans[i]+" "); } System.out.println(sb); } static class SegmentTree{ int[] lo,hi,idx; long[] delta,min; SegmentTree(int n){ lo=new int[4*n+1]; hi=new int[4*n+1]; delta=new long[4*n+1]; min=new long[4*n+1]; idx=new int[4*n+1]; init(1,0,n-1); } void init(int i,int a,int b){ lo[i]=a; hi[i]=b; if (a==b)return; int m=(a+b)/2; init(2*i,a,m); init(2*i+1,m+1,b); } void propagate(int i){ delta[2*i]+=delta[i]; delta[2*i+1]+=delta[i]; delta[i]=0L; } long minimum(int a,int b){ return minimum(1,a,b); } void increment(int a,int b,long val){ increment(1,a,b,val); } void update(int i){ if (min[2*i]+delta[2*i]<min[2*i+1]+delta[2*i+1]){ idx[i]=idx[2*i]; min[i]=min[2*i]+delta[2*i]; }else { idx[i]=idx[2*i+1]; min[i]=min[2*i+1]+delta[2*i+1]; } } void increment(int i,int a,int b,long val){ if (a>hi[i] || b<lo[i]){ return; } if (hi[i]<=b && lo[i]>=a){ delta[i]+=val; if (a==b)idx[i]=a; return; } propagate(i); increment(2*i,a,b,val); increment(2*i+1,a,b,val); update(i); } long minimum(int i,int a,int b){ if (a>hi[i] || b<lo[i]){ return Long.MAX_VALUE; } if (hi[i]<=b && lo[i]>=a){ return min[i]+delta[i]; } propagate(i); long minLeft=minimum(2*i,a,b); long minRight=minimum(2*i+1,a,b); update(i); return Math.min(minLeft,minRight); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 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, a[200005]; long long s[200005]; pair<long long, int> sg[800005]; long long lz[800005]; void Build(int nod, int l, int r) { sg[nod] = {0, r}; if (l == r) return; int mid = (l + r) / 2; Build(2 * nod, l, mid); Build(2 * nod + 1, mid + 1, r); } void Sift(int nod, int l, int r) { sg[nod].first += lz[nod]; if (l != r) { lz[2 * nod] += lz[nod]; lz[2 * nod + 1] += lz[nod]; } lz[nod] = 0; } void Upd(int nod, int l, int r, int lt, int rt, long long v) { Sift(nod, l, r); if (l > rt || r < lt) return; if (l >= lt && r <= rt) { lz[nod] += v; Sift(nod, l, r); return; } int mid = (l + r) / 2; Upd(2 * nod, l, mid, lt, rt, v); Upd(2 * nod + 1, mid + 1, r, lt, rt, v); if (sg[2 * nod + 1].first > sg[2 * nod].first) sg[nod] = sg[2 * nod]; else sg[nod] = sg[2 * nod + 1]; } int32_t main() { ios_base ::sync_with_stdio(0); cin.tie(); cout.tie(); cin >> n; Build(1, 1, n); for (int i = 1; i <= n; i++) { cin >> s[i]; Upd(1, 1, n, i, i, s[i]); } for (int i = 1; i <= n; i++) { int p = sg[1].second; a[p] = i; Upd(1, 1, n, p, n, -i); Upd(1, 1, n, p, p, 1000000000000000005); } 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 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; namespace fast { inline char nc() { static char buf[100000], *L = buf, *R = buf; return L == R && (R = (L = buf) + fread(buf, 1, 100000, stdin), L == R) ? EOF : *L++; } template <class orz> inline void qread(orz &x) { x = 0; char ch = nc(); bool f = 0; while (ch < '0' || ch > '9') (ch == '-') && (f = 1), ch = nc(); while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = nc(); f && (x = -x); } } // namespace fast using namespace fast; template <class orz> inline void read(orz &x) { x = 0; bool f = 0; char ch = getchar(); while (ch < '0' || ch > '9') (ch == '-') && (f = 1), ch = getchar(); while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); f && (x = -x); } template <class orz> inline void out(orz x) { (x < 0) && (putchar('-'), x = -x); if (x > 9) out(x / 10); putchar(x % 10 + '0'); } const double PI = acos(-1); const double eps = 1e-8; const int INF = 0x3f3f3f3f; const long long mod = 998244353; const int maxn = 2e5 + 5; long long c[maxn], n; long long ask(int x) { long long ans = 0; for (; x; x -= x & -x) ans += c[x]; return ans; } void add(int x, long long y) { for (; x <= n; x += x & -x) c[x] += y; } long long s[maxn]; long long ans[maxn]; int erfen(long long key) { int left = 1; int right = n; while (left <= right) { int mid = (left + right) / 2; if (ask(mid) <= key) { left = mid + 1; } else { right = mid - 1; } } return right; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &s[i]); add(i, i * 1LL); } for (int i = n; i >= 1; i--) { ans[i] = erfen(s[i]) + 1; add(ans[i], -1LL * ans[i]); } printf("%lld", ans[1]); for (int i = 2; i <= n; i++) { printf(" %lld", ans[i]); } putchar('\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
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, ans[N]; long long a[N], bit[N]; void update(int pos) { for (int i = pos; i < N; i += (i & -i)) { bit[i] += pos; } } long long query(int pos) { long long res = 0; for (int i = pos; i; i -= (i & -i)) { res += bit[i]; } return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = n; i; i--) { int l = 1, r = n; while (l <= r) { int mid = (l + r) / 2; if (1ll * (mid - 1) * mid / 2 - query(mid - 1) <= a[i]) { ans[i] = mid; l = mid + 1; } else { r = mid - 1; } } update(ans[i]); } for (int i = 1; i <= n; i++) { cout << ans[i] << (i == n ? "\n" : " "); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeMap; public class Solution{ public static void main(String[] args) throws IOException { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int tt = 1; while(tt-->0) { int n = fs.nextInt(); long[] s = new long[n+1]; Ftree ft = new Ftree(n); for(int i=1;i<=n;i++) { ft.update(i, i); s[i] = fs.nextLong(); } int[] ans = new int[n+1]; for(int i=n;i>0;i--) { ans[i] = ft.search(s[i]) + 1; ft.update(ans[i], -ans[i]); } for(int i=1;i<=n;i++) out.print(ans[i]+" "); out.println(); } out.close(); } static class Ftree{ long[] bit; int n; Ftree(int n){ this.n = n; bit = new long[n+1]; } void update(int ind, int val) { while(ind<=n) { bit[ind] += val; ind += ind&(-ind); } } long query(int ind) { long sum = 0; while(ind>0) { sum += bit[ind]; ind -= ind&(-ind); } return sum; } //doing search in O(LOG(N)) using binary lifting or you can do binary search in O(LOG^2(N)) int search(long x) { int pos = 0; long sum = 0; for(int i=20;i>=0;i--) { if(pos+(1<<i)<=n && sum + bit[pos + (1<<i)]<=x) { sum += bit[pos + (1<<i)]; pos += (1<<i); } } return pos; } } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); int temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } 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(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt(){ return Integer.parseInt(next()); } public int[] readArray(int n){ int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } public long nextLong() { return Long.parseLong(next()); } public char nextChar() { return next().toCharArray()[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
def update(x,val): while x<=n: BIT[x]+=val x+=(x&-x) def query(x): s=0 while x>0: s=(s+BIT[x]) x-=(x&-x) return s n=int(input()) BIT=[0]*(n+1) for i in range(1,n+1): update(i,i) arr=list(map(int,input().split())) answers=[0]*(n) #print(BIT) for i in range(n-1,-1,-1): lol=arr[i] low=0 fjf=0 high=n # print(lol) while True: mid=(high+low+1)//2 j=query(mid) # print(mid,j) # print(answers) # break if j>lol: if query(mid-1)==lol: answers[i]=mid update(mid,-mid) break else: high=mid else: low=mid print(*answers)
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 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 = 1000005; const long long inf = 0x3f3f3f3f3f3f3f3f; const long long MOD = 100000007; const double eps = 1e-10; long long qpow(long long a, long long b) { long long tmp = a % MOD, ans = 1; while (b) { if (b & 1) { ans *= tmp, ans %= MOD; } tmp *= tmp, tmp %= MOD, b >>= 1; } return ans; } long long lowbit(long long x) { return x & -x; } long long max(long long a, long long b) { return a > b ? a : b; } long long min(long long a, long long b) { return a < b ? a : b; } long long mmax(long long a, long long b, long long c) { return max(a, max(b, c)); } long long mmin(long long a, long long b, long long c) { return min(a, min(b, c)); } void mod(long long &a) { a += MOD; a %= MOD; } bool chk(long long now) {} long long half(long long l, long long r) { while (l <= r) { long long m = (l + r) / 2; if (chk(m)) r = m - 1; else l = m + 1; } return l; } long long ll(long long p) { return p << 1; } long long rr(long long p) { return p << 1 | 1; } long long mm(long long l, long long r) { return (l + r) / 2; } long long lg(long long x) { if (x == 0) return 1; return (long long)log2(x) + 1; } bool smleql(double a, double b) { if (a < b || fabs(a - b) <= eps) return true; return false; } double len(double a, double b, double c, double d) { return sqrt((a - c) * (a - c) + (b - d) * (b - d)); } bool isp(long long x) { if (x == 1) return false; if (x == 2) return true; for (long long i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; } long long n; long long s[maxn], h[maxn], a[maxn]; long long mx[maxn], mn[maxn]; long long tag[maxn]; void up(long long p) { mx[p] = max(mx[ll(p)], mx[rr(p)]); mn[p] = min(mn[ll(p)], mn[rr(p)]); } void down(long long p, long long l, long long r) { if (tag[p]) { long long m = mm(l, r); tag[ll(p)] += tag[p]; tag[rr(p)] += tag[p]; mx[ll(p)] += tag[p]; mx[rr(p)] += tag[p]; mn[ll(p)] += tag[p]; mn[rr(p)] += tag[p]; tag[p] = 0; } } pair<long long, long long> ask(long long p, long long l, long long r, long long L, long long R) { if (L <= l && r <= R) { return make_pair(mn[p], mx[p]); } down(p, l, r); pair<long long, long long> le = make_pair(inf, -1), ri = make_pair(inf, -1); long long m = mm(l, r); if (L <= m) le = ask(ll(p), l, m, L, R); if (R > m) ri = ask(rr(p), m + 1, r, L, R); up(p); return make_pair(min(le.first, ri.first), max(le.second, ri.second)); } void change(long long p, long long l, long long r, long long L, long long R, long long v) { if (L <= l && r <= R) { mx[p] += v; mn[p] += v; tag[p] += v; return; } down(p, l, r); long long m = mm(l, r); if (L <= m) change(ll(p), l, m, L, R, v); if (R > m) change(rr(p), m + 1, r, L, R, v); up(p); } void build(long long p = 1, long long l = 1, long long r = n) { if (l == r) { mx[p] = h[l]; mn[p] = h[l]; return; } long long m = mm(l, r); build(ll(p), l, m); build(rr(p), m + 1, r); up(p); } long long get(long long l, long long r, long long v) { if (l == r) return l; long long m = mm(l, r); pair<long long, long long> le = make_pair(inf, -1), ri = make_pair(inf, -1); if (l <= m) le = ask(1, 1, n, l, m); if (le.first <= v && le.second >= v) return get(l, m, v); else return get(m + 1, r, v); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n; for (long long i = 1; i <= 200000; ++i) h[i] = i * (i - 1) / 2; for (long long i = 1; i <= n; ++i) cin >> s[i]; build(); for (long long i = n; i >= 1; --i) { a[i] = get(1, n, s[i]); if (a[i] < n) change(1, 1, n, a[i] + 1, n, -a[i]); change(1, 1, n, a[i], a[i], -10000000); } for (long long 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
java
import java.io.*; import java.util.*; public class Codeforces1208D { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); long[] s = new long[n]; st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { s[i] = Long.parseLong(st.nextToken()); } int[] p = new int[n]; //sum[0][i-1] is i if i is there, 0 otherwise //god i need to make the sums long fml long[][] sum = new long[19][]; for (int i = 0; i <= 18; i++) { sum[i] = new long[1 + ((n-1)>>i)]; } for (int i = 0; i < n; i++) { sum[0][i] = (long) (i+1); } for (int i = 1; i <= 18; i++) { for (int j = 0; j <= (n-1)>>i; j++) { sum[i][j] = sum[i-1][2*j]; if (2*j+1 <= (n-1)>>(i-1)) { sum[i][j] += sum[i-1][2*j+1]; } } } for (int i = n-1; i >= 0; i--) { //find p[i] first //let t be array sum[0] shifted by 1 //want smallest j such that p[1]+p[2]+...+p[j] > s[i] long SUM = sum[18][0]; int j = 0; for (int k = 17; k >= 0; k--) { j = 2*j+1; if (((n-1)>>k) < j) { j--; } else if (SUM - sum[k][j] > s[i]) { SUM -= sum[k][j]; j--; } } p[i] = j+1; //decrement sum[0][j] -= (j+1); for (int k = 1; k <= 18; k++) { sum[k][j>>k] -= (j+1); } } for (int i = 0; i < n; i++) { pw.print(p[i] + " "); } pw.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
python2
N = int(raw_input()) arr = list(map(int,raw_input().split())) seen = [0 for x in range(N+1)] cnt = 1 tree = [0 for x in range(200001)] def update(i, v): while i < 200001: tree[i] += v i += i&-i def query(i): res = 0 while i > 0: res += tree[i] i -= i&-i return res for i in range(N-1,-1,-1): if arr[i]==0: arr[i] = cnt seen[cnt] = 1 update(cnt,cnt) cnt += 1 else: lo = 0 hi = N while lo < hi: mid = (lo+hi)/2 cur = mid*(mid+1)/2 cur -= query(mid) if arr[i] < cur: hi = mid else: lo = mid+1 while lo < N and seen[lo]: lo += 1 arr[i] = lo seen[lo] = 1 update(lo,lo) while cnt < N and seen[cnt]: cnt += 1 print " ".join([str(x) for x in arr])
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 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("unroll-loops") #pragma GCC optimize("O3") #pragma GCC optimize("fast-math") #pragma GCC optimize("Ofast") using namespace std; const int INF = 1e9 + 10; const long long INFll = 2e18; const int BASE1 = 179; const int BASE2 = 653; const long long MOD = 998244353; const int MAXA = 2000001; const int MAXN = 2e5 + 1; const int MAXLOG = 22; const long long COST = 1000 * 1000 * 1000; const long double PI = 3.1415926535; const long double EPS = 1e-15; struct SegmentTree { vector<pair<long long, long long> > tree; vector<long long> arr, add; SegmentTree(vector<long long>& a) { tree.resize(4 * ((int)(a).size()), make_pair(INF, -1)); add.resize(4 * ((int)(a).size()), 0); arr = a; build(0, 0, ((int)(arr).size())); } void build(long long v, long long left, long long right) { if (left + 1 == right) { tree[v] = make_pair(arr[left], -left); return; } long long mid = (left + right) / 2; build(2 * v + 1, left, mid); build(2 * v + 2, mid, right); tree[v] = min(tree[2 * v + 1], tree[2 * v + 2]); } void push(long long v, long long left, long long right) { if (add[v] == 0) return; tree[v].first += add[v]; if (left != right - 1) { add[2 * v + 1] += add[v]; add[2 * v + 2] += add[v]; } add[v] = 0; } void addval(long long v, long long left, long long right, long long lq, long long rq, long long val) { push(v, left, right); if (left >= rq || lq >= right) return; if (lq <= left && rq >= right) { add[v] += val; push(v, left, right); return; } long long mid = (left + right) / 2; addval(2 * v + 1, left, mid, lq, rq, val); addval(2 * v + 2, mid, right, lq, rq, val); tree[v] = min(tree[2 * v + 1], tree[2 * v + 2]); } pair<long long, long long> getMin(long long v, long long left, long long right, long long lq, long long rq) { push(v, left, right); if (left >= lq && right <= rq) return tree[v]; else if (left >= rq || right <= lq) return make_pair(INF, -1); long long mid = (left + right) / 2; return min(getMin(2 * v + 1, left, mid, lq, rq), getMin(2 * v + 2, mid, right, lq, rq)); } }; void solve() { long long n; cin >> n; vector<long long> a(n); for (int i = 0; i < (n); i++) cin >> a[i]; SegmentTree tree(a); vector<long long> p(n, -1); for (int i = 0; i < (n); i++) { auto pos = tree.getMin(0, 0, n, 0, n); p[-pos.second] = i + 1; tree.addval(0, 0, n, -pos.second, -pos.second + 1, INFll); tree.addval(0, 0, n, -pos.second + 1, n, -i - 1); } for (int i = 0; i < (n); i++) cout << p[i] << " "; cout << "\n"; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t--) solve(); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.StringTokenizer; public class RestorePermutation_D_CF_Manthan { static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws Exception{ BufferedReader bf= new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; int n=Integer.parseInt(bf.readLine()); long[] s=new long[n]; st=new StringTokenizer(bf.readLine()); int ans[]=new int[n]; FenwickTree ft= new FenwickTree(n); for(int i=0;i<n;i++) s[i]=Long.parseLong(st.nextToken()); for(int i=n-1;i>=0;i--) { int lo=1,hi=n; while(hi-lo>=0) { int mid=lo+hi>>1; long x=sum(mid-1); x-=ft.query(mid-1); if(x<s[i]) { lo=mid+1; }else if(x>s[i]) { hi=mid-1; }else { ans[i]=mid; lo=mid+1; } } ft.update(ans[i], ans[i]); } for(int i=0;i<n;i++) { pw.print(ans[i]+" "); } pw.println(); pw.flush(); } static long sum(int x) { return (x*1l*(x+1))/2; } static class FenwickTree{ int n; long ft[]; public FenwickTree(int n) { this.n=n; ft=new long[n+1]; } public void update(int idx,int val) { while(idx<=n) { ft[idx]+=val; idx+=(idx&-idx); } } long query(int i) { long sum=0; while(i>0) { sum+=ft[i]; i-=i&-i; } return sum; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int grand(int x) { return uniform_int_distribution<int>(0, x - 1)(rng); } long long getInt() { bool minus = false; long long result = 0; char ch; ch = getchar(); while (true) { if (ch == '-') break; if (ch >= '0' && ch <= '9') break; ch = getchar(); } if (ch == '-') minus = true; else result = ch - '0'; while (true) { ch = getchar(); if (ch < '0' || ch > '9') break; result = result * 10 + (ch - '0'); } if (minus) return -result; else return result; } long long gcd(long long x, long long y) { if (x < y) return gcd(y, x); if (y == 0) return x; return gcd(y, x % y); } const long long mod = 1e9 + 7; long long modexp(long long x, long long ex) { long long ans = 1ll; while (ex > 0) { if (ex & 1ll) ans = (ans * x) % mod; ex >>= 1ll; x = (x * x) % mod; } return ans; } const long long maxn = 1e6 + 7; const long long N = 1e6 + 7; const long long inf = 1e151515151515151515151515151515 + 7; long long srr[N]; long long per[N]; pair<long long, long long> tree[N]; long long lzy[N]; void build(long long nd, long long st, long long en) { if (st == en) { tree[nd] = {srr[st], -st}; return; } long long mid = (st + en) >> 1; build(nd + nd, st, mid); build(nd + nd + 1, mid + 1, en); tree[nd] = min(tree[nd + nd], tree[nd + nd + 1]); } inline void push(long long x) { if (lzy[x] == 0) return; tree[x + x].first += lzy[x]; tree[x + x + 1].first += lzy[x]; lzy[x + x] += lzy[x]; lzy[x + x + 1] += lzy[x]; lzy[x] = 0; } void upd(long long nd, long long st, long long en, long long l, long long r, long long val) { if (en < l || r < st) return; if (l <= st && en <= r) { tree[nd].first += val; lzy[nd] += val; return; } push(nd); long long mid = (st + en) >> 1; upd(nd + nd, st, mid, l, r, val); upd(nd + nd + 1, mid + 1, en, l, r, val); tree[nd] = min(tree[nd + nd], tree[nd + nd + 1]); } pair<long long, long long> qry(long long nd, long long st, long long en, long long l, long long r) { if (en < l || r < st) return {inf, 0}; if (l <= st && en <= r) return tree[nd]; push(nd); long long mid = (st + en) >> 1; return min(qry(nd + nd, st, mid, l, r), qry(nd + nd + 1, mid + 1, en, l, r)); } int32_t main() { { ios ::sync_with_stdio(false); cin.tie(0); cout.tie(0); }; long long n; cin >> n; for (long long i = 1; i <= n; i++) { cin >> srr[i]; } build(1, 1, n); for (long long i = 1; i <= n; i++) { auto p = qry(1, 1, n, 1, n); per[-p.second] = i; upd(1, 1, n, -p.second + 1, n, -i); upd(1, 1, n, -p.second, -p.second, inf); } for (long long i = 1; i <= n; i++) { cout << per[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
cpp
#include <bits/stdc++.h> using namespace std; int t, n; long long s[200005]; long long T[200005 << 2]; void build(int u, int l, int r) { T[u] = 0; if (l == r) { T[u] = l; return; } build((u << 1), l, ((l + r) >> 1)); build((u << 1 | 1), ((l + r) >> 1) + 1, r); T[u] = T[(u << 1)] + T[(u << 1 | 1)]; } int query(int u, int l, int r, long long S) { if (l == r) return l; if (T[(u << 1)] > S) return query((u << 1), l, ((l + r) >> 1), S); else return query((u << 1 | 1), ((l + r) >> 1) + 1, r, S - T[(u << 1)]); } void change(int u, int l, int r, int p) { if (l == r) { T[u] = 0; return; } if (p <= ((l + r) >> 1)) change((u << 1), l, ((l + r) >> 1), p); else change((u << 1 | 1), ((l + r) >> 1) + 1, r, p); T[u] = T[(u << 1)] + T[(u << 1 | 1)]; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &s[i]); } build(1, 1, n); vector<int> ans(n + 1); for (int i = n; i >= 1; i--) { ans[i] = query(1, 1, n, s[i]); change(1, 1, n, 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
cpp
#include <bits/stdc++.h> using namespace std; int n; long long s[200001]; int used[200001]; long long stree[800001]; int result[200001]; long long update(int node, int l, int r, int index, long long diff) { if (r < index || index < l) return stree[node]; if (r == l) { stree[node] += diff; return stree[node]; }; return stree[node] = update(node * 2, l, (l + r) / 2, index, diff) + update(node * 2 + 1, (l + r) / 2 + 1, r, index, diff); } long long query(int node, int l, int r, int left, int right) { if (r < left || l > right) return 0; if (l >= left && r <= right) return stree[node]; return query(node * 2, l, (l + r) / 2, left, right) + query(node * 2 + 1, (l + r) / 2 + 1, r, left, right); } int bsearch(long long c) { int mi = 1; int ma = n; if (c == 0 && used[1] == 0) return 0; while (mi + 1 < ma) { int mid = (mi + ma) / 2; long long res = query(1, 1, n, 1, mid); if (res <= c) mi = mid; else ma = mid; } return mi; } int main(void) { cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i]; } for (int i = 1; i <= n; i++) { update(1, 1, n, i, i); } for (int i = n; i >= 1; i--) { result[i] = bsearch(s[i]) + 1; update(1, 1, n, result[i], -result[i]); used[result[i]] = 1; } for (int i = 1; i <= n; i++) { cout << result[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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author null */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Input in = new Input(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { final int T = 1 << 18; long[] t = new long[2 * T]; public void solve(int testNumber, Input in, PrintWriter out) { try { int n = in.readInt(); long[] f = new long[n]; for (int i = 0; i < n; i++) { f[i] = in.readLong(); } for (int i = T; i < t.length; i++) { t[i] = i - T; } for (int i = T - 1; i >= 1; i--) { t[i] = t[2 * i] + t[2 * i + 1]; } int[] p = new int[n]; for (int i = n - 1; i >= 0; i--) { p[i] = find(f[i]); remove(p[i]); } StringBuilder ans = new StringBuilder(); for (int i = 0; i < n; i++) { ans.append(p[i]).append(' '); } out.println(ans.toString()); } catch (Exception e) { throw new RuntimeException(e); } } int find(long s) { int v = 1; while (v < T) { if (t[2 * v] > s) { v = 2 * v; } else { s -= t[2 * v]; v = 2 * v + 1; } } return v - T; } void remove(int p) { p = T + p; t[p] = 0; while (p > 1) { p /= 2; t[p] = t[2 * p] + t[2 * p + 1]; } } } static class Input { public final BufferedReader reader; private String line = ""; private int pos = 0; public Input(InputStream inputStream) { reader = new BufferedReader(new InputStreamReader(inputStream)); } private boolean isSpace(char ch) { return ch <= 32; } public String readWord() throws IOException { skip(); int start = pos; while (pos < line.length() && !isSpace(line.charAt(pos))) { pos++; } return line.substring(start, pos); } public int readInt() throws IOException { return Integer.parseInt(readWord()); } public long readLong() throws IOException { return Long.parseLong(readWord()); } private void skip() throws IOException { while (true) { if (pos >= line.length()) { line = reader.readLine(); pos = 0; } while (pos < line.length() && isSpace(line.charAt(pos))) { pos++; } if (pos < line.length()) { return; } } } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 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 <class T> bool mini(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool maxi(T &a, T b) { return a < b ? (a = b, true) : false; } const int N = 2e5 + 5; int n, ans[N]; long long bit[N], p[N]; void up(int i, int val) { assert(i > 0); for (; i <= n; i += i & -i) bit[i] += val; } long long get(int i) { long long res = 0; for (; i; i -= i & -i) res += bit[i]; return res; } inline void sol() { cin >> n; for (int i = 0, _ = (n); i < _; i++) cin >> p[i], up(i + 1, i + 1); for (int i = (int)(n)-1; i >= 0; --i) { int l = 1, r = n, mid; while (l < r) { mid = (l + r + 1) >> 1; if (get(mid - 1) <= p[i]) l = mid; else r = mid - 1; } up(l, -l); ans[i] = l; } for (int i = 0, _ = (n); i < _; i++) cout << ans[i] << ' '; } signed main() { { ios_base::sync_with_stdio(false), cin.tie(NULL); }; cout.precision(10); cout << fixed; sol(); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const long long INF = 1e18; int n; long long a[maxn], ans[maxn]; long long tree[maxn << 2], laz[maxn << 2]; void pushup(int rt) { tree[rt] = min(tree[rt << 1], tree[(rt << 1) | 1]); } void build(int l, int r, int rt) { if (l == r) { tree[rt] = a[l]; return; } build(l, ((l + r) >> 1), 2 * rt); build(((l + r) >> 1) + 1, r, 2 * rt + 1); pushup(rt); } void change(int x, int l, int r, int rt) { if (l == r) { tree[rt] = INF; return; } if (x <= ((l + r) >> 1)) change(x, l, ((l + r) >> 1), 2 * rt); else change(x, ((l + r) >> 1) + 1, r, 2 * rt + 1); pushup(rt); } void pushdown(int rt) { long long& x = laz[rt]; if (x) { tree[rt << 1] += x; tree[(rt << 1) | 1] += x; laz[rt << 1] += x; laz[(rt << 1) | 1] += x; x = 0; } } void update(int x, int ql, int qr, int l, int r, int rt) { if (ql == l && qr == r) { laz[rt] += x; tree[rt] += x; return; } pushdown(rt); if (qr <= ((l + r) >> 1)) update(x, ql, qr, l, ((l + r) >> 1), 2 * rt); else if (ql > ((l + r) >> 1)) update(x, ql, qr, ((l + r) >> 1) + 1, r, 2 * rt + 1); else { update(x, ql, ((l + r) >> 1), l, ((l + r) >> 1), 2 * rt); update(x, ((l + r) >> 1) + 1, qr, ((l + r) >> 1) + 1, r, 2 * rt + 1); } pushup(rt); } int query(int l, int r, int rt) { if (l == r) return l; pushdown(rt); if (tree[((l + r) >> 1) + 1, r, 2 * rt + 1] == 0) return query(((l + r) >> 1) + 1, r, 2 * rt + 1); else return query(l, ((l + r) >> 1), 2 * rt); } int main() { cin.sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } build(1, n, 1); int t; for (int i = 1; i <= n; i++) { t = query(1, n, 1); ans[t] = i; change(t, 1, n, 1); if (t != n) update(-i, t + 1, n, 1, n, 1); } for (int i = 1; i <= n; i++) cout << ans[i] << ' '; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> inline long long minn(long long a, long long b) { if (a < b) return a; return b; } const int MAXN = 2e5 + 5; const long long INF = 1ll << 60; int n; long long mi[MAXN << 2], tag[MAXN << 2]; inline void pushdwn(int k) { mi[k << 1] -= tag[k]; tag[k << 1] += tag[k]; mi[k << 1 | 1] -= tag[k]; tag[k << 1 | 1] += tag[k]; tag[k] = 0; } inline void pushup(int k) { mi[k] = minn(mi[k << 1], mi[k << 1 | 1]); } void bld(int k, int l, int r) { if (l == r) { scanf("%I64d", mi + k); return; } int mid = l + r >> 1; bld(k << 1, l, mid); bld(k << 1 | 1, mid + 1, r); pushup(k); return; } int qx, qy, qv; int Query(int k, int l, int r) { if (l == r) return l; pushdwn(k); int mid = l + r >> 1; if (!mi[k << 1 | 1]) return Query(k << 1 | 1, mid + 1, r); return Query(k << 1, l, mid); } void Modify2(int k, int l, int r) { if (l == r) { if (mi[k] < 0) mi[k] = INF; return; } pushdwn(k); if (mi[k] < 0) { int mid = l + r >> 1; Modify2(k << 1, l, mid); Modify2(k << 1 | 1, mid + 1, r); pushup(k); } return; } void Modify(int k, int l, int r) { if (qx <= l && r <= qy) { mi[k] -= qv; tag[k] += qv; if (mi[k] < 0) { if (l == r) { mi[k] = INF; return; } Modify2(k, l, r); } return; } pushdwn(k); int mid = l + r >> 1; if (qx <= mid) Modify(k << 1, l, mid); if (mid < qy) Modify(k << 1 | 1, mid + 1, r); pushup(k); return; } int ans[MAXN]; int main() { scanf("%d", &n); bld(1, 1, n); for (int i = 1; i <= n; i++) { int t = Query(1, 1, n); ans[t] = i; qx = t, qy = n, qv = i; Modify(1, 1, n); } for (int i = 1; i <= n; i++) printf("%d%c", ans[i], i == n ? '\n' : ' '); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct S { int a, b; S() {} S(int _a, int _b) { a = _a; b = _b; } const bool operator<(const S &o) const { return a < o.a; } }; string exm; inline void exf(void) { cout << exm << "\n"; exit(0); } template <typename T> inline void showAll(vector<T> &v, string sep = "") { for (T &here : v) cout << here << sep; } template <typename T> inline void showAll(T arr[], int st, int end, string sep = "") { for (int i = st; i <= end; i++) cout << arr[i] << sep; } template <typename T> inline vector<int> int_seperation(T N, int d = 10) { vector<int> v; while (N) { v.push_back(N % d); N /= d; } reverse(v.begin(), v.end()); return v; } const int SIZE = 200009; long long arr[SIZE], tree[SIZE * 8]; int ans[SIZE]; int n; long long getSum(int i) { long long res = 0; while (i) { res += tree[i]; i -= (i & -i); } return res; } void update(int i, long long a) { while (i < n + 1) { tree[i] += a; i += (i & -i); } return; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &arr[i]); } for (int i = 1; i <= n; i++) { update(i, i); } for (int i = n; i; i--) { int l = 0, r = n + 1; while (l + 1 < r) { int mid = (l + r) / 2; long long sum = getSum(mid) - getSum(0); if (sum > arr[i]) r = mid; else l = mid; } ans[i] = l + 1; update(l + 1, -l - 1); } showAll(ans, 1, 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 int N = 200005; long long a[N]; pair<long long, long long> seg[4 * N]; void build(long long ind, long long l, long long r) { if (l > r) return; if (l == r) { seg[ind] = {a[l], l}; return; } long long mid = (l + r) / 2; build(2 * ind + 1, l, mid); build(2 * ind + 2, mid + 1, r); if (seg[2 * ind + 1].first < seg[2 * ind + 2].first) seg[ind] = seg[2 * ind + 1]; else seg[ind] = seg[2 * ind + 2]; } long long lazy[4 * N]; void update(long long ind, long long l, long long r, long long lq, long long rq, long long val) { if (lazy[ind] != 0) { seg[ind].first += lazy[ind]; if (l != r) { lazy[ind * 2 + 1] += lazy[ind]; lazy[ind * 2 + 2] += lazy[ind]; } lazy[ind] = 0; } if (l > r || r < lq || rq < l) return; if (l >= lq && r <= rq) { seg[ind].first += val; if (l != r) { lazy[ind * 2 + 1] += val; lazy[ind * 2 + 2] += val; } return; } long long mid = (l + r) / 2; update(2 * ind + 1, l, mid, lq, rq, val); update(2 * ind + 2, mid + 1, r, lq, rq, val); if (seg[2 * ind + 1].first < seg[2 * ind + 2].first) seg[ind] = seg[2 * ind + 1]; else seg[ind] = seg[2 * ind + 2]; } pair<long long, long long> query(long long ind, long long l, long long r, long long lq, long long rq) { if (l > r || r < lq || rq < l) return {1000000007, 0}; if (lazy[ind] != 0) { seg[ind].first += lazy[ind]; if (l != r) { lazy[ind * 2 + 1] += lazy[ind]; lazy[ind * 2 + 2] += lazy[ind]; } lazy[ind] = 0; } if (l >= lq && r <= rq) return seg[ind]; long long mid = (l + r) / 2; pair<long long, long long> k1, k2; k1 = query(2 * ind + 1, l, mid, lq, rq); k2 = query(2 * ind + 2, mid + 1, r, lq, rq); if (k1.first < k2.first) return k1; else return k2; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL); long long n, i, k; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; build(0, 0, n - 1); long long ans[n]; pair<long long, long long> x; long long l; k = 1; for (i = 0; i < n; i++) { x = query(0, 0, n - 1, 0, n - 1); l = x.second; ans[l] = k; update(0, 0, n - 1, l, l, 1000000007000000); update(0, 0, n - 1, l + 1, n - 1, -k); k++; } for (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 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n; long long s[200001], up[200001]; long long d[200001], c[200001]; long long ans[200001]; inline long long lowbit(long long x) { return x & -x; } inline long long getup(long long x) { long long ans = 0; for (; x; x -= lowbit(x)) ans += c[x]; return ans; } inline void add(long long x, long long d) { for (; x <= n; x += lowbit(x)) c[x] += d; } long long mybound(long long x) { long long l = 1, r = n; while (l < r) { long long mid = (l + r + 1) >> 1; if (getup(mid) <= x) l = mid; else r = mid - 1; } return l; } signed main() { scanf("%lld", &n); for (long long i = 1; i <= n; i++) scanf("%lld", &s[i]); for (long long i = 1; i <= n; i++) up[i] = up[i - 1] + i - 1; for (long long i = 1; i <= n; i++) c[i] = up[i] - up[i - lowbit(i)]; for (long long i = n; i >= 1; i--) { long long p = mybound(s[i]); ans[i] = p; add(p + 1, -p); } for (long long i = 1; i <= n; i++) printf("%lld ", ans[i]); putchar('\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 int mod = 1e9 + 7; template <class T, class U> inline void add_self(T &a, U b) { a += b; if (a >= mod) a -= mod; if (a < 0) a += mod; } template <class T, class U> inline void min_self(T &x, U y) { if (y < x) x = y; } template <class T, class U> inline void max_self(T &x, U y) { if (y > x) x = y; } template <class T, class U> inline void mul_self(T &x, U y) { x *= y; x %= mod; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { cout << t; ; if (sizeof...(v)) cerr << ", "; _print(v...); } template <class T, class U> void print_m(const map<T, U> &m, int w = 3) { if (m.empty()) { cout << "Empty" << endl; return; } for (auto x : m) cout << "(" << x.first << ": " << x.second << ")," << endl; cout << endl; } template <class T, class U> void debp(const pair<T, U> &pr, bool end_line = 1) { cout << "{" << pr.first << " " << pr.second << "}"; cout << (end_line ? "\n" : ", "); } template <class T> void print_vp(const T &vp, int sep_line = 0) { if (vp.empty()) { cout << "Empty" << endl; return; } if (!sep_line) cout << "{ "; for (auto x : vp) debp(x, sep_line); if (!sep_line) cout << "}\n"; cout << endl; } template <typename T> void print(const T &v, bool show_index = false) { int w = 2; if (show_index) { for (int i = 0; i < int((v).size()); i++) cout << setw(w) << i << " "; cout << endl; } for (auto &el : v) cout << setw(w) << el << " "; cout << endl; } template <typename T> void print_vv(const T &vv) { if (int((vv).size()) == 0) { cout << "Empty" << endl; return; } int w = 3; cout << setw(w) << " "; for (int j = 0; j < int((*vv.begin()).size()); j++) cout << setw(w) << j << " "; cout << endl; int i = 0; for (auto &v : vv) { cout << i++ << " {"; for (auto &el : v) cout << setw(w) << el << " "; cout << "},\n"; } cout << endl; } const long long inf = 1e15L; struct node { bool clazy = 0; long long lazy = 0, mn = inf; node(){}; node(long long v) : mn(v){}; }; class SegmentTree { public: int n; vector<node> st; SegmentTree(vector<long long> &a) { n = int((a).size()); st.resize(8 * n); build(1, 0, n - 1, a); } node merge(node &l, node &r) { node cur; cur.mn = min(l.mn, r.mn); return cur; } void build(int pos, int l, int r, vector<long long> &a) { if (l == r) { st[pos] = node(a[l]); return; } int mid = (l + r) / 2; build(2 * pos, l, mid, a); build(2 * pos + 1, mid + 1, r, a); st[pos] = merge(st[2 * pos], st[2 * pos + 1]); } void update(int pos, int sl, int sr, int l, int r, long long val) { propagate(pos, sl, sr); if (r < sl || sr < l) return; else if (l <= sl && sr <= r) { st[pos].clazy = 1; st[pos].lazy = val; propagate(pos, sl, sr); return; } int mid = (sl + sr) / 2; update(2 * pos, sl, mid, l, r, val); update(2 * pos + 1, mid + 1, sr, l, r, val); st[pos] = merge(st[2 * pos], st[2 * pos + 1]); } int query(int pos, int sl, int sr) { propagate(pos, sl, sr); if (sl == sr) return sl; int mid = (sl + sr) / 2; propagate(2 * pos, sl, mid); propagate(2 * pos + 1, mid + 1, sr); if (st[2 * pos + 1].mn == 0) return query(2 * pos + 1, mid + 1, sr); return query(2 * pos, sl, mid); } void propagate(int pos, int sl, int sr) { if (!st[pos].clazy) return; if (sl != sr) { st[2 * pos].lazy += st[pos].lazy; st[2 * pos + 1].lazy += st[pos].lazy; st[2 * pos].clazy = st[2 * pos + 1].clazy = 1; } st[pos].mn += st[pos].lazy; st[pos].lazy = st[pos].clazy = 0; } void update(int l, int r, long long v) { if (l > r) return; update(1, 0, n - 1, l, r, v); } int query() { return query(1, 0, n - 1); } }; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; while (cin >> n) { vector<long long> a(n); for (int i = 0; i < int(n); i++) cin >> a[i]; SegmentTree st(a); vector<int> out(n); for (int i = int(1); i < int(n + 1); i++) { int idx = st.query(); out[idx] = i; st.update(idx, idx, inf); st.update(idx + 1, n - 1, -i); } print(out); } 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
from sys import setrecursionlimit as SRL, stdin SRL(10 ** 7) rd = stdin.readline rrd = lambda: map(int, rd().strip().split()) n = int(rd()) bit = [0] * 200005 def add(x, val): while x <= n: bit[x] += val x += (x & -x) def query(x): num = 0 for i in range(30, -1, -1): if num+(1 << i) <= n and bit[num + (1 << i)] <= x: x -= bit[num + (1 << i)] num += (1 << i) return num + 1 for i in range(1, n + 1): add(i, i) s = list(rrd()) ans = [] for i in range(len(s) - 1, -1, -1): q = query(s[i]) ans.append(q) add(q, -q) ans = ans[::-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> using namespace std; const int M = 2e5 + 10; const long long inf = 1e15 + 10; const long long mod = 1e9 + 7; set<int> s; long long a[M], ans[M]; long long sum[4 * M], lazy[4 * M]; int v[M * 4]; void pushup(int x) { sum[x] = min(sum[x << 1], sum[x << 1 | 1]); } void built(int l, int r, int i) { if (l == r) { sum[i] = a[l]; return; } int mid = l + r >> 1; built(l, mid, i << 1); built(mid + 1, r, i << 1 | 1); pushup(i); } void pp(int i) { if (lazy[i]) { sum[i << 1] -= lazy[i]; sum[i << 1 | 1] -= lazy[i]; lazy[i << 1] += lazy[i]; lazy[i << 1 | 1] += lazy[i]; lazy[i] = 0; } } void add(int l, int r, int x, int y, long long va, int i) { if (l >= x && r <= y) { sum[i] -= va; lazy[i] += va; return; } pp(i); int mid = l + r >> 1; if (x <= mid) add(l, mid, x, y, va, i << 1); if (y > mid) add(mid + 1, r, x, y, va, i << 1 | 1); pushup(i); } long long query(int l, int r, int i) { if (l == r) { return l; } pp(i); int mid = l + r >> 1, k; if (sum[i << 1] < sum[i << 1 | 1]) k = query(l, mid, i << 1); else k = query(mid + 1, r, i << 1 | 1); return k; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); } built(1, n, 1); for (int i = 1; i <= n; i++) { int pos = query(1, n, 1); ans[pos] = i; add(1, n, pos, pos, -inf, 1); add(1, n, pos + 1, n, i, 1); } for (int i = 1; i <= n; i++) { printf("%d ", ans[i]); } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DRestorePermutation solver = new DRestorePermutation(); solver.solve(1, in, out); out.close(); } static class DRestorePermutation { public void solve(int testNumber, FastReader s, PrintWriter out) { int n = s.nextInt(); long[] arr = s.nextLongArray(n); int[] ans = new int[n]; DRestorePermutation.SegmentTree st = new DRestorePermutation.SegmentTree(n); for (int i = 0; i < n; i++) { st.increment(i, i, arr[i]); } for (int i = 1; i <= n; i++) { st.minimum(0, n - 1); int ans1 = (int) st.ans[1]; ans[ans1] = i; st.increment(ans1 + 1, n - 1, i * -1); st.increment(ans1, ans1, Long.MAX_VALUE - 1); } out.println(DRestorePermutation.arrays.printArr(ans)); } private static class arrays { static StringBuilder printArr(int[] arr) { StringBuilder ans = new StringBuilder(); for (int i = 0; i < arr.length; i++) { ans.append(arr[i] + " "); } return ans; } } private static class SegmentTree { long[] lo; long[] hi; long[] min; long[] delta; long[] ans; int n; public SegmentTree(int n) { this.n = n; this.hi = new long[4 * n + 1]; this.lo = new long[4 * n + 1]; this.min = new long[4 * n + 1]; this.delta = new long[4 * n + 1]; this.ans = new long[4 * n + 1]; init(1, 0, n - 1); } void prop(int i) { delta[2 * i] += delta[i]; delta[2 * i + 1] += delta[i]; delta[i] = 0; } void update(int i) { if (min[2 * i + 1] + delta[2 * i + 1] <= min[2 * i] + delta[2 * i]) { min[i] = min[2 * i + 1] + delta[2 * i + 1]; ans[i] = ans[2 * i + 1]; } else { min[i] = min[2 * i] + delta[2 * i]; ans[i] = ans[2 * i]; } // min[i] = Math.min(min[2 * i + 1] + delta[2 * i + 1], min[2 * i] + delta[2 * i]); } void increment(int a, int b, long val) { increment(1, a, b, val); } private void increment(int i, int a, int b, long val) { if (a > hi[i] || b < lo[i]) { return; } //Fully overlap case. if (a <= lo[i] && hi[i] <= b) { delta[i] += val; return; } //Partial overlap case. prop(i); increment(2 * i, a, b, val); increment(2 * i + 1, a, b, val); update(i); } private long minimum(int a, int b) { return minimum(1, a, b); } private long minimum(int i, int a, int b) { if (a > hi[i] || b < lo[i]) { return Long.MAX_VALUE; } if (a <= lo[i] && hi[i] <= b) { return min[i] + delta[i]; } prop(i); long minLeft = minimum(2 * i, a, b); long minRight = minimum(2 * i + 1, a, b); update(i); return Math.min(minLeft, minRight); } private void init(int i, int from, int to) { lo[i] = from; hi[i] = to; if (from == to) { ans[i] = from; return; } int mid = (from + to) / 2; init(2 * i, from, mid); init(2 * i + 1, mid + 1, to); } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; ++i) array[i] = nextLong(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.nio.CharBuffer; import java.util.NoSuchElementException; public class P1208D { public static void main(String[] args) { SimpleScanner scanner = new SimpleScanner(System.in); PrintWriter writer = new PrintWriter(System.out); int n = scanner.nextInt(); long[] s = new long[n]; for (int i = 0; i < n; ++i) s[i] = scanner.nextLong(); long[] bit = new long[n + 1]; for (int i = 1; i <= n; ++i) bitUpdate(bit, i, i); int[] x = new int[n]; for (int i = n - 1; i >= 0; --i) { int l = 1; int r = n + 1; while (l < r) { int mid = (l + r) / 2; long sum = bitQuery(bit, mid - 1); if (sum <= s[i]) l = mid + 1; else r = mid; } x[i] = l - 1; bitUpdate(bit, x[i], -x[i]); } for (int i = 0; i < n; ++i) writer.print(x[i] + " "); writer.println(); writer.close(); } private static long bitQuery(long[] bit, int x) { long sum = 0; for (; x >= 0; x = (x & (x + 1)) - 1) sum += bit[x]; return sum; } private static void bitUpdate(long[] bit, int x, int val) { for (; x < bit.length; x = x | (x + 1)) bit[x] += val; } private static class SimpleScanner { private static final int BUFFER_SIZE = 10240; private Readable in; private CharBuffer buffer; private boolean eof; SimpleScanner(InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); buffer = CharBuffer.allocate(BUFFER_SIZE); buffer.limit(0); eof = false; } private char read() { if (!buffer.hasRemaining()) { buffer.clear(); int n; try { n = in.read(buffer); } catch (IOException e) { n = -1; } if (n <= 0) { eof = true; return '\0'; } buffer.flip(); } return buffer.get(); } void checkEof() { if (eof) throw new NoSuchElementException(); } char nextChar() { checkEof(); char b = read(); checkEof(); return b; } String next() { char b; do { b = read(); checkEof(); } while (Character.isWhitespace(b)); StringBuilder sb = new StringBuilder(); do { sb.append(b); b = read(); } while (!eof && !Character.isWhitespace(b)); return sb.toString(); } int nextInt() { return Integer.valueOf(next()); } long nextLong() { return Long.valueOf(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MN = 200010; int n; long long ft[MN]; void update(int pos, long long by) { while (pos < n) ft[pos] += by, pos |= pos + 1; } long long query(int pos) { long long ans = 0; while (pos >= 0) ans += ft[pos], pos = (pos & (pos + 1)) - 1; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) update(i, i); vector<int> ans(n); for (int i = n - 1; i >= 0; i--) { int lo = 0, hi = n; while (hi - lo > 1) { int mi = (hi + lo) / 2; if (query(mi) <= a[i]) lo = mi; else hi = mi; } update(lo + 1, -lo - 1); ans[i] = lo; } for (int i = 0; i < n; i++) cout << ans[i] + 1 << " "; cout << '\n'; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, p[200005], mnpos[1 << 19]; long long s[200005], mn[1 << 19], add[1 << 19]; void pushup(int u) { mn[u] = mn[u << 1 | 1], mnpos[u] = mnpos[u << 1 | 1]; if (mn[u << 1] < mn[u]) mn[u] = mn[u << 1], mnpos[u] = mnpos[u << 1]; } void build(int u, int l, int r) { if (l == r) { mn[u] = s[l]; mnpos[u] = l; return; } int mid = l + r >> 1; build(u << 1, l, mid); build(u << 1 | 1, mid + 1, r); pushup(u); } void upd(int u, long long v) { add[u] += v, mn[u] += v; } void pushdown(int u) { if (add[u]) { upd(u << 1, add[u]); upd(u << 1 | 1, add[u]); add[u] = 0; } } void update(int u, int l, int r, int ql, int qr, long long v) { if (l >= ql && r <= qr) { upd(u, v); return; } int mid = l + r >> 1; pushdown(u); if (ql <= mid) update(u << 1, l, mid, ql, qr, v); if (qr > mid) update(u << 1 | 1, mid + 1, r, ql, qr, v); pushup(u); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%lld", s + i); build(1, 1, n); for (int i = 1; i <= n; ++i) { int x = mnpos[1]; p[x] = i; update(1, 1, n, x, x, 1LL << 40); if (x + 1 <= n) update(1, 1, n, x + 1, n, -i); } for (int i = 1; i <= n; ++i) printf("%d ", p[i]); puts(""); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200001; long long pre[N] = {}; void add(long long p, long long x) { for (long long i = p; i <= N; i += (i & -i)) pre[i] += x; } long long query(long long p) { long long ans = 0; for (long long i = p; i > 0; i -= i & (-i)) ans += pre[i]; return ans; } int main() { long long n; cin >> n; long long x[n], ans[n]; for (long long i = 1; i <= n; i++) add(i, i); for (long long i = 0; i < n; i++) cin >> x[i]; for (long long i = n - 1; i >= 0; i--) { long long l = 0, r = n, mid; while (l < r) { mid = l + r >> 1; if (query(mid) <= x[i]) l = mid + 1; else r = mid; } ans[i] = l; add(l, -l); } for (long long i : ans) cout << i << ' '; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void solve(); int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t = 1; for (int i = 1; i <= t; ++i) solve(); cerr << "Time taken: " << ((clock() * 1000) / CLOCKS_PER_SEC) << "ms\n"; } const long long int N = 2e5 + 2; long long int t[4 * N]; long long int lazy[4 * N]; long long int a[N]; long long int n; void init() { memset(lazy, 0, sizeof(lazy)); } void upd(long long int node, long long int l, long long int r, long long int x) { lazy[node] += x; t[node] += x; return; } void passDown(long long int node, long long int l, long long int r) { long long int mid = (l + r) / 2; upd(2 * node, l, mid, lazy[node]); upd(2 * node + 1, mid + 1, r, lazy[node]); lazy[node] = 0; } void build(long long int node = 1, long long int tl = 0, long long int tr = n - 1) { if (tl > tr) { return; } if (tl == tr) { t[node] = a[tl]; return; } long long int mid = (tl + tr) / 2; build(2 * node, tl, mid); build(2 * node + 1, mid + 1, tr); t[node] = min(t[2 * node], t[2 * node + 1]); } void updateRange(long long int l, long long int r, long long int val, long long int node = 1, long long int tl = 0, long long int tr = n - 1) { if (r < l or l > tr or tl > r or tl > tr) { return; } if (l <= tl and tr <= r) { upd(node, tl, tr, val); return; } passDown(node, tl, tr); long long int mid = (tl + tr) / 2; updateRange(l, r, val, 2 * node, tl, mid); updateRange(l, r, val, 2 * node + 1, mid + 1, tr); t[node] = min(t[2 * node], t[2 * node + 1]); } long long int queryRange(long long int l, long long int r, long long int node = 1, long long int tl = 0, long long int tr = n - 1) { if (r < l or tr < tl or l > tr or tl > r) { return 0; } if (tl == tr) { return tl; } passDown(node, tl, tr); long long int mid = (tl + tr) / 2; if (t[2 * node + 1]) return queryRange(l, r, 2 * node, tl, mid); else return queryRange(l, r, 2 * node + 1, mid + 1, tr); } void solve() { cin >> n; std::vector<long long int> ans(n); for (long long int i = 0; i < n; ++i) { cin >> a[i]; } init(); build(); for (long long int i = 0; i < n; ++i) { long long int pos = queryRange(0, n - 1); ans[pos] = i + 1; updateRange(pos + 1, n - 1, -(i + 1)); updateRange(pos, pos, 1e14); } for (long long int i = 0; 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.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 lewin */ 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 { static long[] arr; public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); arr = in.readLongArray(n); DRestorePermutation.SegmentTree rt = new DRestorePermutation.SegmentTree(0, n - 1); int[] ret = new int[n]; for (int i = 1; i <= n; i++) { int idx = rt.getZero(); ret[idx] = i; rt.update(idx, n - 1, i); } out.println(ret); } static class SegmentTree { public long min; public long sub; public boolean haszero; public DRestorePermutation.SegmentTree left; public DRestorePermutation.SegmentTree right; public int start; public int end; public SegmentTree(int start, int end) { this.start = start; this.end = end; if (start != end) { int mid = (start + end) / 2; left = new DRestorePermutation.SegmentTree(start, mid); right = new DRestorePermutation.SegmentTree(mid + 1, end); this.min = Math.min(left.min, right.min); this.sub = 0; this.haszero = left.haszero || right.haszero; } else { this.min = arr[start]; this.sub = 0; this.haszero = this.min == 0; } } public void upd(long csub) { sub += csub; min -= csub; haszero = min == 0; } public void push() { if (left != null) { left.upd(this.sub); right.upd(this.sub); this.sub = 0; } } public void join() { if (left != null) { this.min = Math.min(left.min, right.min); this.haszero = left.haszero || right.haszero; } } public void update(int s, int e, long csub) { if (this.start == s && this.end == e) { upd(csub); return; } push(); int mid = (start + end) / 2; if (e <= mid) left.update(s, e, csub); else if (mid < s) right.update(s, e, csub); else { left.update(s, mid, csub); right.update(mid + 1, e, csub); } this.join(); } public int getZero() { if (start == end) { this.min = 1L << 60; this.haszero = false; return start; } push(); int ans; if (right.haszero) ans = right.getZero(); else ans = left.getZero(); join(); return ans; } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public long[] readLongArray(int tokens) { long[] ret = new long[tokens]; for (int i = 0; i < tokens; i++) { ret[i] = nextLong(); } return ret; } public int read() { if (this.numChars == -1) { throw new InputMismatchException(); } else { if (this.curChar >= this.numChars) { this.curChar = 0; try { this.numChars = this.stream.read(this.buf); } catch (IOException var2) { throw new InputMismatchException(); } if (this.numChars <= 0) { return -1; } } return this.buf[this.curChar++]; } } public int nextInt() { int c; for (c = this.read(); isSpaceChar(c); c = this.read()) { ; } byte sgn = 1; if (c == 45) { sgn = -1; c = this.read(); } int res = 0; while (c >= 48 && c <= 57) { res *= 10; res += c - 48; c = this.read(); if (isSpaceChar(c)) { return res * sgn; } } throw new InputMismatchException(); } public long nextLong() { int c; for (c = this.read(); isSpaceChar(c); c = this.read()) { ; } byte sgn = 1; if (c == 45) { sgn = -1; c = this.read(); } long res = 0L; while (c >= 48 && c <= 57) { res *= 10L; res += (long) (c - 48); c = this.read(); if (isSpaceChar(c)) { return res * (long) sgn; } } throw new InputMismatchException(); } public static boolean isSpaceChar(int c) { return c == 32 || c == 10 || c == 13 || c == 9 || c == -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 println(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
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(len(p)-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; 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 = 21; i >= 0; i--) if (pos + (1 << i) <= N_MAX && sum + v[pos + (1 << i)] <= s) { pos += (1 << i); sum += v[pos]; } return pos + 1; } }; int main() { int n; scanf("%d", &n); BIT bit(n); vector<long long> v(n + 1), ans(n + 1); for (int i = 1; i <= n; i++) scanf("%lld", &v[i]), bit.upd(i, i); for (int i = n; i; i--) { int p = bit.bit_search(v[i]); ans[i] = p; bit.upd(p, -p); } for (int i = 1; i <= n; i++) printf("%lld ", ans[i]); printf("\n"); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
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))); int n; int S = 448; long[] blk = new long[S]; long[] add = new long[S]; long[] s = new long[234567]; void init() { for (int i = 1; i <= n; i++) { int b = i/S; blk[b] = Math.min(blk[b], s[i]); } } void push(int b) { if (add[b] != 0) { for (int i = b*S; i < (b+1)*S; i++) s[i] += add[b]; blk[b] += add[b]; add[b] = 0; } } void add(int i, long v) { if (i > n) return; int b = i/S; int off = i % S; int bn = n/S; push(b); for (int j = b*S+off; j < b*S+S; j++) { s[j] += v; blk[b] = Math.min(blk[b], s[j]); } for (int j = b + 1; j < bn; j++) add[j] += v; if (b != bn) { push(bn); for (int j = bn*S; j < bn*S+S; j++) { s[j] += v; blk[bn] = Math.min(blk[bn], s[j]); } } } int zero() { int b = 1/S; int bn = n/S; push(bn); for (int i = bn*S+S-1; i>= bn*S; i--) if (s[i] == 0) return i; for (int i = bn - 1; i > b; i--) if (blk[i] + add[i] == 0) { push(i); for (int j = i*S+S-1; j >= i*S; j--) if (s[j] == 0) return j; } push(b); for (int i = b*S + S - 1; i >= b*S; i--) if (s[i] == 0) return i; return -1; } void clear(int i) { int b = i/S; push(b); s[i] = Long.MAX_VALUE; blk[b] = Long.MAX_VALUE; for (int j = b*S; j < b*S+S; j++) { blk[b] = Math.min(blk[b], s[j]); } } void start() { n = readInt(); Arrays.fill(blk, Long.MAX_VALUE); Arrays.fill(s, Long.MAX_VALUE); for (int i = 1; i <= n; i++) { long v = readLong(); s[i] = v; } init(); int[] ans = new int[n+1]; for (int i = 1; i <= n; i++) { int j = zero(); clear(j); ans[j] = i; add(j+1, -i); } for (int i = 1; i <= n; i++) writer.print(ans[i] + " "); } public static void main(String[] args) { Test te = new Test(); te.start(); writer.flush(); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; const long long inf = 1e14; int n; long long a[maxn], ans[maxn]; struct SegmentTree { long long tr[maxn << 2], tag[maxn << 2]; void pushup(int rt) { tr[rt] = min(tr[rt << 1], tr[rt << 1 | 1]); } void build(int rt, int l, int r) { tag[rt] = 0; if (l == r) { tr[rt] = a[l]; return; } int mid = (l + r) >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); pushup(rt); } void load(int rt, long long k) { tr[rt] += k; tag[rt] += k; } void pushdown(int rt) { load(rt << 1, tag[rt]); load(rt << 1 | 1, tag[rt]); tag[rt] = 0; } void update(int nl, int nr, int l, int r, int rt, long long k) { if (nl > nr) return; if (nl <= l && nr >= r) { load(rt, k); return; } int mid = (l + r) >> 1; pushdown(rt); if (nl <= mid) update(nl, nr, l, mid, rt << 1, k); if (nr >= mid + 1) update(nl, nr, mid + 1, r, rt << 1 | 1, k); pushup(rt); } } seg2; int pos; void get_pos2(int rt, int l, int r) { if (l == r) { pos = l; return; } seg2.pushdown(rt); int mid = (l + r) >> 1; if (seg2.tr[rt << 1 | 1] <= 0) get_pos2(rt << 1 | 1, mid + 1, r); else get_pos2(rt << 1, l, mid); seg2.pushup(rt); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", a + i); seg2.build(1, 1, n); for (int x = 1; x <= n; x++) { get_pos2(1, 1, n); ans[pos] = x; seg2.update(pos, pos, 1, n, 1, inf); seg2.update(pos + 1, n, 1, n, 1, -x); } for (int i = 1; i <= n; i++) printf("%lld ", ans[i]); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ '0'); ch = getchar(); } return x * f; } namespace BIT { long long C[1000005]; inline void Add(long long x, long long val) { for (register long long i = x; i < 1000005; i += (i & (-i))) { C[i] += val; } } inline long long Query(long long x) { long long ans = 0; for (register long long i = x; i > 0; i -= (i & (-i))) { ans += C[i]; } return ans; } }; // namespace BIT using namespace BIT; long long a[1000005], n; inline long long BinSearch(long long pos) { long long l = 0, r = 1000005 - 1, ans; while (l < r - 1) { long long mid = (l + r) >> 1; if (Query(mid) <= a[pos]) l = mid; else r = mid; } return r; } long long ans[1000005]; int main() { n = read(); for (register long long i = 1; i <= n; ++i) a[i] = read(); for (register long long i = 1; i <= n; ++i) { Add(i, i); } for (register long long i = n; i >= 1; --i) { long long pos = BinSearch(i); Add(pos, -pos); ans[i] = pos; } for (register long long i = 1; i <= n; ++i) { printf("%I64d ", 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
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; long long BIT[N], s[N]; int n; int ans[N]; void update(int x, int delta) { for (; x <= n; x += x & -x) BIT[x] += delta; } long long query(int x) { long long sum = 0; for (; x > 0; x -= x & -x) sum += BIT[x]; return sum; } int searchNumber(long long prefSum) { int num = 0; long long sum = 0; for (int i = 21; i >= 0; --i) { if ((num + (1 << i) <= n) && (sum + BIT[num + (1 << i)] <= prefSum)) { num += (1 << i); sum += BIT[num]; } } return num + 1; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { update(i, i); scanf("%I64d", &s[i]); } for (int i = n; i >= 1; --i) { ans[i] = searchNumber(s[i]); update(ans[i], -ans[i]); } for (int i = 1; i <= n; ++i) { printf("%d", ans[i]); if (i < n) { printf(" "); } else 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
cpp
#include <bits/stdc++.h> using namespace std; template <class X, class Y> bool minimize(X &x, const Y &y) { X eps = 1e-9; if (x > y + eps) { x = y; return true; } else return false; } template <class X, class Y> bool maximize(X &x, const Y &y) { X eps = 1e-9; if (x + eps < y) { x = y; return true; } else return false; } template <class T> T Abs(const T &x) { return (x < 0 ? -x : x); } class SegmentTree { private: int n; vector<pair<long long, int>> tree; vector<long long> lazy; void build(long long a[], int i, int l, int r) { if (l == r) { tree[i] = make_pair(a[l], -l); return; } int m = (l + r) >> 1; build(a, 2 * i, l, m); build(a, 2 * i + 1, m + 1, r); tree[i] = min(tree[2 * i], tree[2 * i + 1]); } void pushDown(int i) { for (int j = (2 * i), _b = (2 * i + 1); j <= _b; j++) { tree[j].first += lazy[i]; lazy[j] += lazy[i]; } lazy[i] = 0; } void update(int i, int l, int r, int u, int v, long long c) { if (l > v || r < u || l > r || v < u) return; if (u <= l && r <= v) { tree[i].first += c; lazy[i] += c; return; } pushDown(i); int m = (l + r) >> 1; update(2 * i, l, m, u, v, c); update(2 * i + 1, m + 1, r, u, v, c); tree[i] = min(tree[2 * i], tree[2 * i + 1]); } public: SegmentTree(int n = 0, long long a[] = NULL) { this->n = n; if (n > 0) { tree.assign(4 * n + 7, pair<long long, int>()); lazy.assign(4 * n + 7, 0); build(a, 1, 1, n); } } void update(int l, int r, long long c) { update(1, 1, n, l, r, c); } int getZeroPos(void) const { return tree[1].first == 0 ? -tree[1].second : -1; } }; const long long INF = (long long)1e18 + 7LL; long long a[200200]; int perm[200200], n; void process(void) { cin >> n; for (int i = (1), _b = (n); i <= _b; i++) cin >> a[i]; SegmentTree myit(n, a); for (int i = (1), _b = (n); i <= _b; i++) { int pos = myit.getZeroPos(); assert(pos > 0); perm[pos] = i; myit.update(pos + 1, n, -i); myit.update(pos, pos, INF); } for (int i = (1), _b = (n); i <= _b; i++) printf("%d ", perm[i]); printf("\n"); } int main(void) { process(); 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.lang.*; import java.io.*; public class Main { PrintWriter out = new PrintWriter(System.out); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(""); String next() throws IOException { if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int ni() throws IOException { return Integer.parseInt(next()); } long nl() throws IOException { return Long.parseLong(next()); } long mod=1000000007; int n; long big=200000*200000; void solve() throws IOException { n=ni(); long[]A=new long[n+1]; for (int i=1;i<=n;i++) A[i]=nl(); segtree st=new segtree(A,1,n); int[]ans=new int[n+1]; int p=1; while (p<=n) { int posn=st.getnext(p); ans[posn]=p; p++; } for (int i=1;i<=n;i++) out.print(ans[i]+" "); out.println(); out.flush(); } class segtree { int lt,rt; long min,up; segtree lc,rc; public segtree(long[]A,int p,int q) { lt=p; rt=q; if (lt==rt) min=A[lt]; else { int mid=(lt+rt)/2; lc=new segtree(A,lt,mid); rc=new segtree(A,mid+1,rt); min=Math.min(lc.min,rc.min); } } public int getnext(int v) { if (lt==rt) { //out.println(lt); min=Long.MAX_VALUE; return lt; } //out.println(lt+" "+rt+" "+lc.min+" "+rc.min+" "+up); if (up>0) { lc.min-=up; lc.up+=up; rc.min-=up; rc.up+=up; up=0; } int ret=-1; if (rc.min==0) ret=rc.getnext(v); else { ret=lc.getnext(v); rc.min-=v; rc.up+=v; } min=Math.min(lc.min,rc.min); return ret; } } int gcd(int a,int b) { return(b==0?a:gcd(b,a%b)); } long gcd(long a,long b) { return(b==0?a:gcd(b,a%b)); } long mp(long a,long p) { long r=1; while(p>0) { if ((p&1)==1) r=(r*a)%mod; p>>=1; a=(a*a)%mod; } return r; } public static void main(String[] args) throws IOException { new Main().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
cpp
#include <bits/stdc++.h> using namespace std; long long n; long long s[200010]; long long a[200010]; long long cnt[200010]; long long p[200010]; long long lb(long long x) { return x & (-x); } void ins(int x, int y) { for (long long i = x; i <= n; i += lb(i)) { p[i] += y; } return; } long long gs(int x) { long long sum = 0; for (long long i = x; i > 0; i -= lb(i)) { sum += p[i]; } return sum; } int re(long long x) { int l = 1, r = n; while (l < r) { int mid = (l + r + 1) / 2; long long tmp = gs(mid - 1); if (tmp > x) r = mid - 1; else l = mid; } return r; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; int i, j; for (i = 1; i <= n; i++) { cin >> s[i]; ins(i, i); } for (i = n; i > 0; i--) { a[i] = re(s[i]); ins(a[i], -a[i]); } for (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
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class SolutionD { public static void main(String[] args) throws IOException { Reader reader = new Reader(); int n = reader.readIntValue(); long[] s = reader.readLongNumbers(); int[] res = new int[n]; FenwickTree fenwickTree = new FenwickTree(n); for (int i = s.length - 1; i >= 0; i--) { long sum = 0; long currS = s[i]; int currIndex = 0; int a = 0; int b = s.length; while (a <= b) { int size = (a + b) / 2; sum = fenwickTree.sum(size); if (sum < currS) { a = size + 1; } else if (sum > currS) { b = size - 1; } else { currIndex = size; break; } } if (sum == currS) { int firstIndex = currIndex + 1; int lastIndex = s.length; int nextIndex = lastIndex; while (firstIndex < lastIndex) { int size = (firstIndex + lastIndex) / 2; sum = fenwickTree.sum(size); if (sum == currS) { firstIndex = size + 1; } else if (sum > currS) { lastIndex = size; nextIndex = lastIndex; } } res[i] = nextIndex; fenwickTree.updateValue(nextIndex, -nextIndex); } } for (int i = 0; i < res.length; i++) { System.out.print(res[i] + " "); } } } class FenwickTree { private long[] arr; FenwickTree(int size) { this.arr = new long[size + 1]; for (int i = 0; i < arr.length; i++) { updateValue(i, i); } } long sum(int range) { long result = 0; for (; range >= 0; range = (range & (range + 1)) - 1) { result += arr[range]; } return result; } // int sumABInterval(int l, int r) { // return sum(r) - sum(l - 1); // } void updateValue(int i, int delta) { for (; i < arr.length; i = (i | (i + 1))) arr[i] += delta; } } class Reader { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); public StringTokenizer getStringTokenizer() throws IOException { String line = bufferedReader.readLine().replaceAll("\\s+$", ""); return new StringTokenizer(line, " "); } public int getNextInt(StringTokenizer stringTokenizer) { while (stringTokenizer.hasMoreTokens()) { return Integer.parseInt(stringTokenizer.nextToken()); } throw new RuntimeException("no more tokens in string"); } public long getNextLong(StringTokenizer stringTokenizer) { while (stringTokenizer.hasMoreTokens()) { return Long.parseLong(stringTokenizer.nextToken()); } throw new RuntimeException("no more tokens in string"); } public String getNextString(StringTokenizer stringTokenizer) { while (stringTokenizer.hasMoreTokens()) { return stringTokenizer.nextToken(); } throw new RuntimeException("no more tokens in string"); } public String readStringValue() throws IOException { return bufferedReader.readLine().replaceAll("\\s+$", ""); } public int readIntValue() throws IOException { String line = bufferedReader.readLine().replaceAll("\\s+$", ""); int parsedToInt = Integer.parseInt(line); return parsedToInt; } public int[] readIntNumbers() throws IOException { String[] line = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); int[] parsedToInt = Arrays.stream(line).mapToInt(Integer::parseInt).toArray(); return parsedToInt; } public long readLongValue() throws IOException { String line = bufferedReader.readLine().replaceAll("\\s+$", ""); long parsedToLong = Long.parseLong(line); return parsedToLong; } public long[] readLongNumbers() throws IOException { String[] line = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); long[] parsedToLong = Arrays.stream(line).mapToLong(Long::parseLong).toArray(); return parsedToLong; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 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 dx[] = {1, 0, -1, 0}; long long dy[] = {0, 1, 0, -1}; long long gcd(long long x, long long y) { if (y == 0) return x; else return gcd(y, x % y); } long long expo(long long n, long long m, long long p) { long long r = 1; n = n % p; while (m > 0) { if (m % 2) r = (r * n) % p; n = (n * n) % p; m = m / 2; } return r % p; } bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long lazy[2000005]; vector<long long> a; struct node { long long val; long long ind; }; node seg[2000005]; void build(long long m, long long s, long long e) { node t; if (s > e) return; if (s == e) { t.val = a[s]; t.ind = s; seg[m] = t; return; } long long mid = (s + e) / 2; build(2 * m, s, mid); build(2 * m + 1, mid + 1, e); seg[m].val = min(seg[2 * m].val, seg[2 * m + 1].val); if (seg[2 * m].val >= seg[2 * m + 1].val) seg[m].ind = seg[2 * m + 1].ind; else seg[m].ind = seg[2 * m].ind; } void update(long long m, long long s, long long e, long long l, long long r, long long v) { if (lazy[m] != 0) { seg[m].val = seg[m].val + lazy[m]; if (s != e) { lazy[2 * m] += lazy[m]; lazy[2 * m + 1] += lazy[m]; } lazy[m] = 0; } if (s > e || s > r || e < l || r < l) return; if (s >= l && e <= r) { seg[m].val += v; if (s != e) { lazy[2 * m] += v; lazy[2 * m + 1] += v; } return; } long long mid = (s + e) / 2; update(2 * m, s, mid, l, r, v); update(2 * m + 1, mid + 1, e, l, r, v); seg[m].val = min(seg[2 * m].val, seg[2 * m + 1].val); if (seg[2 * m].val >= seg[2 * m + 1].val) seg[m].ind = seg[2 * m + 1].ind; else seg[m].ind = seg[2 * m].ind; } node query(long long m, long long s, long long e, long long l, long long r) { if (lazy[m] != 0) { seg[m].val = seg[m].val + lazy[m]; if (s != e) { lazy[2 * m] += lazy[m]; lazy[2 * m + 1] += lazy[m]; } lazy[m] = 0; } node x, y, t; x.ind = -1; if (s > e || s > r || e < l) return x; if (s >= l && e <= r) { return seg[m]; } long long mid = (s + e) / 2; x = query(2 * m, s, mid, l, r); y = query(2 * m + 1, mid + 1, e, l, r); t.val = min(x.val, y.val); if (x.val >= y.val) { t.ind = y.ind; } else { t.ind = x.ind; } return t; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; for (long long i = 0; i < n; i++) { long long x; cin >> x; a.push_back(x); } build(1, 0, n - 1); vector<long long> ans(n); for (long long i = 1; i <= n; i++) { node t = query(1, 0, n - 1, 0, n - 1); ans[t.ind] = i; update(1, 0, n - 1, t.ind, t.ind, (1LL << 61)); update(1, 0, n - 1, t.ind + 1, n - 1, -i); } for (long long 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 " ] }
CORRECT
java
import java.io.*; import java.util.*; public class Main { static int inf = (int) 1e9 + 7; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); int n = nextInt(); long a[] = new long [n]; for(int i = 0;i < n;i++) a[i] = nextLong(); int ans[] = new int [n]; segment_tree b = new segment_tree(n); for(int i = n - 1;i >= 0;i--) { ans[i] = b.get(a[i]) + 1; b.set(ans[i] - 1); } for(int i = 0;i < n;i++) pw.print(ans[i] + " "); pw.close(); } static BufferedReader br; static StringTokenizer st = new StringTokenizer(""); static PrintWriter pw; static String next() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } } class segment_tree { long min[], push[]; boolean used[]; int last; segment_tree(int n) { last = n - 1; min = new long [n * 4]; push = new long [n * 4]; used = new boolean[n * 4]; build(0, 0, last); } void build(int v, int l, int r) { if (l == r) { min[v] = (long)(l + 1) * l / 2; return; } int m = ((l + r) >> 1); build(v * 2 + 1, l, m); build(v * 2 + 2, m + 1, r); min[v] = Math.min(min[v * 2 + 1], min[v * 2 + 2]); } void pushing(int v) { push[v * 2 + 1] += push[v]; push[v * 2 + 2] += push[v]; min[v * 2 + 1] += push[v]; min[v * 2 + 2] += push[v]; push[v] = 0; } void set(int l) { set(0, 0, last, l, last, l + 1); } void set(int v, int l, int r, int left, int right, int num) { if (l > right || r < left) return; if (l >= left && r <= right) { push[v] -= num; min[v] -= num; return; } pushing(v); int m = ((l + r) >> 1); set(v * 2 + 1, l, m, left, right, num); set(v * 2 + 2, m + 1, r, left, right, num); min[v] = Math.min(min[v * 2 + 1], min[v * 2 + 2]); } int get(long s) { return get(0, 0, last, s); } int get(int v, int l, int r, long s) { if (min[v] > s || used[v]) return -1; if (l == r) { used[v] = true; return l; } pushing(v); int m = ((l + r) >> 1); int ans = get(v * 2 + 2, m + 1, r, s); if (ans == -1) ans = get(v * 2 + 1, l, m, s); used[v] = used[v * 2 + 1] && used[v * 2 + 2]; return ans; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; public class gym{ static public class SegmentTree { // 1-based DS, OOP int N; //the number of elements in the array as a power of 2 (i.e. after padding) pair[] array, sTree; long[]lazy; SegmentTree(pair[] in) { array = in; N = in.length - 1; sTree = new pair[N<<1]; //no. of nodes = 2*N - 1, we add one to cross out index zero lazy = new long[N<<1]; build(1,1,N); } void build(int node, int b, int e) // O(n) { if(b == e) sTree[node] = array[b]; else { int mid = b + e >> 1; build(node<<1,b,mid); build(node<<1|1,mid+1,e); if(sTree[node<<1].compareTo(sTree[node<<1|1])<=0) { sTree[node]=new pair(sTree[node<<1].n,sTree[node<<1].idx); } else { sTree[node]=new pair(sTree[node<<1|1].n,sTree[node<<1|1].idx); } } } void update_range(int i, int j, long val) // O(log n) { update_range(1,1,N,i,j,val); } void update_range(int node, int b, int e, int i, int j, long val) { if(i > e || j < b) return; if(b >= i && e <= j) { sTree[node].n += val; lazy[node] += val; } else { int mid = b + e >> 1; propagate(node, b, mid, e); update_range(node<<1,b,mid,i,j,val); update_range(node<<1|1,mid+1,e,i,j,val); if(sTree[node<<1].compareTo(sTree[node<<1|1])<=0) { sTree[node]=new pair(sTree[node<<1].n,sTree[node<<1].idx); } else { sTree[node]=new pair(sTree[node<<1|1].n,sTree[node<<1|1].idx); } } } void propagate(int node, int b, int mid, int e) { lazy[node<<1] += lazy[node]; lazy[node<<1|1] += lazy[node]; sTree[node<<1].n += lazy[node]; sTree[node<<1|1].n += lazy[node]; lazy[node] = 0; } pair query(int i, int j) { return query(1,1,N,i,j); } pair query(int node, int b, int e, int i, int j) // O(log n) { if(i>e || j <b) return inf; if(b>= i && e <= j) return sTree[node]; int mid = b + e >> 1; propagate(node, b, mid, e); pair q1 = query(node<<1,b,mid,i,j); pair q2 = query(node<<1|1,mid+1,e,i,j); if(q1.compareTo(q2)<=0) { return q1; } else { return q2; } } } static pair inf=new pair(100000000000l,100000000); static class pair implements Comparable<pair>{ long n;int idx; pair(long x,int y){ n=x;idx=y; } @Override public int compareTo(pair o) { if(n!=o.n) { if(n>o.n)return 1; return -1; } return idx-o.idx; } public String toString() { return n+" "+idx; } } public static void main(String[] args) throws Exception{ MScanner sc = new MScanner(System.in); PrintWriter pw=new PrintWriter(System.out); int n = sc.nextInt(); int N = 1; while(N < n) N <<= 1; //padding pair[] in = new pair[N + 1]; for(int i = 1; i <= n; i++) in[i] = new pair(sc.nextLong(),-i); for(int i=n+1;i<=N;i++) { in[i]=inf; } int[]ans=new int[n]; SegmentTree st=new SegmentTree(in); for(int i=1;i<=n;i++) { pair p=st.query(1, n); int indx=-p.idx; //System.out.println(indx+" "+p.n); ans[indx-1]=i; st.update_range(indx, indx, 100000000000l); if(indx!=n) { st.update_range(indx+1, n, -i); } /*if(i==1) { System.out.println(st.query(4, 5)); System.out.println(Arrays.toString(st.sTree)); }*/ } 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 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; long long arr[N]; int n; int lowbit(int x) { return x & -x; } void addv(int p, long long val) { while (p <= n) { arr[p] += val; p += lowbit(p); } } void add(int l, int r, long long val) { addv(l, val); addv(r + 1, -val); } long long get(int p) { long long res = 0; while (p) { res += arr[p]; p -= lowbit(p); } return res; } long long search(long long val) { int l = 1, r = n; while (l <= r) { int mid = (l + r) / 2; if (get(mid) <= val) l = mid + 1; else r = mid - 1; } return r; } long long sum[N]; long long ans[N]; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> sum[i]; add(i + 1, n, i); } for (int i = n; i >= 1; i--) { ans[i] = search(sum[i]); add(ans[i] + 1, n, -ans[i]); } for (int i = 1; i <= n; i++) cout << ans[i] << " "; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Node { long long l, r, rt, lz, minn, maxx; } node[1008611]; long long a[200861], sum[400861]; long long ans[200861]; void build(long long l, long long r, long long rt) { long long mid = (l + r) / 2; node[rt].l = l, node[rt].r = r; node[rt].lz = 0; if (l == r) { node[rt].minn = sum[l]; node[rt].maxx = sum[l]; return; } build(l, mid, rt * 2); build(mid + 1, r, rt * 2 + 1); node[rt].minn = min(node[rt * 2].minn, node[rt * 2 + 1].minn); node[rt].maxx = max(node[rt * 2].maxx, node[rt * 2 + 1].maxx); } void push_down(long long rt) { if (node[rt].lz) { node[rt * 2].minn -= node[rt].lz; node[rt * 2 + 1].minn -= node[rt].lz; node[rt * 2].maxx -= node[rt].lz; node[rt * 2 + 1].maxx -= node[rt].lz; node[rt * 2].lz += node[rt].lz; node[rt * 2 + 1].lz += node[rt].lz; node[rt].lz = 0; } } long long query(long long v, long long rt) { if (node[rt].l == node[rt].r) { return node[rt].r + 1; } push_down(rt); if (node[rt * 2 + 1].minn <= v) { return query(v, rt * 2 + 1); } else if (node[rt * 2].maxx >= v) { return query(v, rt * 2); } node[rt].minn = min(node[rt * 2].minn, node[rt * 2 + 1].minn); node[rt].maxx = max(node[rt * 2].maxx, node[rt * 2 + 1].maxx); } void update(long long l, long long r, long long v, long long rt) { long long mid = (node[rt].l + node[rt].r) / 2; if (node[rt].l == l && node[rt].r == r) { node[rt].lz += v; node[rt].minn -= v; node[rt].maxx -= v; return; } push_down(rt); if (r <= mid) { update(l, r, v, rt * 2); } else if (l > mid) { update(l, r, v, rt * 2 + 1); } else { update(l, mid, v, rt * 2); update(mid + 1, r, v, rt * 2 + 1); } node[rt].minn = min(node[rt * 2].minn, node[rt * 2 + 1].minn); node[rt].maxx = max(node[rt * 2].maxx, node[rt * 2 + 1].maxx); } int main() { long long n, i, j; scanf("%lld", &n); sum[0] = 0; for (i = 1; i <= n; i++) { scanf("%lld", &a[i]); sum[i] = sum[i - 1] + i; } build(0, n, 1); for (i = n; i >= 1; i--) { ans[i] = query(a[i], 1); update(ans[i], n, ans[i], 1); } for (i = 1; i <= n; i++) { if (i == n) printf("%lld\n", ans[i]); else if (i < n) { printf("%lld ", ans[i]); } } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") const int MAXN = 2e5 + 20; const int SIZE = (1 << 19) + 20; long long s[MAXN]; int ans[MAXN]; struct Segment_tree { struct Node { int sl, sr; long long val; } tree[SIZE]; inline void update(int root) { tree[root].val = tree[(root << 1)].val + tree[(root << 1 | 1)].val; } inline void build(int root, int l, int r) { tree[root].sl = l; tree[root].sr = r; if (l == r) { tree[root].val = l; return; } build((root << 1), l, ((tree[root].sl + tree[root].sr) >> 1)); build((root << 1 | 1), ((tree[root].sl + tree[root].sr) >> 1) + 1, r); update(root); } inline int find(int root, long long v) { if (tree[root].sl == tree[root].sr) { tree[root].val = 0; return tree[root].sl; } int res = 0; if (tree[(root << 1)].val > v) { res = find((root << 1), v); } else { res = find((root << 1 | 1), v - tree[(root << 1)].val); } update(root); return res; } } Tree; int main() { ios::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL); int n; cin >> n; for (register int i = (1); i <= (n); ++i) { cin >> s[i]; } Tree.build(1, 1, n); for (register int i = (n); i >= (1); --i) { ans[i] = Tree.find(1, s[i]); } for (register int i = (1); 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
python3
from operator import add class Stree: def __init__(self, f, n, default, init_data): self.ln = 2**(n-1).bit_length() self.data = [default] * (self.ln * 2) self.f = f for i, d in init_data.items(): self.data[self.ln + i] = d for j in range(self.ln - 1, 0, -1): self.data[j] = f(self.data[j*2], self.data[j*2+1]) def update(self, i, a): p = self.ln + i self.data[p] = a while p > 1: p = p // 2 self.data[p] = self.f(self.data[p*2], self.data[p*2+1]) def get(self, i, j): def _get(l, r, p): if i <= l and j >= r: return self.data[p] else: m = (l+r)//2 if j <= m: return _get(l, m, p*2) elif i >= m: return _get(m, r, p*2+1) else: return self.f(_get(l, m, p*2), _get(m, r, p*2+1)) return _get(0, self.ln, 1) def find_value(self, v): def _find_value(l, r, p, v): if r == l+1: return l elif self.data[p*2] <= v: return _find_value((l+r)//2, r, p*2+1, v - self.data[p*2]) else: return _find_value(l, (l+r)//2, p*2, v) return _find_value(0, self.ln, 1, v) def main(): n = int(input()) sums = {i:i for i in range(n+1)} stree = Stree(add, n+1, 0, sums) ss = list(map(int, input().split())) ss.reverse() pp = [] for s in ss: sval = stree.find_value(s) pp.append(sval) stree.update(sval,0) print(*(reversed(pp))) if __name__ == "__main__": main()
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.StringTokenizer; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Andy Phan */ public class d { public static void main(String[] args) { FS in = new FS(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextLong(); } BIT bit = new BIT(n+1); for(int i = 1; i <= n; i++) bit.update(i, i); int[] perm = new int[n]; for(int i = n-1; i >= 0; i--) { int ind = bit.getKth(arr[i]); perm[i] = ind; bit.update(ind, -ind); } for(int i = 0; i < n; i++) out.print(perm[i] + " "); out.println(); out.close(); }//@ static class BIT { int n; long[] tree; public BIT(int n) { this.n = n; tree = new long[n + 1]; } int read(int i) { i++; int sum = 0; while (i > 0) { sum += tree[i]; i -= i & -i; } return sum; } void update(int i, long val) { i++; while (i <= n) { tree[i] += val; i += i & -i; } } // if the BIT is a freq array, returns the // index of the kth item, or n if there are fewer // than k items. int getKth(long k) { int e = Integer.highestOneBit(n), o = 0; for (; e != 0; e >>= 1) { if (e + o <= n && tree[e + o] <= k) { k -= tree[e + o]; o += e; } } return o; } } static class FS { BufferedReader in; StringTokenizer token; public FS(InputStream str) { in = new BufferedReader(new InputStreamReader(str)); } public String next() { if (token == null || !token.hasMoreElements()) { try { token = new StringTokenizer(in.readLine()); } catch (IOException ex) { } return next(); } return token.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; template <class T> bool tomin(T &x, T y) { return y < x ? x = y, 1 : 0; } template <class T> bool tomax(T &x, T y) { return x < y ? x = y, 1 : 0; } template <class T> void read(T &x) { char c; x = 0; int f = 1; while (c = getchar(), c < '0' || c > '9') if (c == '-') f = -1; do x = (x << 3) + (x << 1) + (c ^ 48); while (c = getchar(), c >= '0' && c <= '9'); x *= f; } bool mem1; const double Pi = acos(-1); const int maxn = 2e5 + 5; int n; long long A[maxn]; int ans[maxn]; long long c[maxn]; void Add(int x, int v) { while (x <= n) { c[x] += v; x += x & -x; } } long long query(int x) { long long res = 0; while (x) { res += c[x]; x &= x - 1; } return res; } bool mem2; int main() { srand(time(NULL)); read(n); for (int i = 1, i_ = n; i <= i_; ++i) read(A[i]); for (int i = 1, i_ = n; i <= i_; ++i) Add(i, i); for (int i = n, i_ = 1; i >= i_; --i) { int L = 0, R = n - 1, mid, res; while (L <= R) { if (query(mid = L + R >> 1) <= A[i]) L = (res = mid) + 1; else R = mid - 1; } ans[i] = res + 1; Add(ans[i], -ans[i]); } for (int i = 1, i_ = n; i <= i_; ++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; long long int bit[200005]; int n; void update(int j, int x) { for (; j < 200005; j += j & (-j)) bit[j] += x; } long long int query(int j) { if (j == 0) return 0; long long int x = 0; for (; j > 0; j -= (j) & (-j)) x += bit[j]; return x; } int next_ele(long long int s) { int l, r, m; long long int q; l = 0, r = n; while (l < r) { m = (l + r) / 2; if (query(m) > s) r = m; else l = m + 1; } return l; } int compute(long long int s) { int x; s *= 2; x = sqrt(s); return x + 1; } int main() { int i; scanf("%d", &n); vector<long long int> sum(n); for (i = 0; i < n; i++) cin >> sum[i]; vector<int> ans(n); ans[n - 1] = compute(sum[n - 1]); for (i = 1; i < n + 1; i++) update(i, i); update(ans[n - 1], -ans[n - 1]); for (i = n - 2; i >= 0; i--) { ans[i] = next_ele(sum[i]); update(ans[i], -ans[i]); } for (i = 0; i < n; i++) printf("%d ", ans[i]); printf("\n"); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mxN = 2e5 + 10; long long a[mxN], v[mxN]; long long N; long long ans[mxN]; void upd(long long x, long long v) { for (long long i = x; i <= N; i += i & -i) a[i] += v; } long long sum(long long x) { long long S = 0; for (long long i = x; i; i -= i & -i) S += a[i]; return S; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> N; for (long long i = 1; i <= N; ++i) { cin >> v[i]; upd(i, i); } for (long long i = N; i; --i) { long long hi = N, lo = 1; while (lo < hi) { long long mid = (lo + hi) >> 1; long long x = sum(mid); if (x <= v[i]) lo = mid + 1; else hi = mid; } ans[i] = lo; upd(lo, -lo); } for (int i = 1; i <= N; ++i) cout << ans[i] << " \n"[i == N]; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.util.*; public class D implements Runnable { FastReader scn; PrintWriter out; String INPUT = ""; long[] st, lazy; void solve() { int n = scn.nextInt(); long[] arr = scn.nextLongArray(n); st = new long[4 * n]; lazy = new long[4 * n]; Arrays.fill(st, n * 1L * n); build(0, 0, n - 1, arr); int[] ans = new int[n]; for (int i = 0; i < n; i++) { int rv = move(0, 0, n - 1); ans[rv] = i + 1; update(0, 0, n - 1, rv, rv, n * 1L * n); update(0, 0, n - 1, rv + 1, n - 1, -(i + 1)); } for(int a : ans) { out.print(a + " "); } out.println(); } int move(int ind, int l, int r) { if (lazy[ind] != 0) { st[ind] += lazy[ind]; if (l != r) { lazy[2 * ind + 1] += lazy[ind]; lazy[2 * ind + 2] += lazy[ind]; } lazy[ind] = 0; } if (st[ind] == 0) { if (l == r) { return l; } int m = (l + r) >> 1; if (st[2 * ind + 2] + lazy[2 * ind + 2] == 0) { return move(2 * ind + 2, m + 1, r); } else { return move(2 * ind + 1, l, m); } } return -1; } void update(int ind, int l, int r, int si, int li, long val) { if (lazy[ind] != 0) { st[ind] += lazy[ind]; if (l != r) { lazy[2 * ind + 1] += lazy[ind]; lazy[2 * ind + 2] += lazy[ind]; } lazy[ind] = 0; } if (l > li || r < si || si > li) { return; } if (l >= si && r <= li) { st[ind] += val; if (l != r) { lazy[2 * ind + 1] += val; lazy[2 * ind + 2] += val; } return; } int m = (l + r) >> 1; update(2 * ind + 1, l, m, si, li, val); update(2 * ind + 2, m + 1, r, si, li, val); st[ind] = Math.min(st[2 * ind + 1], st[2 * ind + 2]); } void build(int ind, int l, int r, long[] arr) { if (l == r) { st[ind] = arr[l]; return; } int m = (l + r) >> 1; build(2 * ind + 1, l, m, arr); build(2 * ind + 2, m + 1, r, arr); st[ind] = Math.min(st[2 * ind + 1], st[2 * ind + 2]); } public void run() { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; out = new PrintWriter(System.out); scn = new FastReader(oj); solve(); out.flush(); if (!oj) { System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } } public static void main(String[] args) { new Thread(null, new D(), "Main", 1 << 26).start(); } class FastReader { InputStream is; public FastReader(boolean onlineJudge) { is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes()); } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return (char) skip(); } String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } char[] next(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } char[][] nextMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = next(m); return map; } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[][] next2DInt(int n, int m) { int[][] arr = new int[n][]; for (int i = 0; i < n; i++) { arr[i] = nextIntArray(m); } return arr; } long[][] next2DLong(int n, int m) { long[][] arr = new long[n][]; for (int i = 0; i < n; i++) { arr[i] = nextLongArray(m); } return arr; } int[] shuffle(int[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); int c = arr[i]; arr[i] = arr[j]; arr[j] = c; } return arr; } long[] shuffle(long[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); long c = arr[i]; arr[i] = arr[j]; arr[j] = c; } return arr; } int[] uniq(int[] arr) { arr = scn.shuffle(arr); Arrays.sort(arr); int[] rv = new int[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } long[] uniq(long[] arr) { arr = scn.shuffle(arr); Arrays.sort(arr); long[] rv = new long[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } int[] reverse(int[] arr) { int l = 0, r = arr.length - 1; while (l < r) { arr[l] = arr[l] ^ arr[r]; arr[r] = arr[l] ^ arr[r]; arr[l] = arr[l] ^ arr[r]; l++; r--; } return arr; } long[] reverse(long[] arr) { int l = 0, r = arr.length - 1; while (l < r) { arr[l] = arr[l] ^ arr[r]; arr[r] = arr[l] ^ arr[r]; arr[l] = arr[l] ^ arr[r]; l++; r--; } return arr; } int[] compress(int[] arr) { int n = arr.length; int[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } long[] compress(long[] arr) { int n = arr.length; long[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 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
_ = 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: 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) 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
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author beginner1010 */ 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); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { final int Max = (int) (2 * (1e5) + 10); long[] tree; long read(int idx) { if (idx == 0) return 0; long res = 0; while (idx > 0) { res += tree[idx]; idx -= (idx & (-idx)); } return res; } void update(int idx, int value) { while (idx < Max) { tree[idx] += value; idx += (idx & (-idx)); } return; } public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); tree = new long[Max]; for (int i = 1; i <= n; i++) { update(i, i); } long[] s = new long[n]; for (int i = 0; i < n; i++) { s[i] = in.nextLong(); } int[] ans = new int[n]; for (int i = n - 1; i >= 0; i--) { int lo = 1; int hi = n + 1; while (lo < hi) { int mid = (lo + hi + 1) >> 1; if (read(mid - 1) <= s[i]) { lo = mid; } else { hi = mid - 1; } } update(lo, -lo); ans[i] = lo; } for (int i = 0; i < n; i++) { if (i > 0) out.print(" "); out.print(ans[i]); } out.println(); } } static class InputReader { private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputStream stream; public InputReader(InputStream stream) { this.stream = stream; } private boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private 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 (isWhitespace(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 (!isWhitespace(c)); return res * sgn; } public long nextLong() { int c = read(); while (isWhitespace(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 (!isWhitespace(c)); return res * sgn; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 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 Problem_1208D { static long[] sum; static int n = 0; public static void main(String[] args) throws IOException { FastScanner input = new FastScanner(System.in); PrintWriter output = new PrintWriter(System.out); n = input.nextInt(); sum = new long[n + 1]; Arrays.fill(sum, 0); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = input.nextLong(); addToSum(i + 1, i + 1); } for (int i = n - 1; i >= 0; i--) { int low = 0, high = n; while (high - low > 1) { int middle = (low + high) / 2; long response = getSum(middle); if (response <= a[i]) low = middle; else high = middle; } a[i] = high; addToSum(high, -high); } for (int i = 0; i < n; i++) { output.print(a[i]); if (i < n - 1) output.print(" "); } output.println(); output.close(); } static long getSum(int middle) { long response = 0; for (; middle >= 0; middle = (middle & (middle + 1)) - 1) response += sum[middle]; return response; } static void addToSum(int index, long amount) { for (; index <= n; index |= (index + 1)) sum[index] += amount; } static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } FastScanner(FileReader s) { br = new BufferedReader(s); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { return br.readLine(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { 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; long long n; class fenwicktree { public: long long n; long long b[200005]; fenwicktree(long long N) { n = N; for (long long i = 0; i <= n; i++) b[i] = 0; } long long sum(long long idx) { long long ret = 0; for (idx; idx > 0; idx -= (idx & -idx)) ret += b[idx]; return ret; } void add(long long idx, long long delta) { for (idx; idx <= n; idx += (idx & -idx)) b[idx] += delta; } }; long long bin(long long l, long long r, long long x, fenwicktree &f) { while (l < r) { long long mid = (l + r + 1) / 2; if (f.sum(mid) <= x) l = mid; else r = mid - 1; } return l + 1; } void solve() { cin >> n; fenwicktree f(n + 5); for (long long i = 1; i <= n; i++) f.add(i, i); long long a[200005]; long long res[200005]; for (long long i = 0; i < n; i++) cin >> a[i]; for (long long i = n - 1; i >= 0; i--) { long long x = bin(0, n - 1, a[i], f); res[i] = x; f.add(x, -x); } for (long long i = 0; i < n; i++) cout << res[i] << " "; cout << "\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; t = 1; while (t--) solve(); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> const int N = 1e6 + 10; const long long int INF = 1e18; const int MOD = 998244353; const int lgN = 20; using namespace std; long long int tree[4 * N], lz[4 * N], v[N], w[N]; void build(int node, int st, int en) { lz[node] = 0; if (st == en) { tree[node] = v[st]; return; } int m = (st + en) / 2; build(2 * node + 1, st, m); build(2 * node + 2, m + 1, en); tree[node] = min(tree[2 * node + 1], tree[2 * node + 2]); } void pushdown(int node, int st, int en) { if (lz[node] > 0) { tree[node] -= lz[node]; if (st < en) { lz[2 * node + 1] += lz[node]; lz[2 * node + 2] += lz[node]; } lz[node] = 0; } } void pupd(int pos, long long int val, int node, int st, int en) { pushdown(node, st, en); if (st == en) { tree[node] = val; return; } int m = (st + en) / 2; pushdown(2 * node + 1, st, m); pushdown(2 * node + 2, m + 1, en); if (pos <= m) pupd(pos, val, 2 * node + 1, st, m); else pupd(pos, val, 2 * node + 2, m + 1, en); tree[node] = min(tree[2 * node + 1], tree[2 * node + 2]); } void upd(int l, int r, int val, int node, int st, int en) { pushdown(node, st, en); if (st > r || en < l) return; int m = (st + en) / 2; if (st >= l && en <= r) { tree[node] -= val; if (st < en) { lz[2 * node + 1] += val; lz[2 * node + 2] += val; } return; } upd(l, r, val, 2 * node + 1, st, m); upd(l, r, val, 2 * node + 2, m + 1, en); tree[node] = min(tree[2 * node + 1], tree[2 * node + 2]); } int query(int node, int st, int en) { pushdown(node, st, en); if (st == en) return st; int m = (st + en) / 2; pushdown(2 * node + 1, st, m); pushdown(2 * node + 2, m + 1, en); if (tree[2 * node + 2] <= tree[2 * node + 1]) return query(2 * node + 2, m + 1, en); return query(2 * node + 1, st, m); } void solve() { int n, i, go = 1; cin >> n; for (i = 0; i < n; i++) cin >> v[i]; build(0, 0, n - 1); while (go <= n) { int ans = query(0, 0, n - 1); assert(ans >= 0 && ans <= n - 1); w[ans] = go; if (ans + 1 < n) upd(ans + 1, n - 1, go, 0, 0, n - 1); go++; pupd(ans, INF, 0, 0, n - 1); } for (i = 0; i < n; i++) cout << w[i] << " \n"[i == n - 1]; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int T = 1; for (int t = 1; t <= T; t++) { solve(); } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200004; long long s[N], p[N]; int n, a[N]; void add(int x, int v) { for (int i = x; i <= n; i += (i & -i)) s[i] += v; } long long que(int x) { long long ans = 0; for (int i = x; i; i -= (i & -i)) ans += s[i]; return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &p[i]); add(i, i); } for (int i = n; i >= 1; i--) { int l = 0, r = n; while (l < r) { int mid = (l + r) / 2; if (que(mid) > p[i]) r = mid; else l = mid + 1; } add(l, -l); a[i] = l; } for (int i = 1; i <= n; i++) printf("%d ", 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; const long long N = 2 * 1e5 + 3; const long long INF = INT_MAX; const long long NEINF = INT_MIN; const long long MOD = 1e9 + 7; long long Add(long long x, long long y) { return (x + y) % MOD; } long long Mul(long long x, long long y) { return (x * y) % MOD; } long long BinPow(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = Mul(res, x); x = Mul(x, x); y >>= 1; } return res; } long long ModInverse(long long x) { return BinPow(x, MOD - 2); } long long Div(long long x, long long y) { return Mul(x, ModInverse(y)); } long long GetBit(long long num, long long i) { return (num >> i) & 1; } long long n; long long bit[N], s[N], val[N], res[N]; void add(long long idx, long long delta) { while (idx <= n) { bit[idx] += delta; idx += idx & -idx; } } long long get(long long idx) { long long res = 0; while (idx > 0) { res += bit[idx]; idx -= idx & -idx; } return res; } long long search(long long v) { long long l = 0, r = n - 1, res, mid; while (l <= r) { mid = (l + r) >> 1; long long temp = get(mid) + ((mid + 1) * mid) / 2; if (temp == v) { res = mid; l = mid + 1; } else if (temp > v) r = mid - 1; else l = mid + 1; } return res; } void Solve() { cin >> n; for (long long i = 1; i <= n; ++i) cin >> s[i]; for (long long i = 1; i <= n; ++i) val[i] = i; for (long long i = n; i >= 1; --i) { long long pos = search(s[i]) + 1; res[i] = pos; add(pos, -pos); } for (long long i = 1; i <= n; ++i) cout << res[i] << " "; } signed main() { cin.tie(NULL); ios_base::sync_with_stdio(false); Solve(); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.DataInputStream; import java.io.IOException; import java.io.PrintWriter; public class P1208D8 { 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); } else { ans[i] = binSearch(tree, cur, n); } tree.add(ans[i], -(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 { final int N; private final long[] tree; public BIT(int sz) { tree = new long[(N = sz + 1)]; } private static int lsb(int i) { return i & -i; } private long prefixSum(int i) { long sum = 0L; while (i != 0) { sum += tree[i]; i &= ~lsb(i); } return sum; } public void add(int i, long v) { while (i < N) { tree[i] += v; i += lsb(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; struct BigNum { vector<long long> value; void set(long long x) { value = *(new vector<long long>); value.push_back(x); } void duplicate(BigNum other) { value = other.value; } void add(BigNum other) { vector<long long> o = other.value; if (o.size() > value.size()) { vector<long long> t = value; value = o; o = t; } for (int i = 0; i < o.size(); i += 1) { value[i] += o[i]; if (i < value.size() - 1 && value[i] >= (long long)1e9) { value[i + 1] += value[i] / (long long)1e9; value[i] = value[i] % (long long)1e9; } } if (value.back() >= (long long)1e9) { value.push_back(value.back() / (long long)1e9); value[value.size() - 2] = value[value.size() - 2] % (long long)1e9; } } void mul(long long mul) { for (int i = 0; i < value.size(); i += 1) { value[i] *= mul; } for (int i = 0; i < value.size() - 1; i += 1) { if (value[i] >= (long long)1e9) { value[i + 1] += value[i] / (long long)1e9; value[i] = value[i] % (long long)1e9; } } if (value.back() >= (long long)1e9) { value.push_back(value.back() / (long long)1e9); value[value.size() - 2] = value[value.size() - 2] % (long long)1e9; } } long double div(BigNum other1, BigNum other2) { int s = value.size(); long double current = value.back(); if (s >= 2) { current += ((long double)value[s - 2]) / (long long)1e9; } if (s >= 3) { current += ((long double)value[s - 3]) / ((long long)1e9 * (long long)1e9); } int s1 = other1.value.size(); int s2 = other2.value.size(); long double other = 0; if (s1 + s2 - 1 == s) { other += other1.value.back() * other2.value.back(); if (s1 >= 2) { other += ((long double)other1.value[s1 - 2] * other2.value.back()) / (long long)1e9; } if (s2 >= 2) { other += ((long double)other2.value[s2 - 2] * other1.value.back()) / (long long)1e9; } if (s1 >= 3) { other += ((long double)other1.value[s1 - 3] * other2.value.back()) / ((long long)1e9 * (long long)1e9); } if (s2 >= 3) { other += ((long double)other2.value[s2 - 3] * other1.value.back()) / ((long long)1e9 * (long long)1e9); } } if (s1 + s2 - 1 == s - 1) { other += other1.value.back() * other2.value.back(); if (s1 >= 2) { other += ((long double)other1.value[s1 - 2] * other2.value.back()) / (long long)1e9; } if (s2 >= 2) { other += ((long double)other2.value[s2 - 2] * other1.value.back()) / (long long)1e9; } other /= (long long)1e9; } if (s1 + s2 - 1 == s - 2) { other += other1.value.back() * other2.value.back(); other /= (long long)1e9 * (long long)1e9; } return other / current; } string stringify() { string out = to_string(value.back()); for (int i = value.size() - 1 - 1; i > -1; i -= 1) { string cur = to_string(value[i] + (long long)1e9); out += cur.substr(1); } return out; } }; long long gcd(long long a, long long b) { if (b == 0) { return a; } return gcd(b, a % b); } long long triangle(long long x) { return (((long long)(x + 1)) * (x)) / 2; } long long modInverse(long long a) { long long m = 1000000007; long long y = 0, x = 1; while (a > 1) { long long q = a / m; long long t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) { x += 1000000007; } return x; } long long modInverse(long long a, long long b) { long long m = b; long long y = 0, x = 1; while (a > 1) { long long q = a / m; long long t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) { x += b; } return x; } long long pow(long long a, long long b) { if (a <= 1) { return a; } if (b == 0) { return 1; } if (b % 2 == 0) { return pow((a * a) % 1000000007, b / 2) % 1000000007; } return (a * pow((a * a) % 1000000007, b / 2)) % 1000000007; } vector<long long> dev(vector<pair<long long, long long> > divisors) { if (divisors.size() == 0) { vector<long long> cur; cur.push_back(1); return cur; } long long x = divisors.back().first; long long n = divisors.back().second; divisors.pop_back(); vector<long long> ans = dev(divisors); vector<long long> cur; long long xi = 1; for (int i = 0; i < n + 1; i += 1) { for (int j = 0; j < ans.size(); j += 1) { cur.push_back(ans[j] * xi); } xi *= x; } return cur; } void add(vector<int>& x, vector<int>& y) { for (int i = 0; i < x.size(); i += 1) { x[i] += y[i]; } } struct pt { long long x, y; long long d() const { return x * x + y * y; } }; inline pt operator-(const pt& a) { return {-a.x, -a.y}; } inline pt operator+(const pt& a, const pt& b) { return {a.x + b.x, a.y + b.y}; } inline pt operator-(const pt& a, const pt& b) { return {a.x - b.x, a.y - b.y}; } inline long long operator*(const pt& a, const pt& b) { return a.x * b.y - a.y * b.x; } inline bool operator<(const pt& a, const pt& b) { return a * b < 0; } inline long long operator/(const pt& a, const pt& b) { return a.x * b.x + a.y * b.y; } inline bool operator==(const pt& a, const pt& b) { return a.x == b.x && a.y == b.y; } inline bool operator!=(const pt& a, const pt& b) { return a.x != b.x || a.y != b.y; } pt o; bool comp(pt a, pt b) { return (a - o) * (b - o) < 0; } vector<vector<long long> > segtre(20); void fix(int i, int j) { long long t = min(segtre[i - 1][2 * j], segtre[i - 1][2 * j + 1]); segtre[i][j] += t; segtre[i - 1][2 * j] -= t; segtre[i - 1][2 * j + 1] -= t; } int findzero() { int i = 20 - 1; int j = 0; while (i > 0) { if (segtre[i - 1][2 * j]) { j = 2 * j + 1; } else { j = 2 * j; } i--; } return j; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long n; cin >> n; for (int i = 0; i < 20; i += 1) { segtre[i].resize(2 * n); } for (int i = 0; i < n; i += 1) { cin >> segtre[0][n - i - 1]; } for (int i = 1; i < 20; i += 1) { for (int j = 0; j < n; j += 1) { fix(i, j); } } vector<long long> a(n); for (int i = 0; i < n; i += 1) { long long cur = findzero(); a[cur] = i + 1; segtre[0][cur] = n * n; for (int j = 0; j < 20; j += 1) { if ((1 << j) & cur) { segtre[j][(cur >> j) - 1] -= i + 1; } } for (int j = 1; j < 20; j += 1) { fix(j, cur >> j); fix(j, max((long long)0, (cur >> j) - 1)); } } for (int i = n - 1; i > -1; i -= 1) { cout << a[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 inf = 1e17; struct node { long long ans, sub; node() : ans(0), sub(0){}; }; struct SegmentTree { long long N; vector<node> st; vector<long long> a; long long left(long long p) { return p << 1; } long long right(long long p) { return (p << 1) + 1; } void push(long long p) { st[left(p)].sub += st[p].sub; st[right(p)].sub += st[p].sub; st[left(p)].ans -= st[p].sub; st[right(p)].ans -= st[p].sub; st[p].sub = 0; } void merge(long long p) { st[p].ans = min(st[left(p)].ans, st[right(p)].ans); } void build(long long p, long long l, long long r) { if (l == r) { st[p].ans = a[l]; } else { long long mid = l + r >> 1; build(left(p), l, mid); build(right(p), mid + 1, r); merge(p); } } SegmentTree(long long N, vector<long long>& a) : N(N), st(4 * N), a(a) { build(1, 0, N - 1); } void setElem(long long p, long long l, long long r, long long i) { if (l == r) { st[p].ans = inf; } else { long long mid = l + r >> 1; push(p); if (i > mid) setElem(right(p), mid + 1, r, i); else setElem(left(p), l, mid, i); merge(p); } } void update(long long p, long long l, long long r, long long i, long long j, long long val) { if (l == i && r == j) { st[p].sub += val; st[p].ans -= val; } else { long long mid = l + r >> 1; push(p); if (i > mid) update(right(p), mid + 1, r, i, j, val); else if (j <= mid) update(left(p), l, mid, i, j, val); else { update(left(p), l, mid, i, mid, val); update(right(p), mid + 1, r, mid + 1, j, val); } merge(p); } } void update(long long i, long long val) { if (i + 1 < N) update(1, 0, N - 1, i + 1, N - 1, val); setElem(1, 0, N - 1, i); } long long query(long long p, long long l, long long r) { if (l == r) { return l; } else { long long mid = l + r >> 1; push(p); if (st[right(p)].ans == 0) return query(right(p), mid + 1, r); else return query(left(p), l, mid); } } long long query() { return query(1, 0, N - 1); } }; int main() { ios::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; vector<long long> s(n); for (long long i = 0; i < n; i++) cin >> s[i]; vector<int> ans(n); SegmentTree st(n, s); for (long long i = 0; i < n; i++) { long long idx = st.query(); ans[idx] = i + 1; st.update(idx, i + 1); } for (int i = 0; i < n; i++) cout << ans[i] << " "; cout << endl; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.util.*; public class C { /* * 5 0 0 3 0 1 */ static long sum(long x) { x--; return x * (x + 1) / 2; } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = sc.nextLong(); FenwickTree tree = new FenwickTree(n); int[] ans = new int[n]; for (int i = n - 1; i >= 0; i--) { int lo = 1, hi = n; int found = -2; while (lo <= hi) { int mid = lo + hi >> 1; long x = sum(mid); x -= tree.query(mid); if (x < a[i]) { lo = mid + 1; } else if (x > a[i]) { hi = mid - 1; } else { found = mid; lo = mid + 1; } } ans[i] = found; tree.update(found + 1, found); } for (int x : ans) { out.print(x + " "); } out.println(); out.close(); } static class FenwickTree { long[] bit; FenwickTree(int n) { bit = new long[n + 5]; } void update(int idx, int v) { while (idx < bit.length) { bit[idx] += v; idx += idx & -idx; } } long query(int idx) { long ans = 0; while (idx > 0) { ans += bit[idx]; idx -= (idx & -idx); } return ans; } } static class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } Scanner(String fileName) throws FileNotFoundException { br = new BufferedReader(new FileReader(fileName)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { return br.readLine(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } boolean ready() throws IOException { return br.ready(); } } static void sort(int[] a) { shuffle(a); Arrays.sort(a); } static void shuffle(int[] a) { int n = a.length; Random rand = new Random(); for (int i = 0; i < n; i++) { int tmpIdx = rand.nextInt(n); int tmp = a[i]; a[i] = a[tmpIdx]; a[tmpIdx] = tmp; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; ll n; ll a[200001]; ll sum[200001]; ll bit[200001]; ll p[200001]; template <typename T> inline T read() { T x = 0; T multiplier = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') { multiplier = -1; } ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch & 15); ch = getchar(); } return x * multiplier; } inline ll query(ll idx) { ll ans = 0; for (; idx; idx -= idx & -idx) { ans += bit[idx]; } return ans; } inline void add(ll n, ll idx, ll val) { for (; idx <= n; idx += idx & -idx) { bit[idx] += val; } } inline ll get_idx(ll x) { ll l = 1, r = n, mid; ll idx; while (l <= r) { mid = (l + r + 1) >> 1; if (query(mid) <= x) { l = mid + 1; idx = mid; } else { r = mid - 1; } } return idx; } int main() { n = read<ll>(); for (ll i = 1; i <= n; i++) { a[i] = read<ll>(); } for (ll i = 1; i <= n; i++) { sum[i] = sum[i - 1] + i - 1; } for (ll i = 1; i <= n; i++) { bit[i] = sum[i] - sum[i - (i & -i)]; } for (ll i = n; i >= 1; i--) { ll x = get_idx(a[i]); p[i] = x; add(n, x + 1, -x); } for (ll i = 1; i <= n; i++) { printf("%lld ", p[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
java
import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; public class q5 { public static void main(String[] args) throws IOException { Reader.init(System.in); PrintWriter out=new PrintWriter(System.out); int n=Reader.nextInt(); long[] arr=new long[n]; for(int i=0;i<n;i++) arr[i]=Reader.nextLong(); Seg st=new Seg(n); st.build(0, 0, n-1); int[] ans=new int[n]; for(int i=n-1;i>=0;i--) { ans[i]=st.query(0, 0, n-1, arr[i]); st.update(0, 0, n-1, ans[i]); } for(int i=0;i<n;i++) { out.print(ans[i]+1+" "); } out.flush(); } } class Seg{ long[] st; Seg(int n){ st=new long[4*n]; } void build(int index, int left, int right) { if(left==right) { st[index]=left+1; } else { int mid=(left+right)/2; build(2*index+1,left,mid);build(2*index+2,mid+1,right); st[index]=st[2*index+1]+st[2*index+2]; } } int query(int index, int left, int right, long v) { if(left==right) { return left; } int mid=left+right;mid/=2; if(st[2*index+1]>v) return query(2*index+1,left,mid,v); return query(2*index+2,mid+1,right,v-st[2*index+1]); } void update(int index, int left, int right, int pos) { if(left==right) st[index]=0; else { int mid=(left+right)/2; if(pos<=mid) update(2*index+1,left,mid,pos); else update(2*index+2,mid+1,right,pos); st[index]=st[2*index+1]+st[2*index+2]; } } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init() throws IOException { reader = new BufferedReader( new FileReader("input.txt")); tokenizer = new StringTokenizer(""); } static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String nextLine() throws IOException{ return reader.readLine(); } static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.util.*; import java.io.*; public class MAN19D { public static void main(String[] args) throws IOException { FastScanner scan = new FastScanner(System.in); int n = scan.nextInt(); long[] arr = new long[n]; for(int i = 0; i < n; i++){ arr[i] = scan.nextLong(); } SegmentTree s = new SegmentTree(arr); int[] ans = new int[n]; for(int i = 1; i <= n; i++){ s.rmq(0, n-1); int idx = s.idx[1]; ans[idx] = i; s.inc(idx+1, n-1, -i); s.inc(idx, idx, Long.MAX_VALUE-1); } for(int i = 0; i < n; i++){ System.out.print(ans[i]+" "); } } static class SegmentTree{ int n; int[] lo, hi; long[] min, delta; int[] idx; SegmentTree(int t){ n = t; lo = new int[4*n+1]; hi = new int[4*n+1]; min = new long[4*n+1]; idx = new int[4*n+1]; delta = new long[4*n+1]; init(1, 0, n-1); } SegmentTree(long[] arr){ this(arr.length); for(int i = 0; i < n; i++) inc(i, i, arr[i]); } void init(int n, int l, int r) { lo[n] = l; hi[n] = r; if(l == r) return; int m = (l+r)/2; init(2*n, l, m); init(2*n+1, m+1, r); } void prop(int t) { delta[2*t] += delta[t]; delta[2*t+1] += delta[t]; delta[t] = 0; } long rmq(int l, int r) { return rmq(1, l, r); } long rmq(int t, int l, int r) { if(r < lo[t] || hi[t] < l) return Long.MAX_VALUE; if(l <= lo[t] && hi[t] <= r) return min[t]+delta[t]; prop(t); long minL = rmq(2*t, l, r); long minR = rmq(2*t+1, l, r); update(t); return Math.min(minL, minR); } void inc(int l, int r, long v) { inc(1, l, r, v); } void inc(int t, int l, int r, long v) { if(r < lo[t] || hi[t] < l) return; if(l <= lo[t] && hi[t] <= r) { delta[t] += v; if(l == r) idx[t] = r; return; } prop(t); inc(2*t, l, r, v); inc(2*t+1, l, r, v); update(t); } void update(int t) { // if(min[2*t]+delta[2*t] == min[2*t+1]+delta[2*t+1]){ // idx[t] = Math.max(idx[2*t], idx[2*t]+1); // min[t] = min[2*t]+delta[2*t]; // } // else if(min[2*t]+delta[2*t] < min[2*t+1]+delta[2*t+1]){ min[t] = min[2*t]+delta[2*t]; idx[t] = idx[2*t]; } else{ min[t] = min[2*t+1]+delta[2*t+1]; idx[t] = idx[2*t+1]; } } } static class FastScanner{ BufferedReader br; StringTokenizer st; public FastScanner(InputStream i) { br = new BufferedReader(new InputStreamReader(i)); st = new StringTokenizer(""); } public String next() throws IOException{ if(st.hasMoreTokens()) return st.nextToken(); else st = new StringTokenizer(br.readLine()); return next(); } public long nextLong() throws IOException{ return Long.parseLong(next()); } public int nextInt() throws IOException{ return Integer.parseInt(next()); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 222222; int p[N], a[N], streee[N << 2]; long long s[N], stree[N << 2]; int n; void supdate(int x, int v, int l, int r, int pos) { if (l == r) { streee[pos] += v; return; } int m = l + (r - l) / 2; if (x <= m) { supdate(x, v, l, m, pos * 2 + 1); } else { supdate(x, v, m + 1, r, pos * 2 + 2); } streee[pos] = streee[pos * 2 + 1] + streee[pos * 2 + 2]; } int squery(int i, int l, int r, int pos) { if (l == r) { return l; } int m = l + (r - l) / 2; if (i > streee[2 * pos + 1]) { return squery(i - streee[2 * pos + 1], m + 1, r, pos * 2 + 2); } return squery(i, l, m, pos * 2 + 1); } long long build(int l, int r, int pos) { if (l == r) { stree[pos] = a[l]; return stree[pos]; } int m = l + r >> 1; long long x = build(l, m, pos * 2 + 1); long long y = build(m + 1, r, pos * 2 + 2); stree[pos] = x + y; return stree[pos]; } void update(int i, int cl, int cr, int pos) { if (cl == i && cr == i) { stree[pos] = 0; return; } int m = cl + cr >> 1; if (i <= m) { update(i, cl, m, pos * 2 + 1); } else { update(i, m + 1, cr, pos * 2 + 2); } stree[pos] = stree[pos * 2 + 1] + stree[pos * 2 + 2]; } long long query(int l, int r, int cl, int cr, int pos) { if (cl > r || cr < l) { return 0; } else if (cl >= l && cr <= r) { return stree[pos]; } int m = cl + cr >> 1; long long x = query(l, r, cl, m, pos * 2 + 1); long long y = query(l, r, m + 1, cr, pos * 2 + 2); return x + y; } int solve(long long sum, int si) { int l = 1, r = si; while (l < r) { int i = l + r >> 1; int ii = squery(i, 1, n, 0); long long curr = query(0, ii - 1, 1, n, 0); if (curr < sum) { l = i + 1; } else { r = i; } } return squery(l, 1, n, 0); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = (1); i <= (n); i++) { cin >> s[i]; a[i] = i; supdate(i, 1, 1, n, 0); } build(1, n, 0); for (int i = (n); i >= (1); i--) { p[i] = solve(s[i], i); update(p[i], 1, n, 0); supdate(p[i], -1, 1, n, 0); } for (int i = (1); i <= (n); i++) { cout << p[i] << " "; } cout << "\n"; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.util.*; public class Test { static int readInt() { int ans = 0; boolean neg = false; try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (c == '-') { start = true; neg = true; continue; } else if (c >= '0' && c <= '9') { start = true; ans = ans * 10 + c - '0'; } else if (start) break; } } catch (IOException e) { } return neg ? -ans : ans; } static long readLong() { long ans = 0; boolean neg = false; try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (c == '-') { start = true; neg = true; continue; } else if (c >= '0' && c <= '9') { start = true; ans = ans * 10 + c - '0'; } else if (start) break; } } catch (IOException e) { } return neg ? -ans : ans; } static String readLine() { StringBuilder b = new StringBuilder(); try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (Character.isLetterOrDigit(c)) { start = true; b.append((char)c); } else if (start) break; } } catch (IOException e) { } return b.toString(); } static PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); long[] min = new long[1234567]; long[] add = new long[1234567]; int[] idx = new int[1234567]; void push(int x) { if (add[x] != 0) { add[2*x] += add[x]; add[2*x+1] += add[x]; add[x] = 0; } } void pull(int x) { if (min[2*x] + add[2*x] < min[2*x+1] + add[2*x+1]) { min[x] = min[2*x] + add[2*x]; idx[x] = idx[2*x]; } else { min[x] = min[2*x+1] + add[2*x+1]; idx[x] = idx[2*x+1]; } } void set(int x, int ll, int rr, int i, long v) { if (i < ll || i > rr) return; if (ll == rr) { add[x] = 0; min[x] = v; idx[x] = x; return; } int mid = (ll+rr)/2; push(x); set(2*x, ll, mid, i, v); set(2*x+1, mid+1, rr, i, v); pull(x); } void upd(int x, int ll, int rr, int i, int j, long d) { if (j < ll || rr < i) return; if (i <= ll && rr <= j) { add[x] += d; return; } push(x); int mid = (ll + rr) / 2; upd(2*x, ll, mid, i, j, d); upd(2*x+1, mid+1, rr, i, j, d); pull(x); } int zero(int x, int ll, int rr) { if (rr < ll || min[x] + add[x] > 0) return -1; if (ll == rr) return ll; push(x); int mid = (ll + rr) / 2; int ans = zero(2*x+1, mid + 1, rr); if (ans >= 0) return ans; return zero(2*x, ll, mid); } void start() { Arrays.fill(min, Long.MAX_VALUE); int n = readInt(); for (int i = 1; i <= n; i++) { long v = readLong(); set(1, 1, n, i, v); } int[] ans = new int[n+1]; for (int i = 1; i <= n; i++) { int j = zero(1, 1, n); ans[j] = i; set(1, 1, n, j, Long.MAX_VALUE); upd(1, 1, n, j+1, n, -i); } for (int i = 1; i <= n; i++) writer.print(ans[i] + " "); } public static void main(String[] args) { Test te = new Test(); te.start(); writer.flush(); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const long long inf = 1e18; long long a[N], ans[N]; struct node { int l, r; long long lazy; long long v; }; node e[N * 5]; void build(int root, int l, int r) { e[root].l = l; e[root].r = r; e[root].lazy = e[root].v = 0; if (l == r) { e[root].v = a[l]; return; } build(root * 2, l, (l + r) / 2); build(root * 2 + 1, (l + r) / 2 + 1, r); e[root].v = min(e[root * 2].v, e[root * 2 + 1].v); } void updown(int root) { if (e[root].lazy) { e[root * 2].lazy += e[root].lazy; e[root * 2 + 1].lazy += e[root].lazy; e[root * 2].v -= e[root].lazy; e[root * 2 + 1].v -= e[root].lazy; e[root].lazy = 0; } } int que(int root, int x) { if (e[root].l == e[root].r) return e[root].l; updown(root); if (e[root * 2 + 1].v == x) return que(root * 2 + 1, x); else return que(root * 2, x); } void update1(int root, int x) { if (e[root].l == e[root].r) { e[root].v = inf; return; } int mid = (e[root].l + e[root].r) / 2; if (x <= mid) update1(root * 2, x); else update1(root * 2 + 1, x); e[root].v = min(e[root * 2].v, e[root * 2 + 1].v); } void update2(int root, int l, int r, int x) { if (e[root].l == l && e[root].r == r) { e[root].v -= x; e[root].lazy += x; return; } updown(root); int mid = (e[root].l + e[root].r) / 2; if (l > mid) update2(root * 2 + 1, l, r, x); else { if (r <= mid) update2(root * 2, l, r, x); else { update2(root * 2, l, mid, x); update2(root * 2 + 1, mid + 1, r, x); } } e[root].v = min(e[root * 2].v, e[root * 2 + 1].v); } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); build(1, 1, n); for (int i = 1; i <= n; i++) { int k = que(1, 0); ans[k] = i; update1(1, k); if (k != n) update2(1, k + 1, n, i); } 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
cpp
#include <bits/stdc++.h> using namespace std; vector<long long> s; vector<pair<long long, long long>> tree; void build(long long l, long long r, long long v) { if (r - 1 == l) { tree[v] = {s[l], l}; return; } long long m = (r + l) / 2; build(l, m, v * 2 + 1); build(m, r, v * 2 + 2); auto a = tree[v * 2 + 1]; auto b = tree[v * 2 + 2]; if (a.first < b.first) { tree[v] = a; } else { tree[v] = b; } } vector<long long> mod; pair<long long, long long> get(long long l, long long r, long long u, long long w, long long v) { if (r <= u || l >= w) { return {1e9, 0}; } if (r <= w && l >= u) { return tree[v]; } long long m = (r + l) / 2; auto a = get(l, m, u, w, v * 2 + 1); auto b = get(m, r, u, w, v * 2 + 2); if (a.first < b.first) { return {a.first + mod[v], a.second}; } else { return {b.first + mod[v], b.second}; } } void update(long long l, long long r, long long u, long long w, long long v, long long delt) { if (r <= u || l >= w) { return; } if (r <= w && l >= u) { mod[v] += delt; auto a = tree[v]; tree[v] = {a.first + delt, a.second}; return; } long long m = (r + l) / 2; update(l, m, u, w, v * 2 + 1, delt); update(m, r, u, w, v * 2 + 2, delt); auto a = tree[v * 2 + 1]; auto b = tree[v * 2 + 2]; if (a.first < b.first) { tree[v] = {a.first + mod[v], a.second}; } else { tree[v] = {b.first + mod[v], b.second}; } } signed main() { long long n; cin >> n; s.resize(n); for (long long i = 0; i < n; i++) { cin >> s[i]; } tree.resize(4 * n); mod.resize(4 * n); build(0, n, 0); vector<long long> v(n); long long sum = 0; for (long long i = 1; i <= n; i++) { auto a = get(0, n, 0, n, 0); v[a.second] = i; update(0, n, a.second, a.second + 1, 0, 1e18 + 1); update(0, n, a.second + 1, n, 0, -i); } for (long long i = 0; i < n; i++) { cout << v[i] << " "; } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; import java.util.Vector; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.FileReader; 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; Scanner in = new Scanner(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, Scanner sc, PrintWriter pw) { int n = sc.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextLong(); FenwickTree ft = new FenwickTree(n + 1); for (int i = 1; i <= n; i++) ft.point_update(i, i); Stack<Integer> st = new Stack<>(); for (int i = n - 1; i >= 0; i--) { st.add(ft.findIndex(arr[i]) + 1); ft.point_update(st.peek(), -st.peek()); } while (!st.isEmpty()) pw.print(st.pop() + " "); } public class FenwickTree { int n; long[] ft; FenwickTree(int size) { n = size; ft = new long[n + 1]; } void point_update(int k, int val) { while (k <= n) { ft[k] += val; k += k & -k; } //min? } int findIndex(long cumFreq) { int msk = n; while ((msk & (msk - 1)) != 0) msk ^= msk & -msk; //msk will contain the MSB of n int idx = 0; while (msk != 0) { int tIdx = idx + msk; if (tIdx <= n && cumFreq >= ft[tIdx]) { idx = tIdx; cumFreq -= ft[tIdx]; } msk >>= 1; } if (cumFreq != 0) return -1; return idx; } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } 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
java
// package Quarantine; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class RestorePermutation { public static void update(long tree[],long lazy[],int s,int e,int l,int r,long val,int node){ if(lazy[node]!=0){ tree[node]+=lazy[node]; if(s!=e){ lazy[2*node]+=lazy[node]; lazy[2*node+1]+=lazy[node]; } lazy[node]=0; } if(s>r||e<l){ return; } if(s>=l&&e<=r){ tree[node]+=val; if(s!=e){ lazy[2*node]+=val; lazy[2*node+1]+=val; } return; } int mid=(s+e)/2; update(tree,lazy,s,mid,l,r,val,2*node); update(tree,lazy,mid+1,e,l,r,val,2*node+1); tree[node]=Math.min(tree[2*node],tree[2*node+1]); return; } public static long query(long tree[],long lazy[],int s ,int e,int l,int r,int node){ if(lazy[node]!=0){ tree[node]+=lazy[node]; if(s!=e){ lazy[2*node]+=lazy[node]; lazy[2*node+1]+=lazy[node]; } lazy[node]=0; } if(s>r||e<l){ return Long.MAX_VALUE; } if(s>=l&&e<=r){ return tree[node]; } int mid=(s+e)/2; long left=query(tree,lazy,s,mid,l,r,2*node); long right=query(tree,lazy,mid+1,e,l,r,2*node+1); return Math.min(left,right); } public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); StringTokenizer st=new StringTokenizer(br.readLine()); long a[]=new long[n+1]; for(int i=1;i<=n;i++){ a[i]=Long.parseLong(st.nextToken()); } int ans[]=new int[n+1]; long tree[]=new long[4*n+4]; long lazy[]=new long[4*n+4]; long infinity=(long)Math.pow(10,11); for(int i=1;i<=n;i++){ update(tree,lazy,1,n,i,i,a[i],1); } for(int i=1;i<=n;i++){ int low=1,high=n; int reqd=1; while(low<=high){ int mid=(low+high)/2; if(query(tree,lazy,1,n,mid,high,1)==0){ reqd=mid; low=mid+1; } else{ high=mid-1; } } // System.out.println(reqd); ans[reqd]=i; update(tree,lazy,1,n,reqd+1,n,-i,1); update(tree,lazy,1,n,reqd,reqd,infinity,1); } StringBuilder print=new StringBuilder(); for(int i=1;i<=n;i++){ print.append(ans[i]+" "); } System.out.println(print.toString()); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
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))); int n; int S = 448; long[] blk = new long[S]; long[] add = new long[S]; long[] s = new long[234567]; void init() { for (int i = 1; i <= n; i++) { int b = i/S; blk[b] = Math.min(blk[b], s[i]); } } void push(int b) { if (add[b] != 0) { for (int i = b*S; i < (b+1)*S; i++) s[i] += add[b]; blk[b] += add[b]; add[b] = 0; } } void add(int i, long v) { if (i > n) return; int b = i/S; int off = i % S; int bn = n/S; int noff = n % S; push(b); for (int j = b*S+off; j < b*S+S; j++) { s[j] += v; blk[b] = Math.min(blk[b], s[j]); } for (int j = b + 1; j < bn; j++) add[j] += v; if (b != bn) { push(bn); for (int j = bn*S; j <= bn*S+noff; j++) { s[j] += v; blk[bn] = Math.min(blk[bn], s[j]); } } } int zero() { int b = 1/S; int off = 1%S; int bn = n/S; int noff = n%S; push(bn); for (int i = bn*S+noff; i>= bn*S; i--) if (s[i] == 0) return i; for (int i = bn - 1; i > b; i--) if (blk[i] + add[i] == 0) { push(i); for (int j = i*S+S-1; j >= i*S; j--) if (s[j] == 0) return j; } push(b); for (int i = b*S + S - 1; i >= b*S + off; i--) if (s[i] == 0) return i; return -1; } void clear(int i) { int b = i/S; push(b); s[i] = Long.MAX_VALUE; blk[b] = Long.MAX_VALUE; for (int j = b*S; j < b*S+S; j++) { blk[b] = Math.min(blk[b], s[j]); } } void start() { n = readInt(); Arrays.fill(blk, Long.MAX_VALUE); for (int i = 1; i <= n; i++) { long v = readLong(); s[i] = v; } init(); int[] ans = new int[n+1]; for (int i = 1; i <= n; i++) { int j = zero(); clear(j); ans[j] = i; add(j+1, -i); } for (int i = 1; i <= n; i++) writer.print(ans[i] + " "); } public static void main(String[] args) { Test te = new Test(); te.start(); writer.flush(); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long int a[200005]; pair<long long int, long long int> tree[4 * 200005]; long long int lazy[4 * 200005]; void build(long long int node, long long int st, long long int en) { if (st == en) { tree[node] = {a[st], st}; return; } long long int mid = (st + en) / 2; build(2 * node, st, mid); build(2 * node + 1, mid + 1, en); if (tree[2 * node].first != tree[2 * node + 1].first) tree[node] = min(tree[2 * node], tree[2 * node + 1]); else { tree[node].first = min(tree[2 * node].first, tree[2 * node + 1].first); tree[node].second = max(tree[2 * node].second, tree[2 * node + 1].second); } } void update(long long int node, long long int st, long long int en, long long int l, long long int r, long long int v) { if (lazy[node] != 0) { tree[node].first += lazy[node]; if (st != en) { lazy[2 * node] += lazy[node]; lazy[2 * node + 1] += lazy[node]; } lazy[node] = 0; } if (st > en || st > r || en < l) return; if (st >= l && en <= r) { tree[node].first += v; if (st != en) { lazy[2 * node] += v; lazy[2 * node + 1] += v; } return; } long long int mid = (st + en) / 2; update(2 * node, st, mid, l, r, v); update(2 * node + 1, mid + 1, en, l, r, v); if (tree[2 * node].first != tree[2 * node + 1].first) tree[node] = min(tree[2 * node], tree[2 * node + 1]); else { tree[node].first = min(tree[2 * node].first, tree[2 * node + 1].first); tree[node].second = max(tree[2 * node].second, tree[2 * node + 1].second); } } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n; cin >> n; long long int i; long long int ans[n]; for (i = 0; i < n; i++) { cin >> a[i]; } build(1, 0, n - 1); for (i = 1; i <= n; i++) { long long int j = tree[1].second; update(1, 0, n - 1, j, j, 1e17); update(1, 0, n - 1, j + 1, n - 1, -i); ans[j] = i; } for (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 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline long long read() { long long s = 0, w = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); } while (isdigit(ch)) s = s * 10 + ch - '0', ch = getchar(); return s * w; } inline void write(long long x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } const int maxn = 2e5 + 10; long long a[maxn], coun[maxn << 2], sum[maxn << 2]; long long ans[maxn]; int n; void build(int l, int r, int i) { if (l == r) { sum[i] = 1; coun[i] = l; return; } int mid = (l + r) >> 1; build(l, mid, i << 1); build(mid + 1, r, (i << 1) + 1); sum[i] = sum[i << 1] + sum[(i << 1) + 1]; coun[i] = coun[i << 1] + coun[(i << 1) + 1]; } void update(int l, int r, int pos, int i) { if (l == r) { sum[i] = 0; coun[i] = 0; return; } int mid = (l + r) >> 1; if (pos <= mid) update(l, mid, pos, i << 1); if (pos > mid) update(mid + 1, r, pos, (i << 1) + 1); sum[i] = sum[i << 1] + sum[(i << 1) + 1]; coun[i] = coun[i << 1] + coun[(i << 1) + 1]; } long long query(int l, int r, int i, int k) { if (sum[i] <= k) return coun[i]; int mid = (l + r) >> 1; if (sum[i << 1] >= k) return query(l, mid, i << 1, k); return coun[i << 1] + query(mid + 1, r, (i << 1) + 1, k - sum[i << 1]); } int queryMin(int l, int r, int i, int k) { if (l == r) return l; int mid = (l + r) >> 1; if (sum[i << 1] >= k) return queryMin(l, mid, i << 1, k); return queryMin(mid + 1, r, (i << 1) + 1, k - sum[i << 1]); } bool check(int mid, long long x) { return query(1, n, 1, mid) >= x; } int main() { n = read(); for (int i = 1; i <= n; ++i) a[i] = read(); build(1, n, 1); for (int i = n; i >= 1; --i) { int l = 1, r = n; while (l <= r) { int mid = (l + r) >> 1; if (check(mid - 1, a[i])) { ans[i] = queryMin(1, n, 1, mid); r = mid - 1; } else l = mid + 1; } update(1, n, ans[i], 1); } for (int i = 1; i <= n; ++i) write(ans[i]), putchar(' '); putchar('\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.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 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.i(); long[] a = in.la(n); 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; } out.printLine(list); } } 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(); } } static class InputReader { private InputStream is; private byte[] inbuf = new byte[1024]; private int lenbuf = 0; private int ptrbuf = 0; public InputReader(InputStream is) { this.is = is; } private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } public long[] la(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = l(); return a; } public int i() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public long l() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } static class Fenwick { public final long[] bit; public final int size; public Fenwick(int[] a) { this(a.length); for (int i = 0; i < a.length; i++) { this.add(i, a[i]); } } public Fenwick(long[] a) { this(a.length); for (int i = 0; i < a.length; i++) this.add(i, a[i]); } public Fenwick(int size) { bit = new long[size + 1]; this.size = size + 1; } public void add(int i, long delta) { for (++i; i < size; i += (i & -i)) { bit[i] += delta; } } public int indexWithGivenCumFreq(long v) { int i = 0, n = size; for (int b = Integer.highestOneBit(n); b != 0; b >>= 1) { if ((i | b) < n && bit[i | b] <= v) { i |= b; v -= bit[i]; } } return i; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
// practice with rainboy import java.io.*; import java.util.*; public class CF1208D extends PrintWriter { CF1208D() { super(System.out, true); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF1208D o = new CF1208D(); o.main(); o.flush(); } long[] tr; int n_; void build(int n) { n_ = 1; while (n_ < n) n_ <<= 1; tr = new long[n_ * 2]; for (int i = 0; i < n; i++) tr[n_ + i] = i + 1; for (int i = n_ - 1; i >= 1; i--) tr[i] = tr[i << 1] + tr[i << 1 | 1]; } int query(long s) { int i = 1; while (i < n_) if (s >= tr[i << 1]) { s -= tr[i << 1]; i = i << 1 | 1; } else i = i << 1; return i - n_; } void update(int i) { tr[i += n_] = 0; while (i > 1) { i >>= 1; tr[i] = tr[i << 1] + tr[i << 1 | 1]; } } void main() { int n = sc.nextInt(); long[] ss = new long[n]; for (int i = 0; i < n; i++) ss[i] = sc.nextLong(); build(n); int[] pp = new int[n]; for (int i = n - 1; i >= 0; i--) { int j = query(ss[i]); update(j); pp[i] = j + 1; } for (int i = 0; i < n; i++) print(pp[i] + " "); println(); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int maxN = 1e6 + 10; ll tree[4 * maxN]; int n, p[maxN]; ll s[maxN]; void update(int x, int l, int r, int k, int w) { if (l == r) tree[x] = w; else { int mid = (l + r) / 2; if (k <= mid) update(2 * x, l, mid, k, w); else update(2 * x + 1, mid + 1, r, k, w); tree[x] = tree[2 * x] + tree[2 * x + 1]; } } void build(int x, int l, int r) { if (l == r) tree[x] = p[l]; else { int mid = (l + r) / 2; build(2 * x, l, mid); build(2 * x + 1, mid + 1, r); tree[x] = tree[2 * x] + tree[2 * x + 1]; } } ll query(int x, int l, int r, int i, int j) { if (l > j || r < i) return 0; else if (l >= i && r <= j) return tree[x]; int mid = (l + r) / 2; return query(2 * x, l, mid, i, j) + query(2 * x + 1, mid + 1, r, i, j); } void input() { cin >> n; for (int i = int(1); i <= int(n); ++i) p[i] = i; for (int i = int(1); i <= int(n); ++i) cin >> s[i]; } void solve() { build(1, 1, n); for (int i = int(n); i >= int(1); --i) { int l = 0, r = n, mid; ll tmp; while (r - l > 1) { mid = (l + r) / 2; if (mid == 0) tmp = 0; else tmp = query(1, 1, n, 1, mid); if (tmp <= s[i]) { l = mid; } else r = mid; } p[i] = l + 1; update(1, 1, n, l + 1, 0); } } void output() { for (int i = int(1); i <= int(n); ++i) cout << p[i] << " "; } int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); input(); solve(); output(); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.util.*; public class RestorePermutation { static class FenwickTree { int n; long[] BIT; FenwickTree(int n) { this.n = n; BIT = new long[n + 1]; } void update(int i, long x) { for (; i <= n; i += i&-i) { BIT[i] += x; } } int searchNum(long prefSum) { int num = 0; long sum = 0; for (int i = 19; i >= 0; --i) { if (num + (1 << i) <= n && sum + BIT[num + (1 << i)] <= prefSum) { num += (1 << i); sum += BIT[num]; } } return num; } } public static void main(String[] args) { FastReader in = new FastReader(System.in); // FastReader in = new FastReader(new FileInputStream("input.txt")); PrintWriter out = new PrintWriter(System.out); // PrintWriter out = new PrintWriter(new FileOutputStream("output.txt")); int n = in.nextInt(); long[] s = new long[n + 1]; FenwickTree ft = new FenwickTree(n); for (int i = 1; i <= n; ++i) { ft.update(i, i); s[i] = in.nextLong(); } int[] p = new int[n + 1]; for (int i = n; i >= 1; --i) { p[i] = ft.searchNum(s[i]) + 1; ft.update(p[i], -p[i]); } for (int i = 1; i <= n; ++i) out.print(p[i] + " "); out.println(); out.close(); } private static class FastReader { BufferedReader br; StringTokenizer st; FastReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } Integer nextInt() { return Integer.parseInt(next()); } Long nextLong() { return Long.parseLong(next()); } Double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } String nextLine() { String x = ""; try { x = br.readLine(); } catch (IOException e) { e.printStackTrace(); } 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
cpp
#include <bits/stdc++.h> using namespace std; long long n, m, q, x, k, t, y, w = 2, z, a[200500], ans[200500], bit[400500]; long long get(int i) { long long ret = 0; while (i) ret += bit[i], i -= (i & -i); return ret; } void update(int i, int val) { while (i <= 2 * n) bit[i] += val, i += (i & -i); } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i - 1], update(i + 1, i); for (int i = n - 1; i >= 0; --i) { z = 0; for (int j = pow(2, ceil(log2(n))); j > 0; j /= 2) if (get(j + z) <= a[i]) z += j; update(z + 1, -z); ans[i] = z; } 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 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> class fenwick { public: vector<T> fenw; int n; fenwick(int _n) : n(_n) { fenw.resize(n); } void modify(int x, T v) { while (x < n) { fenw[x] += v; x |= (x + 1); } } T get(int x) { T v{}; while (x >= 0) { v += fenw[x]; x = (x & (x + 1)) - 1; } return v; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<long long> s(n); for (int i = 0; i < n; i++) { cin >> s[i]; } fenwick<long long> fenw(n); for (int i = 0; i < n; i++) { fenw.modify(i, i + 1); } vector<int> a(n); for (int i = n - 1; i >= 0; i--) { int low = 0, high = n - 1; while (low < high) { int mid = (low + high) >> 1; if (fenw.get(mid) > s[i]) { high = mid; } else { low = mid + 1; } } a[i] = low + 1; fenw.modify(low, -low - 1); } for (int i = 0; i < n; i++) { if (i > 0) { cout << " "; } cout << a[i]; } cout << '\n'; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≤ s_{i} ≤ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} — the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e16 + 5; const int N = 1e6 + 5; long long first[N]; long long lazy[N] = {0}; long long s[N]; int ans[N]; void pull(int v) { first[v] = min(first[2 * v + 1], first[2 * v + 2]); } void apply(int v, long long val) { first[v] += val; lazy[v] += val; } void laziness(int v, int a, int b) { if (lazy[v] != 0) { apply(2 * v + 1, lazy[v]); apply(2 * v + 2, lazy[v]); lazy[v] = 0; } } void zbuduj(int v, int a, int b) { if (a > b) return; if (a == b) { first[v] = s[a]; return; } zbuduj(2 * v + 1, a, (a + b) / 2); zbuduj(2 * v + 2, (a + b) / 2 + 1, b); first[v] = min(first[2 * v + 1], first[2 * v + 2]); } void dodaj(int v, int a, int b, int i, int j, long long val) { if (b < i || a > j) { return; } else if (a >= i && b <= j) { apply(v, val); } else { laziness(v, a, b); dodaj(2 * v + 1, a, (a + b) / 2, i, j, val); dodaj(2 * v + 2, (a + b) / 2 + 1, b, i, j, val); pull(v); } } int get_min(int v, int a, int b) { if (a == b) return a; else { laziness(v, a, b); int x = -1; if (first[2 * v + 2] == 0) { x = get_min(2 * v + 2, (a + b) / 2 + 1, b); } else { x = get_min(2 * v + 1, a, (a + b) / 2); } pull(v); return x; } } int main() { int n; scanf("%d", &n); for (int i = 0; i < (n); ++i) scanf("%I64d", &s[i]); zbuduj(0, 0, n - 1); for (int i = (1); i <= (n); ++i) { int indeks = get_min(0, 0, n - 1); ans[indeks] = i; dodaj(0, 0, n - 1, indeks, indeks, INF); dodaj(0, 0, n - 1, indeks + 1, n - 1, -i); } 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 " ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author KharYusuf */ 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 { int n; long[] t; public void solve(int testNumber, FastReader s, PrintWriter w) { n = s.nextInt(); long[] a = new long[n]; t = new long[n + 2]; int[] ans = new int[n]; for (int i = 0; i < n; i++) { a[i] = s.nextLong(); modify(i + 1, i + 1); } for (int i = n - 1; i >= 0; i--) { int ind = bins(0, n - 1, a[i]); //w.println(ind+" ok"); //w.flush(); ans[i] = ind + 1; modify(ind + 1, -ind - 1); } for (int i : ans) w.print(i + " "); } public int bins(int l, int r, long val) { int mid, ans = r; while (l <= r) { mid = l + r >> 1; long m = query(mid); if (m == val) { l = mid + 1; ans = mid; } else if (m > val) { r = mid - 1; ans = r; } else { l = mid + 1; } } return ans; } public void modify(int ind, int val) { ind++; while (ind <= n) { t[ind] += val; ind += ind & (-ind); } } public long query(int ind) { ind++; long sum = 0; while (ind > 0) { sum += t[ind]; ind -= ind & (-ind); } return sum; } } 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 long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } 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 " ] }
CORRECT
java
import java.util.*; import java.io.*; import java.text.*; public class Gymaya { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int N = 1; while(N < n) N <<= 1; //padding long[] in = new long[N + 1]; long inf= (long) 1e17; Arrays.fill(in,inf); for(int i = 1; i <= n; i++) in[i] = sc.nextLong(); SegmentTree sg = new SegmentTree(in); int[]ans= new int[n+1]; for (int i =1;i<=n;i++){ int id= sg.query(1,n); ans[id]=i; sg.update_range(id+1,n,-i); sg.update_point(id,inf); } for (int i =1;i<=n;i++){ pw.print(ans[i]+" "); } pw.println(); pw.flush(); } static class SegmentTree { // 1-based DS, OOP int N; //the number of elements in the array as a power of 2 (i.e. after padding) int[] sTree; long[] array,lazy; SegmentTree(long[] in) { array = in; N = in.length - 1; sTree = new int[N << 1]; //no. of nodes = 2*N - 1, we add one to cross out index zero lazy = new long[N << 1]; build(1, 1, N); } void build(int node, int b, int e) // O(n) { if (b == e) sTree[node] = b; else { int mid = b + e >> 1; build(node << 1, b, mid); build(node << 1 | 1, mid + 1, e); if (array[sTree[node<<1]]<array[sTree[node << 1 | 1]]){ sTree[node]=sTree[node<<1]; } else sTree[node]=sTree[node<<1 | 1]; } } void update_point(int index, long val) // O(log n) { array[index]=val; index += N - 1; while (index > 1) { index >>= 1; propagate(index,0,0,0); if (array[sTree[index<<1]]<array[sTree[index << 1 | 1]]){ sTree[index]=sTree[index<<1]; } else sTree[index]=sTree[index << 1|1]; } } void update_range(int i, int j, int val) // O(log n) { update_range(1, 1, N, i, j, val); } void update_range(int node, int b, int e, int i, int j, int val) { if (i > e || j < b) return; if (b >= i && e <= j) { array[sTree[node]]+=val; lazy[node] += val; } else { int mid = b + e >> 1; propagate(node, b, mid, e); update_range(node << 1, b, mid, i, j, val); update_range(node << 1 | 1, mid + 1, e, i, j, val); if (array[sTree[node<<1]]<array[sTree[node << 1 | 1]]){ sTree[node]=sTree[node<<1]; } else sTree[node]=sTree[node<<1 | 1]; } } void propagate(int node, int b, int mid, int e) { lazy[node << 1] += lazy[node]; lazy[node << 1 | 1] += lazy[node]; if (sTree[node<<1]!=sTree[node]) array[sTree[node << 1]] += lazy[node]; if (sTree[node<<1 | 1 ]!=sTree[node]) array[sTree[node << 1 | 1]] += lazy[node]; lazy[node] = 0; } int query(int i, int j) { return query(1, 1, N, i, j); } int query(int node, int b, int e, int i, int j) // O(log n) { if (i > e || j < b) return -1; if (b >= i && e <= j) return sTree[node]; int mid = b + e >> 1; propagate(node, b, mid, e); int q1 = query(node << 1, b, mid, i, j); int q2 = query(node << 1 | 1, mid + 1, e, i, j); if (q1==-1)return q2; if (q2==-1)return q1; if (array[q1]<array[q2])return q1; return q2; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }