Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; const ll mod = 998244353; const ll INF = 1000000099; vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1}; template <typename T> struct BIT { int n; vector<T> bit; BIT() : n(-1) {} BIT(int n_, T d) : n(n_), bit(n_ + 1, d) {} T sum(int i) { T s = bit[0]; for (int x = i; x > 0; x -= (x & -x)) s += bit[x]; return s; } void add(int i, T a) { if (i == 0) return; for (int x = i; x <= n; x += (x & -x)) bit[x] += a; } int lower_bound(int w) { if (w <= 0) return 0; int x = 0, r = 1; while (r < n) r <<= 1; for (int k = r; k > 0; k >>= 1) { if (x + k <= n && bit[x + k] < w) { w -= bit[x + k]; x += k; } } return x + 1; } T sum0(int i) { return sum(i + 1); } void add0(int i, T a) { add(i + 1, a); } T query(int l, int r) { return sum(r - 1) - sum(l - 1); } T query0(int l, int r) { return sum(r) - sum(l); } }; ll BS(ll n, ll sub) { ll ok = -1 - sub, ng = 200001; while (abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; if (n < (mid * (mid + 1) / 2)) { ng = mid; } else { ok = mid; } } cout << "BS" << ok << endl; return ok; } signed main() { ll n; cin >> n; BIT<ll> b(n, 0); vector<ll> v(n), ans(n); for (ll i = 0; i < (ll)(n); i++) { cin >> v[i]; b.add(i + 1, i + 1); } for (int i = n - 1; 0 <= i; i--) { ll x = b.lower_bound(v[i] + 1); cout << x << endl; ans[i] = x; b.add(x, -x); } for (ll i = 0; i < (ll)(n); i++) cout << ans[i] << (i == n - 1 ? '\n' : ' '); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
python2
rr = raw_input rri = lambda: int(raw_input()) rrm = lambda: map(int, raw_input().split()) MOD = 10**9 + 7 # S_i : sum of elements before ith element # that are smaller than the ith element def solve(N, A): A = [[i,x] for i,x in enumerate(A)] ans = [0] * N c = 1 while A: for ix in xrange(len(A)-1,-1,-1): if A[ix][1] == 0: break for j in xrange(ix+1, len(A)): A[j][1] -= 1 i, x = A.pop(ix) ans[i] = c; c+=1 return ans N = rri() A = rrm() print " ".join(map(str, solve(N, A)))
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
// Don't place your source in a package import javax.swing.*; import java.lang.reflect.Array; import java.text.DecimalFormat; import java.util.*; import java.lang.*; import java.io.*; import java.math.*; import java.util.stream.Stream; // Please name your class Main public class Main { static FastScanner fs=new FastScanner(); static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int Int() { return Integer.parseInt(next()); } long Long() { return Long.parseLong(next()); } String Str(){ return next(); } } public static void main (String[] args) throws java.lang.Exception { PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int T=1; for(int t=0;t<T;t++){ int n=Int(); long A[]=new long[n]; for(int i=0;i<A.length;i++){ A[i]=Long(); } Solution sol=new Solution(out); sol.solution(A); } out.close(); } public static int Int(){ return fs.Int(); } public static long Long(){ return fs.Long(); } public static String Str(){ return fs.Str(); } } class Solution{ PrintWriter out; public Solution(PrintWriter out){ this.out=out; } public void solution(long A[]){ int n=A.length; int arr[]=new int[n]; int res[]=new int[n]; TreeSet<Integer>tree=new TreeSet<>(); for(int i=0;i<arr.length;i++){ arr[i]=i+1; tree.add(i); } FenWick fen=new FenWick(arr); for(int i=n-1;i>=0;i--){ int l=0,r=A.length-1; int pos=-1; while(l<=r){ int mid=l+(r-l)/2; long sum=fen.sumRange(0,mid); if(sum>=A[i]){ pos=mid; r=mid-1; } else{ l=mid+1; } } if(A[i]==0){ int first= tree.first(); res[i]=first+1; tree.remove(first); fen.update(first,-(first+1)); } else{ Integer high=tree.ceiling(pos); res[i]=high+1; fen.update(high,-(high+1)); tree.remove(high); } } for(int i:res){ out.print(i+" "); } } class FenWick { int tree[];//1-index based int A[]; int arr[]; public FenWick(int[] A) { this.A=A; arr=new int[A.length]; tree=new int[A.length+1]; int sum=0; for(int i=0;i<A.length;i++){ update(i,A[i]); } } public void update(int i, int val) { arr[i]=+val; i++; while(i<tree.length){ tree[i]+=val; i+=(i&-i); } } public long sumRange(int i, int j) { return pre(j+1)-pre(i); } public long pre(int i){ long sum=0; while(i>0){ sum+=tree[i]; i-=(i&-i); } return sum; } } } /* ;\ |' \ _ ; : ; / `-. /: : | | ,-.`-. ,': : | \ : `. `. ,'-. : | \ ; ; `-.__,' `-.| \ ; ; ::: ,::'`:. `. \ `-. : ` :. `. \ \ \ , ; ,: (\ \ :., :. ,'o)): ` `-. ,/,' ;' ,::"'`.`---' `. `-._ ,/ : ; '" `;' ,--`. ;/ :; ; ,:' ( ,:) ,.,:. ; ,:., ,-._ `. \""'/ '::' `:'` ,'( \`._____.-'"' ;, ; `. `. `._`-. \\ ;:. ;: `-._`-.\ \`. '`:. : |' `. `\ ) \ -hrr- ` ;: | `--\__,' '` ,' ,-' free bug dog */
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tree[800005], num[200005], ans[200005]; int n; int lowbit(int x) { return x & -x; } void add(int i, int value) { while (i <= n) { tree[i] += value; i += lowbit(i); } } int sum(int x) { int ans = 0; while (x) { ans += tree[x]; x -= lowbit(x); } return ans; } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> num[i]; add(i, i); } for (int i = n; i >= 1; i--) { int l = 1, r = n, mid; while (l < r) { mid = (l + r) / 2; if (sum(mid) <= num[i]) l = mid + 1; else r = mid; } ans[i] = r; add(r, -r); } for (int i = 1; i <= n; i++) cout << ans[i] << ' '; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class F { public static long[] min; public static long[] t; public static void push(int v){ t[v*2] += t[v]; t[v*2+1] += t[v]; t[v] = 0; } public static long get_min(int v, int vl, int vr, int ql, int qr){ if(ql <= vl && vr <= qr){ return get_val(v); } if(qr <= vl || ql >= vr){ return Long.MAX_VALUE; } push(v); long ans = Math.min(get_min(v*2, vl, (vl+vr)/2, ql, qr), get_min(v*2+1, (vl+vr)/2, vr, ql, qr)); min[v] = Math.min(get_val(v*2), get_val(v*2+1)); return ans; } public static int find(int v, int vl, int vr){ if(vl == vr - 1){ return v; } push(v); if(get_min(v * 2 + 1, (vl + vr) / 2, vr, (vl + vr) / 2, vr) == 0){ return find(v * 2 + 1, (vl + vr) / 2, vr); } else{ return find(v*2, vl, (vl+vr)/2); } } public static void add(int v, int vl, int vr, int ql, int qr, int x){ if(ql <= vl && qr >= vr){ t[v] += x; return; } else if(qr <= vl || ql >= vr){ return; } push(v); add(v*2, vl, (vl+vr)/2, ql, qr,x); add(v*2+1, (vl+vr)/2, vr, ql, qr,x); min[v] = Math.min(get_val(v*2), get_val(v*2+1)); } public static long get_val(int v){ long start = min[v]; return Math.max(start - t[v], 0); } public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); int n = Integer.parseInt(reader.readLine()); long[] array = new long[n]; StringTokenizer st = new StringTokenizer(reader.readLine()); for(int i = 0; i < n; ++i){ array[i] = Long.parseLong(st.nextToken()); } int new_n = n; int counter = 0; while(new_n > 0){ new_n /= 2; ++counter; } new_n = 1 << counter; min = new long[new_n * 2]; t = new long[new_n * 2]; for(int i = 0; i < min.length; ++i){ min[i] = Long.MAX_VALUE; } int[] ans = new int[n]; int now = 1; for(int i = new_n; i < new_n + n; ++i){ min[i] = array[i - new_n]; } for(int i = new_n - 1; i >= 0; --i){ min[i] = Math.min(min[i * 2], min[i * 2 + 1]); } for(int i = 1; i <= n; ++i){ int node = find(1, 0, new_n) - new_n; ans[node] = now; min[node + new_n] = Long.MAX_VALUE; int ver = node + new_n; while(ver != 1){ ver/= 2; min[ver] = Math.min(min[ver * 2], min[ver * 2 + 1]); } add(1, 0, new_n, node + 1, new_n, now); ++now; } for(int i = 0; i < n; ++i){ writer.print(ans[i] + " "); } writer.close(); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; 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(int n, T& t) { t.resize(n); } template <typename First, typename... Rest> void MACRO_rdv2_Init(int n, First& first, Rest&... rest) { first.resize(n); MACRO_rdv2_Init(n, rest...); } template <typename T> void MACRO_rdv2_Scan(int p, T& t) { std::cin >> t[p]; } template <typename First, typename... Rest> void MACRO_rdv2_Scan(int p, First& first, Rest&... rest) { std::cin >> first[p]; MACRO_rdv2_Scan(p, rest...); } template <typename T> void wrv(const vector<T>& v) { for (int(__ii) = (0); (__ii) < (((int)v.size())); ++(__ii)) { if (__ii) cout << ' '; cout << v[__ii]; } cout << '\n'; } template <typename T> void wrm(const vector<vector<T>>& v) { for (int(__ii) = (0); (__ii) < (((int)v.size())); ++(__ii)) { for (int(__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 int N = 2e5 + 10; const int inf = 2e9; int st_mn, st_mx; class segtree { private: struct node { int mn, mx; node(int mn = inf, int 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; } }; int n, ql, qr; vector<node> st; vector<int> lazy; void push(int sl, int sr, int 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(int sl, int sr, int at, int delta) { push(sl, sr, at); if (qr < sl || ql > sr) return; if (ql <= sl && qr >= sr) { lazy[at] += delta; push(sl, sr, at); return; } int mid = (sl + sr) >> 1; int 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(int sl, int sr, int at) { push(sl, sr, at); if (qr < sl || ql > sr) return node(); if (ql <= sl && qr >= sr) return st[at]; int mid = (sl + sr) >> 1; return get_recur(sl, mid, at << 1) + get_recur(mid + 1, sr, at << 1 | 1); } public: segtree() {} segtree(int _n) { init(_n); } void init(int _n) { this->n = _n; st.assign(n << 2, node(0, 0)); lazy.assign(n << 2, 0); } void add(int l, int r, int delta) { ql = l, qr = r; if (l > r) return; add_recur(0, n - 1, 1, delta); } void get(int l, int r) { ql = l, qr = r; node tmp = get_recur(0, n - 1, 1); st_mn = tmp.mn, st_mx = tmp.mx; } }; int find_zero(int n, segtree* st) { int low = 0, high = n - 1; while (low < high) { int mid = (low + high + 1) / 2; st->get(mid, high); if (st_mn == 0) low = mid; else high = mid - 1; } return low; } void solve() { int n; sc(n); vector<int> a(n); for (int(__ii) = (0); (__ii) < (n); ++(__ii)) cin >> a[__ii]; segtree* st = new segtree(n); for (int(i) = (0); (i) < (n); ++(i)) st->add(i, i, a[i]); vector<int> ans(n); for (int(i) = (1); (i) < (n + 1); ++(i)) { int 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() { int 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 " ] }
IN-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=big; 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 " ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class RestorePermutation { static class FenwickTree { int n; int[] BIT; FenwickTree(int n) { this.n = n; BIT = new int[n + 1]; } void update(int i, int x) { for (; i <= n; i += i&-i) { BIT[i] += x; } } int searchNum(int prefSum) { int num = 0; int 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[1 << i]; } } return num + 1; } } 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(); int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = in.nextInt(); FenwickTree ft = new FenwickTree(n); for (int i = 1; i <= n; i++) { ft.update(i, i); } int[] p = new int[n + 1]; for (int i = n; i >= 1; i--) { p[i] = ft.searchNum(a[i]); 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 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool ok = true; long long N, Q; long long st[800000]; long long lazy[800000]; long long arvore[800000]; void push(int lo, int hi, int node) { if (lazy[node] == 0) return; st[node] += lazy[node]; if (lo != hi) { lazy[2 * node + 1] += lazy[node]; lazy[2 * node + 2] += lazy[node]; } lazy[node] = 0; } void update_range(int s, int e, long long x, int lo = 0, int hi = -1, int node = 0) { if (hi == -1) hi = N - 1; push(lo, hi, node); if (hi < s || lo > e) return; if (lo >= s && hi <= e) { lazy[node] = x; push(lo, hi, node); return; } int mid = (lo + hi) / 2; update_range(s, e, x, lo, mid, 2 * node + 1); update_range(s, e, x, mid + 1, hi, 2 * node + 2); st[node] = max(st[2 * node + 1], st[2 * node + 2]); } long long query(long long s, int lo = 0, int hi = -1, int node = 0) { if (hi == -1) hi = N - 1; push(lo, hi, node); if (st[node] < s) return -10; if (lo == hi) { if (st[node] != s) return -10; return arvore[node]; } long long res = -1; int mid = (lo + hi) / 2; push(lo, hi, 2 * node + 1); if (st[2 * node + 1] < s) return query(s, mid + 1, hi, 2 * node + 2); if (st[2 * node + 1] > s) return query(s, lo, mid, 2 * node + 1); res = max(query(s, lo, mid, 2 * node + 1), query(s, mid + 1, hi, 2 * node + 2)); return res; } void atualiza(int v, int i, int j, int pos, int val) { if (pos > j || pos < i) return; if (i == j) { arvore[v] = val; return; } int mid = (j + i) / 2; atualiza(2 * v + 1, i, mid, pos, val), atualiza(2 * v + 2, mid + 1, j, pos, val); arvore[v] = -1; } int main() { cin >> N; long long c = 0; for (long long i = 0; i < N; i++) { c += i; update_range(i, i, c); } for (int i = 0; i < N; i++) { atualiza(0, 0, N - 1, i, i + 1); } ok = false; vector<long long> V, res; for (int i = 0, x; i < N; i++) { cin >> x; V.push_back(x); } long long x; for (int i = N - 1; i >= 0; i--) { x = query(V[i]); res.push_back(x); update_range(x, N - 1, -x); } for (int i = N - 1; i >= 0; i--) { printf("%lld ", res[i]); } printf("\n"); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
/* Code for task B by detestmaths */ import java.io.*; import java.util.*; import static java.lang.Math.*; public class Solver { public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); // FastScanner in = new FastScanner("search.in"); //PrintWriter out = new PrintWriter("search.out"); int n = in.nextInt(); long s[] = new long[n]; for (int i = 0; i < n; i++) { s[i] = in.nextLong(); } segment_tree st = new segment_tree(s); int ans[] = new int[n]; for (int i = 1; i < n + 1; i++) { pair p = st.get(0, n - 1); int ind = p.ind; ans[ind] = i; st.add(ind, ind, 1010101); st.add(ind + 1, n - 1, -i); } for (int i = 0; i < n; i++) { out.print(ans[i] + " "); } out.close(); } } class segment_tree { long min[]; int id[]; long a[]; long sum[]; segment_tree(long a[]) { this.a = a; int n = a.length; min = new long[4 * n]; sum = new long[4 * n]; id = new int[4 * n]; build(1, 0, n - 1); } void build(int v, int l, int r) { if (l == r) { min[v] = a[l]; id[v] = l; return; } int m = ((l + r) >> 1); build(v * 2, l, m); build(v * 2 + 1, m + 1, r); if (min[v * 2] < min[v * 2 + 1]){ min[v] = min[v*2]; id[v] = id[v * 2]; }else if(min[v*2+1] < min[v*2]){ min[v] = min[v*2+1]; id[v] = id[v*2+1]; }else{ min[v] = min[v*2]; id[v] = max(id[v*2],id[v*2+1]); } } void push(int v) { min[v] += sum[v]; sum[v * 2] += sum[v]; sum[v * 2 + 1] += sum[v]; sum[v] = 0; } void upd(int v) { long p1 = get_el(v * 2); long p2 = get_el(v * 2 + 1); if (p1 < p2) { min[v] = p1; id[v] = id[v * 2]; } else if (p1 > p2) { min[v] = p2; id[v] = id[v * 2 + 1]; } else { min[v] = p1; id[v] = max(id[v * 2], id[v * 2 + 1]); } } long get_el(int v) { return min[v] + sum[v]; } void add(int l, int r, long x) { add(1, 0, a.length - 1, l, r, x); } void add(int v, int l, int r, int left, int right, long x) { if (l > right || r < left) return; if (l >= left && r <= right) { sum[v] += x; return; } push(v); int m = ((l + r) >> 1); add(v * 2, l, m, left, right, x); add(v * 2 + 1, m + 1, r, left, right, x); upd(v); } pair get(int l, int r) { return get(1, 0, a.length - 1, l, r); } pair combine(pair a, pair b) { if (a.min < b.min) return a; if (b.min < a.min) return b; return new pair(a.min, max(a.ind, b.ind)); } pair get(int v, int l, int r, int left, int right) { if (l > right || r < left) return new pair((long) 1e10, -1); if (l >= left && r <= right) { return new pair(get_el(v), id[v]); } push(v); int m = ((l + r) >> 1); pair p1 = get(v * 2, l, m, left, right); pair p2 = get(v * 2 + 1, m + 1, r, left, right); pair q = combine(p1, p2); upd(v); return q; } } class pair { long min; int ind; public pair(long min, int ind) { this.min = min; this.ind = ind; } } class FastScanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); public FastScanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public FastScanner(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } public String next() throws IOException { if (!st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, m, q, x, k, t, y, w = 2, z, a[200500], ans[200500]; set<long long> s; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 0; i < n; ++i) cin >> a[i], s.insert(i + 1); for (int i = n - 1; i >= 0; --i) { k = sqrt(w * a[i]); x = *(s.upper_bound(k)); ans[i] = x; s.erase(x); } for (int i = 0; i < n; ++i) cout << ans[i] << " "; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int gi() { char cc = getchar(); int cn = 0, flus = 1; while (cc < '0' || cc > '9') { if (cc == '-') flus = -flus; cc = getchar(); } while (cc >= '0' && cc <= '9') cn = cn * 10 + cc - '0', cc = getchar(); return cn * flus; } const int inf = 12345678; const int N = 2e5 + 5; int Mi[N << 2], tag[N << 2], n, A[N]; void build(int x, int l, int r) { if (l == r) { Mi[x] = A[l]; return; } int mid = (l + r) >> 1; build((x << 1), l, mid), build((x << 1 | 1), mid + 1, r), Mi[x] = min(Mi[(x << 1)], Mi[(x << 1 | 1)]); } void pushmark(int x) { if (tag[x]) { tag[(x << 1)] += tag[x], tag[(x << 1 | 1)] += tag[x], Mi[(x << 1)] += tag[x], Mi[(x << 1 | 1)] += tag[x], tag[x] = 0; } } int Query(int x, int l, int r) { if (l == r) return l; int mid = (l + r) >> 1; pushmark(x); if (!Mi[(x << 1 | 1)]) return Query((x << 1 | 1), mid + 1, r); else return Query((x << 1), l, mid); } void Update(int x, int l, int r, int ql, int qr, int k) { if (l > qr || r < ql || ql > qr) return; if (ql <= l && r <= qr) { tag[x] += k, Mi[x] += k; return; } int mid = (l + r) >> 1; pushmark(x); Update((x << 1), l, mid, ql, qr, k), Update((x << 1 | 1), mid + 1, r, ql, qr, k); Mi[x] = min(Mi[(x << 1)], Mi[(x << 1 | 1)]); } void Change(int x, int l, int r, int pos) { if (l == r) { Mi[x] = inf; return; } int mid = (l + r) >> 1; pushmark(x); if (mid >= pos) Change((x << 1), l, mid, pos); else Change((x << 1 | 1), mid + 1, r, pos); Mi[x] = min(Mi[(x << 1)], Mi[(x << 1 | 1)]); } signed main() { n = gi(); for (register int i = 1; i <= n; ++i) A[i] = gi(); build(1, 1, n); for (register int i = 1; i <= n; ++i) { int l = Query(1, 1, n); A[l] = i, Update(1, 1, n, l + 1, n, -i), Change(1, 1, n, l); } for (register int i = 1; i <= n; ++i) printf("%d ", A[i]); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long double EPS = 1e-9, PI = acos(-1.); const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const long long LINF = 0x3f3f3f3f3f3f3f3f; const int N = 2e5 + 5; int n, ans[N]; long long mi[4 * N], pos[4 * N], v[N], lz[4 * N]; void build(int p, int l, int r) { if (l == r) { mi[p] = v[l], pos[p] = l; return; } build(2 * p, l, (l + r) / 2); build(2 * p + 1, (l + r) / 2 + 1, r); mi[p] = min(mi[2 * p], mi[2 * p + 1]); if (mi[p] == mi[2 * p + 1]) pos[p] = pos[2 * p + 1]; else pos[p] = pos[2 * p]; } void push(int p, int l, int r) { if (lz[p]) { mi[p] += lz[p]; if (l != r) { lz[2 * p] += lz[p]; lz[2 * p + 1] += lz[p]; } lz[p] = 0; } } pair<long long, long long> query(int p, int l, int r, int i, int j) { push(p, l, r); if (r < i or l > j) return {LINF, 0}; if (l >= i and r <= j) return {mi[p], pos[p]}; pair<long long, long long> a = query(2 * p, l, (l + r) / 2, i, j); pair<long long, long long> b = query(2 * p + 1, (l + r) / 2 + 1, r, i, j); if (a.first < b.first) return a; else return b; } void update(int p, int l, int r, int i, int j, int v) { push(p, l, r); if (r < i or l > j) return; if (l >= i and r <= j) { lz[p] = v; push(p, l, r); return; } update(2 * p, l, (l + r) / 2, i, j, v); update(2 * p + 1, (l + r) / 2 + 1, r, i, j, v); mi[p] = min(mi[2 * p], mi[2 * p + 1]); if (mi[p] == mi[2 * p + 1]) pos[p] = pos[2 * p + 1]; else pos[p] = pos[2 * p]; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &v[i]); memset(ans, -1, sizeof(ans)); for (int i = 1; i <= n; i++) printf("%lld ", ans[i]); printf("\n"); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
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] + 1)); } 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 " ] }
IN-CORRECT
java
import java.util.*; import java.io.*; import java.math.*; import java.awt.geom.*; import static java.lang.Math.*; public class Main implements Runnable { boolean multiiple = false; class Pair implements Comparable<Pair> { int val, pos; Pair(int a, int b) { val = a; pos = b; } @Override public int compareTo(Pair o) { if (val == o.val) return o.pos - pos; return val - o.val; } } void solve() throws Exception { int n = sc.nextInt(); PriorityQueue<Pair> pq = new PriorityQueue<>(); for (int i = 0; i < n; i++) pq.add(new Pair(sc.nextInt(), i)); int count = 1; int[] ans = new int[n]; while (!pq.isEmpty()) { Pair next = pq.poll(); ans[next.pos] = count; count++; } for (int an : ans) { System.out.print(an + " "); } } @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new FastScanner(in); if (multiiple) { int q = sc.nextInt(); for (int i = 0; i < q; i++) solve(); } else solve(); } catch (Throwable uncaught) { Main.uncaught = uncaught; } finally { out.close(); } } public static void main(String[] args) throws Throwable { Thread thread = new Thread(null, new Main(), "", (1 << 26)); thread.start(); thread.join(); if (Main.uncaught != null) { throw Main.uncaught; } } static Throwable uncaught; BufferedReader in; FastScanner sc; PrintWriter out; } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int P[200001]; ll sum[200001]; ll fen[200001]; int ans[200001]; void build() { for (int i = 1; i <= 200000; i++) { sum[i] = sum[i - 1] + ll(i - 1); fen[i] = sum[i] - sum[i - (i & -i)]; } } int query(int l, int r) { if (l != 1) return query(1, r) - query(1, l - 1); ll ans = 0LL; while (r > 0) { ans += fen[r]; r -= r & -r; } return ans; } void update(int i) { int taking = i; i++; while (i <= 200000) { fen[i] -= taking; i += (i & -i); } } int find(ll s) { ll cur = 0LL; int ind = 0; for (int b = 17; b >= 0; b--) { if (ind + (1 << b) <= 200000 && cur + fen[ind + (1 << b)] <= s) { cur += fen[ind + (1 << b)]; ind += (1 << b); } } return ind; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> P[i]; build(); for (int i = n; i >= 1; i--) { ans[i] = find(P[i]); update(ans[i]); } for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } cout << endl; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
import java.util.*; import java.io.*; import java.math.*; import java.awt.geom.*; import static java.lang.Math.*; public class Main implements Runnable { boolean multiiple = false; class Pair implements Comparable<Pair> { long val; int pos; Pair(long a, int b) { val = a; pos = b; } @Override public int compareTo(Pair o) { if (val == o.val) return o.pos - pos; if (val - o.val > 0) return 1; if (val - o.val < 0) return -1; return 0; } @Override public String toString() { return "pulled " + val + ", " + pos; } } void solve() throws Exception { int n = sc.nextInt(); TreeSet<Pair> pq = new TreeSet<>(); for (int i = 0; i < n; i++) pq.add(new Pair(sc.nextLong(), i)); int count = 1; int[] ans = new int[n], other = new int[n+1]; long tempa = -1; while (count <= n) { Pair next = pq.first(); if (next.pos > 0 || tempa != -1) { pq.pollFirst(); ans[next.pos] = count; other[count] = next.pos; count++; } else { Pair temp = pq.pollFirst(); for (int i = count; i >= 1; i--) { int sum = ((i - 1) * i) / 2; int pos = other[count - i + 1]; Pair search = new Pair(sum, pos); Pair result = pq.floor(search); if (result != null) { tempa = 1; pq.remove(result); pq.add(new Pair(-1, result.pos)); pq.add(temp); break; } } tempa = 1; pq.add(new Pair(-1, 0)); } } for (int an : ans) System.out.print(an + " "); } @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new FastScanner(in); if (multiiple) { int q = sc.nextInt(); for (int i = 0; i < q; i++) solve(); } else solve(); } catch (Throwable uncaught) { Main.uncaught = uncaught; } finally { out.close(); } } public static void main(String[] args) throws Throwable { Thread thread = new Thread(null, new Main(), "", (1 << 26)); thread.start(); thread.join(); if (Main.uncaught != null) { throw Main.uncaught; } } static Throwable uncaught; BufferedReader in; FastScanner sc; PrintWriter out; } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long f(long long x) { return (x * (x + 1)) / 2; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; map<long long, stack<int> > mapper; for (int i = 0; i < n; i++) { long long x; cin >> x; mapper[x].push(i); } vector<int> ans(n); for (int i = 0; i < n; i++) { int c = f(i); auto it = mapper.upper_bound(c); it--; stack<int> &st = (*it).second; int pos = st.top(); st.pop(); ans[pos] = i; if (((int)st.size()) == 0) mapper.erase(it); } for (int i = 0; i < n; i++) cout << ans[i] + 1 << " "; cout << '\n'; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename S, typename T> ostream& operator<<(ostream& out, pair<S, T> const& p) { out << '(' << p.first << ", " << p.second << ')'; return out; } template <typename T> ostream& operator<<(ostream& out, vector<T> const& v) { long long l = v.size(); for (long long i = 0; i < l - 1; i++) out << v[i] << ' '; if (l > 0) out << v[l - 1]; return out; } template <typename T> void trace(const char* name, T&& arg1) { cout << name << " : " << arg1 << "\n"; } template <typename T, typename... Args> void trace(const char* names, T&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; trace(comma + 1, args...); } const long long N = 2e5 + 100; long long curr = 1; long long n, arr[N]; struct data { long long mn; long long ind; data() : mn(1e9){}; }; struct SegTree { long long N; vector<data> st; vector<bool> cLazy; vector<long long> lazy; void init(long long n) { N = n; st.resize(4 * N + 5); cLazy.assign(4 * N + 5, false); lazy.assign(4 * N + 5, 0); } void merge(data& cur, data& l, data& r) { if (l.mn < r.mn) { cur.mn = l.mn; cur.ind = l.ind; } else { cur.mn = r.mn; cur.ind = r.ind; } } void propagate(long long node, long long L, long long R) { if (L != R) { cLazy[node * 2] = 1; cLazy[node * 2 + 1] = 1; lazy[node * 2] += lazy[node]; lazy[node * 2 + 1] += lazy[node]; } st[node].mn += lazy[node]; cLazy[node] = 0; } void build(long long node, long long L, long long R) { if (L == R) { st[node].ind = curr; st[node].mn = arr[curr]; curr++; return; } long long M = (L + R) / 2; build(node * 2, L, M); build(node * 2 + 1, M + 1, R); merge(st[node], st[node * 2], st[node * 2 + 1]); } data Query(long long node, long long L, long long R, long long i, long long j) { if (cLazy[node]) propagate(node, L, R); if (j < L || i > R) return data(); if (i <= L && R <= j) return st[node]; long long M = (L + R) / 2; data left = Query(node * 2, L, M, i, j); data right = Query(node * 2 + 1, M + 1, R, i, j); data cur; merge(cur, left, right); return cur; } data pQuery(long long node, long long L, long long R, long long pos) { if (cLazy[node]) propagate(node, L, R); if (L == R) return st[node]; long long M = (L + R) / 2; if (pos <= M) return pQuery(node * 2, L, M, pos); else return pQuery(node * 2 + 1, M + 1, R, pos); } void Update(long long node, long long L, long long R, long long i, long long j, long long val) { if (cLazy[node]) propagate(node, L, R); if (j < L || i > R) return; if (i <= L && R <= j) { cLazy[node] = 1; lazy[node] += val; propagate(node, L, R); return; } long long M = (L + R) / 2; Update(node * 2, L, M, i, j, val); Update(node * 2 + 1, M + 1, R, i, j, val); merge(st[node], st[node * 2], st[node * 2 + 1]); } void pUpdate(long long node, long long L, long long R, long long pos, long long val) { if (cLazy[node]) propagate(node, L, R); if (L == R) { cLazy[node] = 1; lazy[node] = val; propagate(node, L, R); return; } long long M = (L + R) / 2; if (pos <= M) pUpdate(node * 2, L, M, pos, val); else pUpdate(node * 2 + 1, M + 1, R, pos, val); merge(st[node], st[node * 2], st[node * 2 + 1]); } data query(long long pos) { return pQuery(1, 1, N, pos); } data query(long long l, long long r) { return Query(1, 1, N, l, r); } void update(long long pos, long long val) { pUpdate(1, 1, N, pos, val); } void update(long long l, long long r, long long val) { Update(1, 1, N, l, r, val); } }; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; SegTree s; s.init(n); for (long long i = 1; i <= n; i++) { cin >> arr[i]; } s.build(1, 1, s.N); vector<long long> ans(N); long long lol = 1; for (long long i = 1; i <= n; i++) { auto get = s.query(1, n); ans[get.ind] = lol; s.update(get.ind, n, -lol); s.update(get.ind, 1000000000000000001); lol++; } for (long long i = 1; i <= n; i++) { cout << ans[i] << " "; } cout << "\n"; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; #define TRACE(x) x #define WATCH(x) TRACE(cout << #x" = " << x << endl) #define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl) #define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());}) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vll; typedef vector<vll> vvll; typedef vector<bool> vb; typedef vector<string> vs; const int MAXN = 200500; const int S = (1 << 18); const int MAXS = 2 * S + 5; ll ans[MAXN]; ll seg[MAXS]; void update(int loc, int add){ loc += S; while (loc){ seg[loc] += add; loc /= 2; } } int find(ll sm){ int loc = 1; while (loc < S){ if (seg[2 * loc] <= sm){ sm -= seg[2 * loc]; loc = 2 * loc + 1; } else { loc = 2 * loc; } } return loc - S; } int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); int n; cin >> n; vi a(n); for (int i = 0; i < n; i++){ cin >> a[i]; } for (int i = 0; i < MAXS; i++){ seg[i] = 0; } for (int i = 1; i <= n; i++){ update(i, i); } for (int i = n-1; i >= 0; i--){ ans[i] = find(a[i]); update(ans[i], -ans[i]); } for (int i = 0; i < n; i++){ cout << ans[i] << " "; } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = ~0ull >> 1; long long _min[2000005 * 5], add[2000005 * 5]; int n; long long s[2000005]; void pushup(int o) { _min[o] = min(_min[o << 1], _min[o << 1 | 1]); } void pushdown(int o) { if (add[o]) { add[o << 1] = add[o << 1 | 1] = add[o]; _min[o << 1] += add[o << 1]; _min[o << 1 | 1] += add[o << 1 | 1]; add[o] = 0; } } void build(int o, int l, int r) { if (l == r) { _min[o] = s[l]; return; } int m = (l + r) >> 1; build(o << 1, l, m); build(o << 1 | 1, m + 1, r); pushup(o); } void update(int o, int l, int r, int ql, int qr, long long v) { if (ql <= l && qr >= r) { add[o] += v; _min[o] += v; return; } pushdown(o); int m = (l + r) >> 1; if (ql <= m && qr >= l) update(o << 1, l, m, ql, qr, v); if (ql <= r && qr >= m + 1) update(o << 1 | 1, m + 1, r, ql, qr, v); pushup(o); } int query(int o, int l, int r, int v) { if (l == r) { return l; } pushdown(o); int m = (l + r) >> 1; if (_min[o << 1 | 1] == v) return query(o << 1 | 1, m + 1, r, v); else return query(o << 1, l, m, v); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { cin >> s[i]; } build(1, 1, n); vector<int> ans; for (int i = 0; i < n; i++) { long long v = _min[1]; int pos = query(1, 1, n, v); ans.push_back(pos); update(1, 1, n, pos, pos, INF); if (pos < n) update(1, 1, n, pos + 1, n, -1); } for (int i = 0; i < n; i++) { if (i) cout << " "; cout << ans[i]; } cout << endl; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using vvl = vector<vl>; const ll INF = 1LL << 60; const ll MOD = 1000000007; template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } template <class C> void print(const C &c, std::ostream &os = std::cout) { std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, " ")); os << std::endl; } template <typename T = int> struct BinaryIndexedTree { int n; vector<T> bit; BinaryIndexedTree(int n_, T init = 0) : n(n_), bit(n_ + 1, init) {} BinaryIndexedTree(vector<T> init) : n(init.size() + 1), bit(init.size() + 1) { for (int i = 1; i < init.size() + 1; ++i) { bit[i] = init[i - 1]; } } T sum(int i) { i++; T s = bit[0]; for (int x = i; x > 0; x -= (x & -x)) s += bit[x]; return s; } void add(int i, T a) { i++; if (i == 0) return; for (int x = i; x <= n; x += (x & -x)) bit[x] += a; } int lower_bound(int w) { w++; if (w <= 0) return 0; int x = 0, r = 1; while (r < n) r <<= 1; for (int k = r; k > 0; k >>= 1) { if (x + k <= n && bit[x + k] < w) { w -= bit[x + k]; x += k; } } return x; } T query(int l, int r) { return sum(r - 1) - sum(l - 1); } }; int main() { ll n; cin >> n; vl s(n); for (int i = 0; i < n; ++i) { cin >> s[i]; } deque<int> ret; BinaryIndexedTree<int> ints(n + 1); for (int i = 0; i <= n; ++i) { ints.add(i, i + 1); } ll sum = n * (n + 1) / 2; for (int i = n - 1; i >= 0; --i) { int idx = ints.lower_bound(s[i]); ret.push_front(idx); ints.add(idx - 1, -(idx)); sum -= idx; } print(ret); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") using namespace std; void redirectIO() { ios::sync_with_stdio(false); cin.tie(0); } long long mVal[1100000]; long long upd[1100000]; long long s[210000]; void refr(int a) { mVal[a] = min(mVal[a * 2], mVal[a * 2 + 1]) + upd[a]; } void push(int a) { mVal[a * 2] += upd[a]; upd[a * 2] += upd[a]; mVal[a * 2 + 1] += upd[a]; upd[a * 2 + 1] += upd[a]; upd[a] = 0; } void build(int a, int beg, int end) { if (beg == end) { mVal[a] = s[beg]; return; } build(a * 2, beg, (beg + end) / 2); build(a * 2 + 1, (beg + end) / 2 + 1, end); refr(a); } int targ_end; int targ_beg; long long targ_val; void update(int a, int beg, int end) { if (beg > targ_end || end < targ_beg) return; if (beg >= targ_beg && end <= targ_end) { upd[a] += targ_val; mVal[a] += targ_val; return; } push(a); update(a * 2, beg, (beg + end) / 2); update(a * 2 + 1, (beg + end) / 2 + 1, end); refr(a); } int lastZero(int a, int beg, int end) { if (beg == end) { return beg; } push(a); if (mVal[a * 2 + 1] == 0) { return lastZero(a * 2 + 1, (beg + end) / 2 + 1, end); } return lastZero(a * 2, beg, (beg + end) / 2); } int main() { redirectIO(); int n; cin >> n; for (int i = 1; i <= (n); i++) cin >> s[i]; build(1, 1, n); for (int i = 0; i < (n); i++) { int a = lastZero(1, 1, n); cout << a << " "; targ_beg = a; targ_end = n; targ_val = -a; update(1, 1, n); targ_end = a; targ_val = 1e17; update(1, 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 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using vvl = vector<vl>; const ll INF = 1LL << 60; const ll MOD = 1000000007; template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } template <class C> void print(const C &c, std::ostream &os = std::cout) { std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, " ")); os << std::endl; } template <typename T = int> struct BinaryIndexedTree { int n; vector<T> bit; BinaryIndexedTree(int n_, T init = 0) : n(n_), bit(n_ + 1, init) {} BinaryIndexedTree(vector<T> init) : n(init.size() + 1), bit(init.size() + 1) { for (int i = 1; i < init.size() + 1; ++i) { bit[i] = init[i - 1]; } } T sum(int i) { i++; T s = bit[0]; for (int x = i; x > 0; x -= (x & -x)) s += bit[x]; return s; } void add(int i, T a) { i++; if (i == 0) return; for (int x = i; x <= n; x += (x & -x)) bit[x] += a; } int lower_bound(int w) { if (w <= 0) return 0; int x = 0, r = 1; while (r < n) r <<= 1; for (int k = r; k > 0; k >>= 1) { if (x + k <= n && bit[x + k] < w) { w -= bit[x + k]; x += k; } } return x + 1; } T query(int l, int r) { return sum(r - 1) - sum(l - 1); } }; int main() { ll n; cin >> n; vl s(n); for (int i = 0; i < n; ++i) { cin >> s[i]; } deque<int> ret; BinaryIndexedTree<ll> ints(n + 1); for (int i = 0; i <= n; ++i) { ints.add(i, i + 1); } ll sum = n * (n + 1) / 2; for (int i = n - 1; i >= 0; --i) { int idx = ints.lower_bound(s[i]); ret.push_front(idx); ints.add(idx - 1, -(idx)); sum -= idx; } print(ret); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; const int INF = 0x3f3f3f3f; const long long LLINF = 0x3f3f3f3f3f3f3f3f; const int MOD = 1e9 + 7; int segtree[MAXN << 2]; int lazy[MAXN << 2]; int arr[MAXN]; int ans[MAXN]; void pushup(int rt) { segtree[rt] = min(segtree[rt << 1], segtree[rt << 1 | 1]); } void pushdown(int rt) { if (lazy[rt]) { segtree[rt << 1] = max(0, segtree[rt << 1] - lazy[rt]); segtree[rt << 1 | 1] = max(0, segtree[rt << 1 | 1] - lazy[rt]); lazy[rt << 1] += lazy[rt]; lazy[rt << 1 | 1] += lazy[rt]; lazy[rt] = 0; } } void build(int l, int r, int rt) { if (l == r) { segtree[rt] = arr[l]; return; } int m = l + r >> 1; build(l, m, rt << 1); build(m + 1, r, rt << 1 | 1); pushup(rt); } int query(int l, int r, int rt) { if (l == r) { return l; } int m = l + r >> 1; pushdown(rt); if (!segtree[rt << 1 | 1]) return query(m + 1, r, rt << 1 | 1); else return query(l, m, rt << 1); } void update(int l, int r, int L, int R, int C, int rt) { if (L <= l && r <= R) { segtree[rt] = max(0, segtree[rt] - C); lazy[rt] += C; return; } int m = l + r >> 1; pushdown(rt); if (L <= m) update(1, m, L, R, C, rt << 1); if (R > m) update(m + 1, r, L, R, C, rt << 1 | 1); pushup(rt); } void update(int l, int r, int pos, int rt) { if (l == r && l == pos) { segtree[rt] = INF; return; } int m = l + r >> 1; pushdown(rt); if (pos <= m) update(l, m, pos, rt << 1); else update(m + 1, r, pos, rt << 1 | 1); pushup(rt); } signed main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &arr[i]); } build(1, n, 1); int now = 0; for (int i = n; i > 0; --i) { int index = query(1, n, 1); ans[index] = ++now; if (index + 1 <= n) update(index + 1, n, 1, n, now, 1); update(1, n, index, 1); } for (int i = 1; i <= n; ++i) printf("%d ", ans[i]); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long int b[200009], c[400035], a[200009], d[200009]; long long int create(long long int N) { for (long long int i = N; i < 2 * N; i++) { c[i] = b[i - N]; } for (long long int i = N - 1; i > 0; i--) c[i] = c[2 * i + 1] + c[2 * i]; } long long int modify(long long int K, long long int N) { long long int t = N + K - 1; c[t] = 0; while (t > 0) { t /= 2; c[t] = c[2 * t] + c[2 * t + 1]; } } int query(long long int l, long long int r, long long int N) { l = l + N - 1; r = r + N - 1; long long int sum = 0; while (l <= r) { if (l & 1) sum += c[l]; if (!(r & 1)) sum += c[r]; l = (l + 1) / 2; r = (r - 1) / 2; } return sum; } int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); long long int N, t; cin >> N; long long int i; for (i = 0; i < 200009; i++) b[i] = i; for (i = 1; i <= N; i++) cin >> a[i]; create(N + 1); for (i = N; i > 0; i--) { long long int l = 1, r = N + 1, mid; while (l <= r) { mid = (l + r) / 2; if (query(1, mid, N + 1) > a[i]) r = mid - 1; else if (query(1, mid, N + 1) <= a[i]) l = mid + 1; } if (query(1, l, N + 1) == a[i]) t = l; else t = l - 1; d[i] = t; modify(t + 1, N + 1); } for (i = 1; i <= N; i++) cout << d[i] << " "; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
python3
import sys def get_rightmost_zero(input_values, res): for i in range(len(input_values) - 1, -1, -1): if input_values[i] == 0 and res[i] == 0: return i def main(): a = sys.stdin.read() a = a.split('\n') N = int(a[0]) input_values = [int(lol) for lol in a[1].split(' ')] res = [0 for i in input_values] for i in range(1, N + 1): index = get_rightmost_zero(input_values, res) res[index] = i for j in range(index + 1, N): input_values[j] -= i print(res) main()
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; class { public: pair<long long, long long> T[200009 << 2]; long long lz[200009 << 2]; int n, f[200009], ans[200009]; void solve() { cin >> n; for (int i = 1; i <= n; ++i) cin >> f[i]; build(1, 1, n); for (int i = 1; i <= n; ++i) { auto res = query(1, 1, n, 1, n); ans[res.second] = i; update(1, 1, n, res.second + 1, n, -i); update(1, 1, n, res.second, res.second, 1e18); } for (int i = 1; i <= n; ++i) cout << ans[i] << " "; } void build(int rt, int l, int r) { lz[rt] = 0; if (l == r) { T[rt] = {f[l], l}; return; } int mid = l + r >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); up(rt); } void up(int rt) { if (T[rt << 1].first == T[rt << 1 | 1].first) { T[rt] = T[rt << 1 | 1]; } else { T[rt] = min(T[rt << 1], T[rt << 1 | 1]); } } void update(int rt, int l, int r, int ll, int rr, int v) { if (ll > rr) return; if (ll <= l && rr >= r) { lz[rt] += v; T[rt].first += v; return; } int mid = l + r >> 1; down(rt); if (ll <= mid) update(rt << 1, l, mid, ll, rr, v); if (rr > mid) update(rt << 1 | 1, mid + 1, r, ll, rr, v); up(rt); } pair<long long, long long> query(int rt, int l, int r, int ll, int rr) { if (ll <= l && rr >= r) { return T[rt]; } int mid = l + r >> 1; down(rt); pair<long long, long long> res = {1e18, 1e18}; if (ll <= mid) { pair<long long, long long> ret = query(rt << 1, l, mid, ll, rr); if (ret.first == res.first) res.second = ret.second; else res = min(res, ret); } if (rr > mid) { pair<long long, long long> ret = query(rt << 1 | 1, mid + 1, r, ll, rr); if (ret.first == res.first) res.second = ret.second; else res = min(res, ret); } return res; } void down(int rt) { if (lz[rt]) { T[rt << 1].first += lz[rt]; lz[rt << 1] += lz[rt]; T[rt << 1 | 1].first += lz[rt]; lz[rt << 1 | 1] += lz[rt]; lz[rt] = 0; } } } NSPACE; int main() { ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); NSPACE.solve(); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
import java.io.*; import java.util.StringTokenizer; public class C { static int find(int i, int[] nxt) { return nxt[i] = i == nxt[i] ? i : find(nxt[i], nxt); } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); long[] base = new long[n + 1]; long s = 0; int[] nxt = new int[n + 1]; for (int i = 1; i <= n; ++i) { base[i] = s; s += i; nxt[i] = i; } long[] a = new long[n]; for(int i = 0; i < n; ++i) { a[i] = sc.nextLong(); } FenwickTree ft = new FenwickTree(n); int[] ans = new int[n]; for(int i = n - 1; i >= 0; --i) { int res = -1, lo = 1, hi = n; while(lo <= hi) { int mid = (lo + hi) / 2; long t = base[mid] - ft.query(mid); if (t >= a[i]) { res = mid; hi = mid - 1; } else lo = mid + 1; } ans[i] = find(res, nxt); nxt[ans[i]]++; } for(int x: ans) out.print(x + " "); out.close(); } static class FenwickTree { long[] ft; FenwickTree(int n) { ft = new long[n + 2]; } void update(int l, int r, int v) { ft[l] += v; ft[r + 1] -= v; } long query(int i) { return ft[i]; } } static class Scanner { StringTokenizer st; BufferedReader br; 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 boolean ready() throws IOException {return br.ready();} } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct BIT { int n, N_MAX; vector<long long> v; BIT(int n) { this->n = n + 100; N_MAX = n; v.assign(n + 110, 0); } void upd(int p, int x) { while (p <= n) v[p] += x, p += p & -p; } long long que(int p) { long long ans = 0; while (p) ans += v[p], p -= p & -p; return ans; } long long quep(int p) { return que(p) - que(p - 1); } long long bit_search(long long s) { long long sum = 0, pos = 0; for (int i = 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("%d", &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 " ] }
IN-CORRECT
python3
n = int(input()) arr = list(map(int, input().split())) res = [0]*n for i in range(n-1,-1,-1): le = int(arr[i]**0.5) res[i] = le+1 for i in range(n-2,0,-1): if res[i] == res[i+1]: temp = res[i] res[i] += 1 if res[i-1] == temp: res[i-1] += 1 print(res)
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &x) { bool b = 0; char c; while (!isdigit(c = getchar()) && c != '-') ; if (c == '-') c = getchar(), b = 1; x = c - 48; while (isdigit(c = getchar())) x = (x << 3) + (x << 1) + c - 48; if (b) x = -x; } template <typename T> inline void wrip(T x) { if (x > 9) wrip(x / 10); putchar(x % 10 + 48); } template <typename T> inline void write(T x) { if (x < 0) putchar('-'), x = -x; wrip(x); } void reads(string &s) { char c; while (!isalpha(c = getchar())) ; s = c; while (isalpha(c = getchar())) s += c; } const long long mod = 1e9 + 7; const int N = 2e5 + 11; int n, a[N]; long long s[N], it[4 * N], lazy[4 * N]; void push(int id) { it[id << 1] += lazy[id]; it[(id << 1) + 1] += lazy[id]; lazy[id << 1] += lazy[id]; lazy[(id << 1) + 1] += lazy[id]; lazy[id] = 0; } void update(int id, int l, int r, int u, int v, long long val) { if (l > v || r < u) return; if (u <= l && r <= v) { lazy[id] += val; it[id] += val; return; } if (lazy[id]) push(id); int mid = l + r >> 1; update(id << 1, l, mid, u, v, val); update((id << 1) + 1, mid + 1, r, u, v, val); it[id] = min(it[id << 1], it[(id << 1) + 1]); } int Find(int id, int l, int r, long long val) { if (it[id] > val) return l - 1; if (l == r) return l; int mid = l + r >> 1; if (it[(id << 1) + 1] <= val) return Find((id << 1) + 1, mid + 1, r, val); return Find(id << 1, l, mid, val); } int main() { read(n); for (int i = 1; i <= n; i++) read(s[i]); for (int i = 1; i <= n; i++) { s[0] += i; update(1, 1, n, i, i, s[0]); } for (int i = n; i > 0; i--) { int pos = Find(1, 1, n, s[i]); a[i] = pos + 1; update(1, 1, n, pos + 1, n, -a[i]); } for (int i = 1; i <= n; i++) { write(a[i]); putchar(' '); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { private static int KvoElem = 131072 * 2; private static int KvoForSum = 262144 * 2; private static int tree[]=new int[KvoForSum]; private static int Result=0; public static void main(String[] args) { Scanner write = new Scanner(System.in); Main user = new Main(); for (int i = KvoElem; i < KvoForSum; i++) user.update(i - KvoElem, 1); int n = write.nextInt(); long []value = new long[n]; for(int i = 0;i<n;i++){ value[i] = write.nextLong(); } List<Integer> list = new ArrayList <>(); for(int i = n-1;i>=0;i--){ long x = user.find_need(value[i]); user.searchNext((int) x); list.add(Result); } for(int i = n-1;i>=0;i--){ System.out.print(list.get(i) + " "); } } private long find_need(long x) { int l = 0; int r = 200005; while (r > l) { long mid = (l + r) / 2; long val = get_sum(mid); if (val == x) { return mid + 1; } else if (val > x) { r = (int) mid; } else { l = (int) (mid + 1); } } return l + 1; } private long get_sum(long x){ return x*(x+1)/2; } private int sum(int index){ index+=KvoElem; int res = tree[index]; while(index>1){ if(index%2==1) res+=tree[index-1]; index/=2; } return res; } private void searchNext(int index) { index += KvoElem; if (tree[index] == 1) { update(index - KvoElem, 0); Result = index-KvoElem; } else { int SearcInd = sum(index-KvoElem) + 1; index = 1; while (Main.tree[index]!=SearcInd && (index*2+1)<Main.KvoForSum){ if(Main.tree[index]>SearcInd){ index*=2; } else if(Main.tree[index]<SearcInd){ SearcInd-=Main.tree[index]; index++; } } UpdateIfRavno(index); } } private void UpdateIfRavno(int index){ while ((index*2 + 1)<Main.KvoForSum){ if(Main.tree[index*2+1]==0) index*=2; else index=index*2 +1; } Result = index-KvoElem; update(index-Main.KvoElem,0); } private void update(int index,int value){ index+=Main.KvoElem; int a=value-Main.tree[index]; while (index>=1){ Main.tree[index]+=a; index/=2; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
python3
import sys def get_max_value(n): return sum([i for i in range(n + 1)]) def get_leftmost_index(val, l, res): n = len(l) for i, value in enumerate(reversed(l)): if value == val and res[n - i - 1] == 0: return n - i - 1 def main(): a = sys.stdin.read() a = a.split('\n') N = int(a[0]) input = [int(lol) for lol in a[1].split(' ')] res = [0 for i in input] for i in range(N): max_value = get_max_value(i) for j in range(max_value, -1, -1): if j in input: index = get_leftmost_index(j, input, res) if not index is None: res[index] = i + 1 break print(' '.join([str(lol) for lol in res])) main()
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
/** * @author derrick20 */ import java.io.*; import java.util.*; public class RestorePermutations { public static void main(String args[]) throws Exception { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int N = sc.nextInt(); long[] sums = new long[N]; for (int i = 0; i < sums.length; i++) { sums[i] = sc.nextLong(); } available = new TreeSet<>(); int[] arr = new int[N + 1]; for (int i = 0; i < arr.length; i++) { arr[i] = i; if (i > 0) { available.add(i); } } bit = new BIT(arr); used = new HashSet<>(); int[] seq = new int[N]; for (int i = N - 1; i >= 0; i--) { int val = binarySearch(1, N, sums[i]); // Move up if it was used (only for 0 case) // while (used.contains(val) && bit.prefixSum(val + 1 - 1) == sums[i]) { // val++; // } if (available.higher(val - 1) != null) { int temp = available.higher(val - 1); if (bit.prefixSum(temp - 1) == sums[i]) { val = temp; } } if (available.lower(val + 1) != null) { int temp = available.lower(val + 1) ; if (bit.prefixSum(temp - 1) == sums[i]) { val = temp; } } seq[i] = val; bit.add(val, -val); // System.out.println(bit); used.add(val); available.remove(val); } for (int i = 0; i < seq.length; i++) { out.print(seq[i] + " "); } out.close(); } static BIT bit; static HashSet<Integer> used; static TreeSet<Integer> available; // Find the number such that the sum of things below it in the // current sequence state equals the goal sum // 0001111 Let's find the lowest one that's >= it. That's the same thing static int binarySearch(int lo, int hi, long sum) { while (lo < hi) { int mid = (lo + hi) / 2; // go towards the 0's, so we crawl up // If it's not used, then we need to go up. if (!used.contains(mid) && bit.prefixSum(mid - 1) >= sum) { hi = mid; } else { lo = mid + 1; } } return lo; } static class BIT { int[] arr; long[] tree; // todo ***This ASSUMES THAT ARR IS 1-INDEXED ALREADY*** public BIT(int[] arr) { this.arr = arr; this.tree = new long[arr.length]; // copy arr values into tree for (int i = 1; i < tree.length; i++) { tree[i] = arr[i]; } constructBIT(arr, tree); } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Index: "); for (int i = 1; i < tree.length; i++) { sb.append(i + " "); } sb.append("\nValue: "); for (int i = 1; i < tree.length; i++) { sb.append(this.sum(i, i) + " "); } return sb.toString(); } public int leastSignificantBit(int x) { // by negating it, all return x & (-x); } public void constructBIT(int[] arr, long[] tree) { // propagate information up to the pieces above it that would be responsible for it for (int i = 1; i < tree.length; i++) { int j = i + leastSignificantBit(i); // all of it's "parents" will need to possess its values, // but, we can be clever and only propagate to its immediate parent. // Since we are processing in order, that parent will propagate to its parent // eventually, so we are fine. Add methods are log(N) because we can't rely on our // parents eventually doing work for us. if (j < arr.length) { tree[j] += tree[i]; } } } // return the sum public long sum(int i, int j) { return prefixSum(j) - prefixSum(i - 1); // exclude the values under i } // returns sum from 1 to i of the array // propagate downward! (decomposing the sum) public long prefixSum(int i) { long sum = 0; while (i > 0) { sum += tree[i]; i -= leastSignificantBit(i); } return sum; } // add a value of val at the ith value // propagate upward! public void add(int i, int val) { while (i < tree.length) { tree[i] += val; i += leastSignificantBit(i); } } } static class FastScanner { public int BS = 1<<16; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double cnt = 1; BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { cnt=1; boolean neg = false; if(c==NC)c=getChar(); for(;(c<'0' || c>'9'); c = getChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=getChar()) { res = (res<<3)+(res<<1)+c-'0'; cnt*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/cnt; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=getChar(); while(c>32) { res.append(c); c=getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=getChar(); while(c!='\n') { res.append(c); c=getChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=getChar(); if(c==NC)return false; else if(c>32)return true; } } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
python3
import math import bisect from collections import defaultdict; def solve(s): n = len(s) result = [0] * n rr = [0] * n xx = defaultdict(lambda: []) for ind, x in enumerate(s): xx[x].append(ind) p = set([-10]) sum = 0 for i in range(1, n + 1): left = sum for start in reversed(sorted(p)): if left in xx and xx[left][-1] > start: val = xx[left][-1] result[val] = i rr[val] = i p.add(val) xx[left].pop(-1) if len(xx[left]) == 0: del xx[left] sum += i newval = val - 1 while newval in p: rr[val] += result[newval] p.remove(newval) newval -= 1 newval = val + 1 while newval in p: rr[val] += rr[newval] p.remove(newval) newval += 1 break left -= rr[start] return result def main(): n = int(input()) s = [int(x) for x in input().split()] print(' '.join([str(x) for x in solve(s)])) if __name__ == '__main__': main()
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
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) { res[i] = fenwickTree.getFirstNotNullValue(currIndex); } } for (int i = 0; i < res.length; i++) { System.out.print(res[i] + " "); } } } class FenwickTree { private int[] arr; FenwickTree(int size) { this.arr = new int[size + 1]; for (int i = 0; i < arr.length; i++) { updateValue(i, i); } } int sum(int range) { int 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; } int getFirstNotNullValue(int index) { for (int i = index + 1; i < arr.length; i++) { if (arr[i] != 0) { arr[i] = 0; return i; } } return 0; } } 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 " ] }
IN-CORRECT
java
/* Rajkin Hossain */ import java.io.*; import java.util.*; import static java.lang.Math.*; public class D{ FastInput k = new FastInput(System.in); //FastInput k = new FastInput("/home/rajkin/Desktop/input.txt"); FastOutput z = new FastOutput(); int n; int [] y; SegmentTree tree; int [] ans; void startAlgorithm() { tree = new SegmentTree(n); for(int i = 1; i<=n; i++) { Pair p = tree.tree[1]; ans[p.index] = i; tree.update(p.index); tree.update(p.index+1, n, i); } for(int i = 1; i<=n; i++) { z.print(ans[i]+" "); } z.println(); } void startProgram() { while(k.hasNext()) { n = k.nextInt(); y = new int[n+1]; ans = new int[n+1]; for(int i = 1; i<=n; i++) { y[i] = k.nextInt(); } startAlgorithm(); } z.flush(); System.exit(0); } class SegmentTree { Pair [] tree; int [] prop; int treeLength; public SegmentTree(int max_size) { tree = new Pair[max_size * 4]; prop = new int[max_size * 4]; treeLength = max_size; initialize(1, 1, treeLength); } public Pair getMin(Pair a, Pair b) { if(a.value == b.value) { if(a.index > b.index) { return a; } else { return b; } } else if(a.value < b.value) { return a; } return b; } public void initialize(int nodeId, int i, int j) { if (i == j) { tree[nodeId] = new Pair(i, y[i]); return; } int left = nodeId * 2; int right = nodeId * 2 + 1; int mid = (i + j) / 2; initialize(left, i, mid); initialize(right, mid + 1, j); tree[nodeId] = getMin(tree[left], tree[right]); } public void update(int index) { update(1, 1, treeLength, index); } public void update(int nodeId, int i, int j, int index) { if (i>index || j<index) { return; } if (i==index && j==index) { tree[nodeId] = new Pair(-1, Integer.MAX_VALUE); return; } int left = nodeId * 2; int right = nodeId * 2 + 1; int mid = (i + j) / 2; update(left, i, mid, index); update(right, mid + 1, j, index); if(tree[nodeId].index == index) { tree[nodeId] = getMin(tree[left], tree[right]); tree[nodeId].value -= prop[nodeId]; } } public void update(int b, int e, int value) { update(1, 1, treeLength, b, e, value); } public void update(int nodeId, int i, int j, int b, int e, int value) { if (i>e || j<b) { return; } if (i>=b && j<=e) { prop[nodeId] = value; tree[nodeId].value -= value; return; } int left = nodeId * 2; int right = nodeId * 2 + 1; int mid = (i + j) / 2; update(left, i, mid, b, e, value + prop[left]); update(right, mid + 1, j, b, e, value + prop[right]); tree[nodeId] = getMin(getMin(tree[left], tree[right]), tree[nodeId]); } } class Pair { int index; int value; public Pair(int index, int value) { this.index = index; this.value = value; } } public static void main(String [] args) throws IOException { new Thread(null, new Runnable(){ public void run(){ try{ new D().startProgram(); } catch(Exception e){ e.printStackTrace(); } } },"Main",1<<28).start(); } /* MARK: FastInput and FastOutput */ class FastInput { BufferedReader reader; StringTokenizer tokenizer; FastInput(InputStream stream){ reader = new BufferedReader(new InputStreamReader(stream)); } FastInput(String path){ try { reader = new BufferedReader(new FileReader(path)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } tokenizer = null; } String next() { return nextToken(); } String nextLine() { try { return reader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } boolean hasNext(){ try { return reader.ready(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } String nextToken() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { String line = null; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } if (line == null) { return null; } tokenizer = new StringTokenizer(line); } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.valueOf(nextToken()); } double nextDouble() { return Double.valueOf(nextToken()); } } class FastOutput extends PrintWriter { FastOutput() { super(new BufferedOutputStream(System.out)); } public void debug(Object...obj) { System.err.println(Arrays.deepToString(obj)); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int M = 1e16 + 300; const int N = 1e6 + 5; long long n; long long s[N]; long long ans[N]; long long st[4 * N], lazy[4 * N]; void pull(long long si) { st[si] = min(st[2 * si], st[2 * si + 1]); } void apply(long long si, long long val) { st[si] += val; lazy[si] += val; } void push(long long si) { if (lazy[si] != 0) { apply(2 * si, lazy[si]); apply(2 * si + 1, lazy[si]); lazy[si] = 0; } } void build(long long si, long long ss, long long second) { if (ss == second) { st[si] = s[ss]; return; } long long mid = ((ss + second) >> 1LL); build(2 * si, ss, mid); build(2 * si + 1, mid + 1, second); pull(si); } long long findzero(long long si, long long ss, long long second) { if (ss == second) return ss; push(si); long long mid = ((ss + second) >> 1LL); long long res = -1; if (st[2 * si + 1] == 0) res = findzero(2 * si + 1, mid + 1, second); else res = findzero(2 * si, ss, mid); pull(si); return res; } void add(long long si, long long ss, long long second, long long qs, long long qe, long long val) { if (ss > qe || second < qs) return; if (qs <= ss && qe >= second) apply(si, val); else { push(si); long long mid = ((ss + second) >> 1LL); add(2 * si, ss, mid, qs, qe, val); add(2 * si + 1, mid + 1, second, qs, qe, val); pull(si); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (long long i = 1; i <= n; ++i) cin >> s[i]; build(1, 1, n); for (long long i = 1; i <= n; ++i) { long long pos = findzero(1, 1, n); ans[pos] = i; add(1, 1, n, pos, pos, M); add(1, 1, n, pos + 1, n, -i); } for (long long i = 1; i <= n; ++i) cout << ans[i] << " "; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, a[200005]; long long I[200005]; vector<int> v; void up(int x) { int val = x; while (x <= n) { I[x] += val; x += (x & (-x)); } } long long get(int x) { int res = 0; while (x > 0) { res += I[x]; x -= (x & (-x)); } return res; } int fid(int x) { int l = 1, r = n + 1; while (r - l > 1) { int m = (l + r) / 2; if (1ll * 2 * x + 1ll * m + 1ll * 2 * get(m - 1) < 1ll * m * m) r = m; else l = m; } return l; } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = n; i > 0; --i) { int x = fid(a[i]); v.push_back(x), up(x); } reverse(v.begin(), v.end()); for (auto c : v) cout << c << " "; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MXN = 2e5 + 30; const int MAXN = 1e3 + 560; const long long INF = 1e9 + 7; const long long BINF = 1e16; const int mod = 1e9 + 7; const long double EPS = 1e-15; const long long dx[] = {0, 0, 1, -1}; const long long dy[] = {1, -1, 0, 0}; int n; int second[MXN]; int a[MXN]; int t[MXN * 4], to[MXN * 4]; void Push(int v) { t[v + v] -= to[v]; t[v + v + 1] -= to[v]; to[v + v + 1] += to[v]; to[v + v] += to[v]; to[v] = 0; } void build(int v, int tl, int tr) { if (tl == tr) { t[v] = second[tl]; to[v] = 0; return; } int tm = (tl + tr) / 2; build(v * 2, tl, tm); build(v * 2 + 1, tm + 1, tr); t[v] = min(t[v + v], t[v + v + 1]); } void upd(int v, int tl, int tr, int l, int x) { if (tl >= l) { t[v] -= x; to[v] += x; return; } if (tr < l) return; Push(v); int tm = (tl + tr) / 2; upd(v * 2, tl, tm, l, x); upd(v * 2 + 1, tm + 1, tr, l, x); t[v] = min(t[v + v], t[v + v + 1]); } void updi(int v, int tl, int tr, int ind) { if (tl == tr) { t[v] = INF; return; } Push(v); int tm = (tl + tr) / 2; if (ind <= tm) { updi(v + v, tl, tm, ind); } else { updi(v + v + 1, tm + 1, tr, ind); } t[v] = min(t[v + v], t[v + v + 1]); } int get(int v, int tl, int tr) { if (tl == tr) { return tl; } Push(v); int tm = (tl + tr) / 2; if (t[v + v + 1] <= t[v + v]) { return get(v + v + 1, tm + 1, tr); } else { return get(v + v, tl, tm); } } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> second[i]; } build(1, 1, n); for (int i = 1; i <= n; i++) { int j = get(1, 1, n); a[j] = i; updi(1, 1, n, j); upd(1, 1, n, j + 1, i); } for (int i = 1; i <= n; i++) { cout << a[i] << ' '; } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
python3
n=int(input()) a=[] b={} for i in input().split(): a.append(int(i)) b[int(i)]=True qq=list(b.keys()) qq=sorted(qq) ii=1 b=[0 for i in range(n)] for i in qq: for i2 in range(n-1,-1,-1): if a[i2]==i: b[i2]=ii ii+=1 for i in b: print(i,end=' ')
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct BIT { int n, N_MAX; vector<long long> v; BIT(int n) { this->n = n + 100; N_MAX = n; v.assign(n + 110, 0); } void upd(int p, int x) { while (p <= n) v[p] += x, p += p & -p; } long long que(int p) { long long ans = 0; while (p) ans += v[p], p -= p & -p; return ans; } long long quep(int p) { return que(p) - que(p - 1); } long long bit_search(long long s) { long long sum = 0, pos = 0; for (int i = 20; i >= 0; i--) if (pos + (1 << i) <= N_MAX && sum + v[pos + (1 << i)] <= s) { pos += (1 << i); sum += v[pos]; } return pos + 1; } }; int main() { int n; scanf("%d", &n); BIT bit(n); vector<int> v(n + 1), ans(n + 1); for (int i = 1; i <= n; i++) scanf("%d", &v[i]), bit.upd(i, i); for (int i = n; i; i--) { int p = bit.bit_search(v[i]); ans[i] = p; bit.upd(p, -p); } for (int i = 1; i <= n; i++) printf("%d ", ans[i]); printf("\n"); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
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()); HashSet<Integer> set=new HashSet(); 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)-mid; x-=ft.query(mid); if(x<s[i]) { lo=mid+1; }else if(x>s[i]) { hi=mid; }else { ans[i]=mid; break; } } if(ans[i]==0) ans[i]=hi; set.add(ans[i]); ft.update(ans[i], ans[i]); } for(int i=1;i<=n;i++) { if(!set.contains(i)) {ans[0]=i;break;} } 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,ft[]; public FenwickTree(int n) { this.n=n; ft=new int[n+1]; } public void update(int idx,int val) { while(idx<=n) { ft[idx]+=val; idx+=(idx&-idx); } } public int query(int i) { int 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 " ] }
IN-CORRECT
java
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; public class gym{ static class pair implements Comparable<pair>{ long n;int idx; pair(long x,int y){ n=x;idx=y; } @Override public int compareTo(pair o) { if(n!=o.n) { if(n>o.n)return 1; return -1; } return idx-o.idx; } } public static void main(String[] args) throws Exception{ MScanner sc = new MScanner(System.in); PrintWriter pw=new PrintWriter(System.out); int n=sc.nextInt(); TreeSet<pair>t=new TreeSet<pair>(); for(int i=0;i<n;i++) { t.add(new pair(sc.nextLong(),i)); } int[]ans=new int[n]; for(int i=1;i<=n;i++) { pair p=t.floor(new pair((i*1l*(i-1)/2),n)); t.remove(p); ans[p.idx]=i; } for(int i:ans)pw.print(i+" "); pw.flush(); } static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { private static int KvoElem = 131072 * 2; private static int KvoForSum = 262144 * 2; private static int tree[]=new int[KvoForSum]; private static int t[]=new int[KvoForSum*2]; private static int arr[]=new int[KvoElem + 1]; private static int Result=0; public static void main(String[] args) { Scanner write = new Scanner(System.in); Main user = new Main(); for (int i = KvoElem; i < KvoForSum; i++) user.update(i - KvoElem, 1); int n = write.nextInt(); for(int i = 0;i<KvoElem;i++){ arr[i] = 0; } build(1, 0, n); long []value = new long[n]; for(int i = 0;i<n;i++){ value[i] = write.nextLong(); } List<Integer> list = new ArrayList <>(); for(int i = n-1;i>=0;i--){ long x = user.find_need(value[i]); user.searchNext((int) x); list.add(Result); update(1, 0, n, Result, Result); } for(int i = n-1;i>=0;i--){ System.out.print(list.get(i) + " "); } } private long find_need(long x) { int l = 0; int r = 200005; while (r > l) { long mid = (l + r) / 2; long val = get_sum(mid); if (val == x) { return mid + 1; } else if (val > x) { r = (int) mid; } else { l = (int) (mid + 1); } } return l + 1; } private static long get_sum(long x){ return x*(x+1)/2 - sum1(1, 0, KvoElem, 0, (int) x); } private static int sum(int index){ index+=KvoElem; int res = tree[index]; while(index>1){ if(index%2==1) res+=tree[index-1]; index/=2; } return res; } private void searchNext(int index) { index += KvoElem; if (tree[index] == 1) { update(index - KvoElem, 0); Result = index-KvoElem; } else { int SearcInd = sum(index-KvoElem) + 1; index = 1; while (Main.tree[index]!=SearcInd && (index*2+1)<Main.KvoForSum){ if(Main.tree[index]>SearcInd){ index*=2; } else if(Main.tree[index]<SearcInd){ SearcInd-=Main.tree[index]; index++; } } UpdateIfRavno(index); } } private void UpdateIfRavno(int index){ while ((index*2 + 1)<Main.KvoForSum){ if(Main.tree[index*2+1]==0) index*=2; else index=index*2 +1; } Result = index-KvoElem; update(index-Main.KvoElem,0); } private void update(int index,int value){ index+=Main.KvoElem; int a=value-Main.tree[index]; while (index>=1){ Main.tree[index]+=a; index/=2; } } public static void build (int v, int tl, int tr) { if (tl == tr) t[v] = arr[tl]; else { int tm = (tl + tr) / 2; build (v*2, tl, tm); build ( v*2+1, tm+1, tr); t[v] = t[v*2] + t[v*2+1]; } } public static long sum1 (int v, int tl, int tr, int l, int r) { if (l > r) return 0; if (l == tl && r == tr) return t[v]; int tm = (tl + tr) / 2; return sum1 (v*2, tl, tm, l, Math.min(r,tm)) + sum1 (v*2+1, tm+1, tr, Math.max(l,tm+1), r); } public static void update (int v, int tl, int tr, int pos, int new_val) { if (tl == tr) t[v] = new_val; else { int tm = (tl + tr) / 2; if (pos <= tm) update (v*2, tl, tm, pos, new_val); else update (v*2+1, tm+1, tr, pos, new_val); t[v] = t[v*2] + t[v*2+1]; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n; long long 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 - 1) { if (ns > i || ne < i || ns > ne) return; if (ns == ne && ns == i) seg[ni] += v; 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); } long long query(int qe, int ni = 0, int ns = 0, int ne = n - 1) { if (ns > qe || ns > ne) return 0; if (ne <= qe) return seg[ni]; int l = ni * 2 + 1, r = l + 1, mid = (ns + ne) / 2; return query(qe, l, ns, mid) + query(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(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 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long a[n], s[n], i; for (i = 0; i < n; i++) cin >> a[i]; if (a[1] != 0) s[0] = a[1]; else { s[0] = 1, i = 1; while (a[i] == 0 && i < n) s[0]++, i++; } for (i = 1; i < n - 1; i++) { long long x = a[i] + 1, j = i + 1; while (a[j] == a[i] && j < n) x++, j++; s[i] = x; } s[n - 1] = a[n - 1] + 1; for (i = 0; i < n; i++) cout << s[i] << ' '; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MXN = 3e5 + 7; const int MOD = 1e9 + 7; const long long INF = 1e18 + 7; const int N = 2e5; int n, a[MXN], c[MXN]; long long t[MXN * 4]; long long mod[MXN * 4]; void build(int v, int tl, int tr) { if (tl == tr) { t[v] = a[tl]; return; } int tm = tl + tr >> 1; build(v * 2, tl, tm); build(v * 2 + 1, tm + 1, tr); t[v] = min(t[v * 2], t[v * 2 + 1]); } void push(int v, int tl, int tr) { if (mod[v]) { t[v] -= mod[v]; if (tl != tr) { mod[v * 2] += mod[v]; mod[v * 2 + 1] += mod[v]; } mod[v] = 0; } } void upd(int v, int tl, int tr, int pos) { push(v, tl, tr); if (tl == tr) { t[v] = INF; return; } int tm = tl + tr >> 1; if (pos <= tm) { upd(v * 2, tl, tm, pos); } else { upd(v * 2 + 1, tm + 1, tr, pos); } push(v * 2, tl, tm); push(v * 2 + 1, tm + 1, tr); t[v] = min(t[v * 2], t[v * 2 + 1]); } void update(int v, int tl, int tr, int l, int r, long long val) { push(v, tl, tr); if (tl > r || tr < l) return; if (l <= tl && tr <= r) { mod[v] += val; push(v, tl, tr); return; } int tm = tl + tr >> 1; update(v * 2, tl, tm, l, r, val); update(v * 2 + 1, tm + 1, tr, l, r, val); push(v * 2, tl, tm); push(v * 2 + 1, tm + 1, tr); t[v] = min(t[v * 2], t[v * 2 + 1]); } int get(int v, int tl, int tr) { if (tl == tr) { return tl; } int tm = tl + tr >> 1; push(v * 2, tl, tm); push(v * 2 + 1, tm + 1, tr); if (!t[v * 2 + 1]) { return get(v * 2 + 1, tm + 1, tr); } else { return get(v * 2, tl, tm); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } build(1, 1, n); for (int i = 1; i <= n; i++) { long long num = i; int pos = get(1, 1, n); update(1, 1, n, pos + 1, n, num); upd(1, 1, n, pos); c[pos] = i; } for (int i = 1; i <= n; i++) { cout << c[i] << ' '; } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) pos = [0]*n for i in range(1,n+1)[::-1]: p = i-1 s = p*(p+1)//2 for j in range(n)[::-1]: if not pos[j]: if a[j] == s: pos[j] = i break else: s-=p p-=1 print(' '.join(list(map(str,pos))))
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1e6, MAXN = 1e6 + 7; const int mod = 998244353; long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; set<int> num; for (int i = 1; i <= n; ++i) num.insert(i); vector<int> s(n), p(n, -1); for (int i = 0; i < n; ++i) cin >> s[i]; for (int i = n - 1; i >= 0; --i) { auto itr = num.begin(); while (s[i] > 0) { s[i] -= *(itr++); } p[i] = *itr; num.erase(itr); } for (int i = 0; i < n; ++i) { cout << p[i] << ' '; } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int n, a[N], c[N], p[N]; inline int read() { int ret = 0, f = 0; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = 1; c = getchar(); } while (isdigit(c)) { ret = ret * 10 + c - 48; c = getchar(); } if (f) return -ret; return ret; } int lowbit(int x) { return x & -x; } void add(int x, int y) { while (x <= n) { c[x] += y; x += lowbit(x); } } int sum(int x) { int res = 0; while (x > 0) { res += c[x]; x -= lowbit(x); } return res; } signed main() { n = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) add(i, i); for (int i = n; i; i--) { int l = 1, r = n; while (l < r) { int mid = (l + r + 1) / 2; if (sum(mid - 1) <= a[i]) l = mid; else r = mid - 1; } p[i] = r; add(r, -r); } for (int i = 1; i <= n; i++) printf("%d ", p[i]); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; public class Main { static int mod = (int)1e9 + 7; static long arr[]; static long arr1[]; static boolean[] v; public static void main(String[] args) throws IOException { Reader.init(System.in); int n= Reader.nextInt() ;long arr[] = new long[n]; for (int i = 0 ;i < n ; i++) { arr[i] = Reader.nextLong(); } long[] ans = new long[n]; long[] val = new long[n+1]; for (int i = 1 ; i < n+1 ; i++){ val[i] = i + val[i-1]; } HashMap<Long, Long> map = new HashMap<>(); /*ans[n-1] = 1 + Arrays.binarySearch(val,arr[n-1]); System.out.println(ans[n-1]);*/ for (int i = n-1 ; i>= 0 ; i--){ long tmp = 1 + Arrays.binarySearch(val,arr[i]); //System.out.println(tmp); if (map.containsKey(arr[i])){ map.put(arr[i],map.get(arr[i])+1); } else{ map.put(arr[i],tmp); } ans[i] = map.get(arr[i]); } for (int i = 0 ; i < n ; i++){ System.out.print(ans[i] + " "); } System.out.println(); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ 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 " ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class ProblemC { public static void main(String[] args) { InputStream in = System.in; InputReader scan = new InputReader(in); int n = scan.nextInt(); long[] arr = new long[n+2]; arr[n+1]=((long)n*(n+1))/2L; arr[0]=0; for(int i=1;i<=n;i++) { arr[i]=scan.nextLong(); } StringBuilder sb = new StringBuilder(); int currNum = 0; for(int i=1;i<=n+1;i++) { if(arr[i]>arr[i-1]) { int lastNum = i-1; int temp = lastNum; while(lastNum>currNum) { sb.append(lastNum); sb.append(" "); lastNum--; } currNum = temp; } } System.out.println(sb.toString()); } static int findNumFromSum(long sum) { long summation =0; for(int i=1;summation<sum;i++) { summation+=(long)i; if(sum==summation) return i; } return -1; } static long gcd(long a,long b) { long greater = Long.MAX_VALUE; long smaller = Long.MIN_VALUE; if(a>b) { greater = a; smaller = b; } else { greater = b; smaller = a; } if(greater%smaller==0) { return smaller; } else { return gcd(smaller,greater%smaller); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException ioe) { ioe.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
python3
from math import ceil, sqrt n = int(input()) s = [int(i) for i in input().split()] min_s = [0 for i in range(n+2)] ans = [0 for i in range(n)] for i in range(n-1, -1, -1): act = ceil((1+sqrt(1+8*s[i]))/2) ans[i] = (min_s[act:].index(0)+act) min_s[ans[i]] = 1 print(" ".join([str(i) for i in ans]))
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MN = 200010; long long a[MN]; bool cmp(int i, int j) { if (a[i] != a[j]) return a[i] < a[j]; return i > j; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; vector<int> order(n); for (int i = 0; i < n; i++) order[i] = i; sort(order.begin(), order.end(), cmp); vector<int> ans(n); for (int i = 0; i < n; i++) ans[order[i]] = i; for (int i = 0; i < n; i++) cout << ans[i] + 1 << " "; cout << '\n'; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
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 " ] }
IN-CORRECT
java
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; public class gym{ static class pair implements Comparable<pair>{ long n;int idx; pair(long x,int y){ n=x;idx=y; } @Override public int compareTo(pair o) { int me=Arrays.binarySearch(nums, n); int oo=Arrays.binarySearch(nums, o.n); if(me!=oo) { if(n>o.n)return 1; return -1; } return idx-o.idx; } } static long []nums; public static void main(String[] args) throws Exception{ MScanner sc = new MScanner(System.in); PrintWriter pw=new PrintWriter(System.out); nums=new long[(int)2e5+1]; for(int i=1;i<nums.length;i++) { nums[i]=(i*1l*(i-1/2)); } int n=sc.nextInt(); TreeSet<pair>t=new TreeSet<pair>(); for(int i=0;i<n;i++) { t.add(new pair(sc.nextLong(),i)); } int[]ans=new int[n]; for(int i=1;i<=n;i++) { pair p=t.floor(new pair((i*1l*(i-1)/2),n)); t.remove(p); ans[p.idx]=i; } for(int i:ans)pw.print(i+" "); pw.flush(); } static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
python3
n = int(input()) l = [] for i in range(1,n+1): l.append(i) s = list(map(int, input().split())) i = 0 j = 1 ans = [] while j!=n: if(s[i]==s[j]): j += 1 else: for a in range(j-1,i-1,-1): ans.append(l[a]) i = j for a in range(j-1,i-1,-1): ans.append(l[a]) for i in range(0,n): print(ans[i], end=" ")
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <iostream> #include <random> #include <string> #include <vector> #include <climits> #include <algorithm> #include <iomanip> #include <tuple> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <cmath> #include <fstream> #include <deque> #include <queue> #include <iomanip> #include <numeric> #include <regex> #include <cassert> using namespace std; class Init { public: Init() noexcept try { #ifdef JAEYONG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // JAEYONG ios_base::sync_with_stdio(false); cin.tie(nullptr); /// @add more codes to execute before program begins ... } catch(...) { cerr << "Init constructor threw an exception" << endl; terminate(); } } __init __attribute__((unused)); ///==========---------- For CP ----------==========/// #define endl "\n" #define all(v) (v).begin(), (v).end() #define TC int tt;cin>>tt;for(int t=1;t<=tt;t++) #define CONCAT1(x, y) x##y #define CONCAT(x, y) CONCAT1(x, y) #define _ CONCAT(__,__COUNTER__) using ll = long long; using ld = long double; using pi = pair<int, int>; using pl = pair<ll, ll>; using ti = tuple<int, int, int>; using tl = tuple<ll, ll, ll>; template<typename T> istream &operator>>(istream &in, vector<T> &v); template<typename U, typename V> istream &operator>>(istream &in, pair<U, V> &p); template<typename T> ostream &operator<<(ostream &out, const vector<T> &v); template<typename U, typename V> ostream &operator<<(ostream &out, const pair<U, V> &p); template<typename T> istream &operator>>(istream &in, vector<T> &v) { for(T &i: v) in >> i; return in; } template<typename U, typename V> istream &operator>>(istream &in, pair<U, V> &p) { return in >> p.first >> p.second; } template<typename T> ostream &operator<<(ostream &out, const vector<T> &v) { auto it = v.begin(); if(!v.empty()) out << *it++; while(it != v.end()) out << " " << *it++; return out; } template<typename U, typename V> ostream &operator<<(ostream &out, const pair<U, V> &p) { return out << p.first << " " << p.second; } ///==========---------- End of Pre-Code ----------==========/// /** * @class add * + operator class wrapper * * @tparam _Tp value type */ template<class _Tp> class add { public: typedef _Tp value_type; static const value_type e; value_type operator()(value_type a, value_type b) { return a + b; } }; template<class _Tp> const _Tp add<_Tp>::e = 0; /** * @class seg_tree * Segment Tree with Lazy Propagation * use 1-base index * * @tparam _Tp value type of the tree * @tparam _Function function of the tree * @tparam _Container container that holds the tree */ template<class _Tp, class _Function = add<_Tp>, class _Container = std::vector<_Tp>> class seg_tree { public: typedef _Tp value_type; typedef _Container container_type; typedef _Function function_type; typedef unsigned long index_t; static_assert(is_same<value_type, typename container_type::value_type>::value, "value_type and container_type::value_type has to be same"); static_assert(is_same<value_type, typename function_type::value_type>::value, "value_type and function_type::value_type has to be same"); private: index_t __size; container_type seg, lazy; inline index_t __get_size(index_t t) { return t << 2; } template<class _Arr> value_type __init(index_t n, index_t l, index_t r, const _Arr &arr) { if (l == r) return seg[n] = arr[l - 1]; return seg[n] = function_type()(__init(2 * n, l, l + r >> 1, arr), __init(2 * n + 1, (l + r >> 1) + 1, r, arr)); } value_type __nest(value_type a, index_t n) { if (n == 1) return a; value_type res = __nest(a, n >> 1); res = function_type()(res, res); if (n & 1) res = function_type()(a, res); return res; } void __busy(index_t n, index_t l, index_t r) { if (lazy[n] == value_type()) return; seg[n] = function_type()(seg[n], __nest(lazy[n], r - l + 1)); if (l != r) { lazy[2 * n] = function_type()(lazy[2 * n], lazy[n]); lazy[2 * n + 1] = function_type()(lazy[2 * n + 1], lazy[n]); } lazy[n] = value_type(); } void __change(index_t n, index_t l, index_t r, index_t s, index_t e, value_type a) { __busy(n, l, r); if (e < l || r < s) return; if (s <= l && r <= e) { lazy[n] = function_type()(lazy[n], a); __busy(n, l, r); } else { __change(2 * n, l, l + r >> 1, s, e, a); __change(2 * n + 1, (l + r >> 1) + 1, r, s, e, a); seg[n] = function_type()(seg[2 * n], seg[2 * n + 1]); } } void __set(index_t n, index_t l, index_t r, index_t i, value_type a) { __busy(n, l, r); if (i < l || r < i) return; if (l == r) { seg[n] = a; } else { __set(2 * n, l, l + r >> 1, i, a); __set(2 * n + 1, (l + r >> 1) + 1, r, i, a); seg[n] = function_type()(seg[2 * n], seg[2 * n + 1]); } } value_type __get(index_t n, index_t l, index_t r, index_t s, index_t e) { __busy(n, l, r); if (e < l || r < s) return function_type::e; if (s <= l && r <= e) return seg[n]; return function_type()(__get(2 * n, l, l + r >> 1, s, e), __get(2 * n + 1, (l + r >> 1) + 1, r, s, e)); } public: explicit seg_tree(index_t size) : __size(size) { seg.assign(__get_size(__size), value_type(function_type::e)); lazy = seg; } template<class _RandomAccessIterator> seg_tree(const _RandomAccessIterator &begin, const _RandomAccessIterator &end) : __size(end - begin) { seg.assign(__get_size(__size), value_type(function_type::e)); lazy = seg; __init(1, 1, __size, begin); } void change(index_t start, index_t end, value_type delta) { if (start > end) { cerr << "seg_tree::change: start has to be smaller or same as end" << endl; throw; } if (start < 1 || __size < end) { cerr << "seg_tree::change: start or end out of range error" << endl; throw; } __change(1, 1, __size, start, end, delta); } void set(index_t index, value_type value) { if (index < 1 || __size < index) { cerr << "seg_tree::change: index out of range error" << endl; throw; } __set(1, 1, __size, index, value); } value_type get(index_t start, index_t end) { if (start > end) { cerr << "seg_tree::get: start has to be smaller or same as end" << endl; throw; } if (start < 1 || __size < end) { cerr << "seg_tree::change: start or end out of range error" << endl; throw; } return __get(1, 1, __size, start, end); } index_t size() { return __size; } const index_t size() const { return __size; } }; int main() { int n; cin >> n; vector<int> v(n); cin >> v; reverse(all(v)); vector<int> t, res; for(int i=0; i<=n; i++) t.emplace_back(i); seg_tree<ll> s(all(t)); for(int i=0; i<n; i++) { int l = 0, r = n; while(l < r) { int mid = l + r >> 1; if(s.get(1, mid + 1) <= v[i]) l = mid + 1; else r = mid; } res.emplace_back(l); s.set(l + 1, 0); } reverse(all(res)); cout << res << endl; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; 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"; } void solve() { int n; cin >> n; std::vector<long long> v(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } int cur = 1; int j = 0, i = 0; while (i < n) { long long temp = v[i]; while (i + 1 < n and temp == v[i + 1]) { ++i; } temp = i; while (temp >= j) { v[temp] = cur++; temp--; } j = i + 1; ++i; } for (int k = 0; k < n; ++k) { cout << v[k] << " "; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long t[200025 * 4], lz[200025 * 4], n, a[200025]; void build(int l, int r, int i) { t[i] = l * (l - 1) / 2; if (l == r) return; build(l, (l + r) / 2, i * 2 + 1); build((l + r) / 2 + 1, r, i * 2 + 2); t[i] = max(t[i * 2 + 1], t[i * 2 + 2]); } void down(int i) { lz[i * 2 + 1] += lz[i]; lz[i * 2 + 2] += lz[i]; t[i * 2 + 1] += lz[i]; t[i * 2 + 2] += lz[i]; lz[i] = 0; } void updata(int L, int R, int v, int l, int r, int i) { if (L <= l && R >= r) { t[i] += v; lz[i] += v; return; } if (lz[i]) down(i); if (L <= (l + r) / 2) updata(L, R, v, l, (l + r) / 2, i * 2 + 1); if (R > (l + r) / 2) updata(L, R, v, (l + r) / 2 + 1, r, i * 2 + 2); t[i] = max(t[i * 2 + 1], t[i * 2 + 2]); } int query(int v, int l, int r, int i) { if (l == r) return l; if (lz[i]) down(i); if (t[i * 2 + 1] >= v) return query(v, l, (l + r) / 2, i * 2 + 1); return query(v, (l + r) / 2 + 1, r, i * 2 + 2); } int main() { cin >> n; for (int i = (1); i <= (n); i++) scanf("%lld", &a[i]); build(1, n, 0); for (int i = (n); i >= (1); i--) { a[i] = query(a[i], 1, n, 0); if (a[i] < n) updata(a[i] + 1, n, n, 1, n, 0); updata(a[i], a[i], -1e12, 1, n, 0); } for (int i = (1); i <= (n); i++) printf("%lld%c", a[i], i == n ? '\n' : ' '); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; int n; long long a[maxn]; long long tree[maxn]; void add(int x, long long y) { if (!x) return; while (x < maxn) { tree[x] += y; x += x & -x; } } long long sum(int x) { long long res = 0; while (x) { res += tree[x]; x -= x & -x; } return res; } int fd(int x) { int l = 1, r = n; int poi = 1; while (l < r) { long long m = (l + r + 1) / 2; long long tot = (m - 1) * m / 2; tot -= sum(m - 1); if (tot > x) { r = m - 1; poi = r; } else { l = m; poi = l; } } return poi; } int main() { scanf("%d", &n); stack<int> ans; for (int i = 0; i < n; i++) scanf("%lld", &a[i]); for (int i = n - 1; i >= 0; i--) { int tmp = fd(a[i]); add(tmp, tmp); ans.push(tmp); } while (!ans.empty()) { printf("%d ", ans.top()); ans.pop(); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int b[200005]; int n; int sum(int x) { if (x == 0) { return 0; } int pas = 0; while (x > 0) { pas += b[x]; x = x & (x - 1); } return pas; } void update(int x, int i) { while (i <= n) { b[i] += x; i += (i & (-i)); } } int main() { cin >> n; int s[n + 1]; s[0] = 0; int p[n + 1]; p[0] = 0; for (int i = 1; i <= n; i++) { cin >> s[i]; update(i, i); } int mid = 0; for (int i = n; i > 0; i--) { int l = 1; int r = n; while (l != r) { mid = (l + r + 1) / 2; if (sum(mid - 1) <= s[i]) { l = mid; } else { r = mid - 1; } } update(-l, l); p[i] = l; } for (int i = 1; i <= n; i++) { cout << p[i] << " "; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int P[200001]; ll sum[200001]; ll fen[200001]; int ans[200001]; void build() { for (int i = 1; i <= 200000; i++) { sum[i] = sum[i - 1] + ll(i - 1); fen[i] = sum[i] - sum[i - (i & -i)]; } } int query(int l, int r) { if (l != 1) return query(1, r) - query(1, l - 1); ll ans = 0LL; while (r > 0) { ans += fen[r]; r -= r & -r; } return ans; } void update(int i) { int taking = i; i++; while (i <= 200000) { fen[i] -= taking; i += (i & -i); } } int find(ll s) { ll cur = 0LL; int ind = 0; for (int b = 17; b >= 0; b--) { if (cur + fen[ind + (1 << b)] <= s) { cur += fen[ind + (1 << b)]; ind += (1 << b); } } return ind; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> P[i]; build(); for (int i = n; i >= 1; i--) { ans[i] = find(P[i]); update(ans[i]); } for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } cout << endl; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
python3
a = int(input()) b = list(map(int, input().split())) c = [] for i in range(a): c.append([b[i], -i]) c.sort() for i in range(a): c[i] = [i + 1, -c[i][1]] c.sort(key = lambda x:x[1]) for i in c: print(i[0], end = " ")
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = (int)2e5 + 123, inf = 1e9, mod = 1e9 + 7; const long long INF = 1e18; long long t[N << 2], mn[N << 2], second[N]; int n, p[N]; void upd(int v, int tl, int tr, int l, int r, int k) { if (r < tl || tr < l || l > r) return; if (l <= tl && tr <= r) { t[v] += k; mn[v] += k; return; } int tm = (tl + tr) >> 1; upd(v << 1, tl, tm, l, r, k); upd(v << 1 | 1, tm + 1, tr, l, r, k); mn[v] = min(mn[v << 1], mn[v << 1 | 1]) + t[v]; } int get(int v, int tl, int tr, long long k) { if (tl == tr) return tl; int tm = (tl + tr) >> 1; if (mn[v << 1 | 1] <= k) return get(v << 1 | 1, tm + 1, tr, k - t[v]); return get(v << 1, tl, tm, k - t[v]); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &second[i]); for (int i = 1; i <= n; i++) upd(1, 1, n, i + 1, n, i); for (int i = n; i >= 1; i--) { p[i] = get(1, 1, n, second[i]); upd(1, 1, n, p[i] + 1, n, -p[i]); } for (int i = 1; i <= n; i++) printf("%d ", p[i]); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
python3
'''input 6 0 1 1 1 10 10 ''' import sys from collections import defaultdict as dd from itertools import permutations as pp from itertools import combinations as cc from collections import Counter as ccd from random import randint as rd from bisect import bisect_left as bl from heapq import heappush as hpush from heapq import heappop as hpop mod=10**9+7 def ri(flag=0): if flag==0: return [int(i) for i in sys.stdin.readline().split()] else: return int(sys.stdin.readline()) n= ri(1) vis=[0 for i in range(n)] c1 =dd(int) for i in range(1,100005): temp = (i*(i+1))//2 c1[temp] =i s1=ri() temp=[0] prime=1 for i in range(1,n): if(s1[i]!=s1[i-1]): temp= temp[::-1] for j in temp: vis[j]=prime prime+=1 temp=[] temp.append(i) else: temp.append(i) temp =temp[::-1] for j in temp: vis[j]=prime prime+=1 print(*vis)
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
python3
import sys n = int(input()) s_i = [int(i) for i in input().split(" ")] mass_n = [(i+1) for i in range(n)] sum_ = int((n-1)*n/2) current_per = [] for j in s_i[::-1]: ch_sum = sum_ for t,i in enumerate(mass_n[::-1]): # print(f"pop index {t}, current_s_i = {j}, ch_sum = {ch_sum}") # print(f"t = {t}") if j == ch_sum: if t == 0: sum_ -= mass_n[len(mass_n) - t - 2] else: sum_ -= mass_n[len(mass_n) - t - 1] # print(f"before pop = {mass_n}") mass_n.pop(len(mass_n) - t - 1) # print(f"after pop = {mass_n}") current_per.append(i) break else: if len(mass_n) - t - 2 >= 0: ch_sum -= mass_n[len(mass_n) - t - 2]; print(current_per[::-1])
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
python3
n=int(input()) queue = list(map(int,input().split())) queue = queue[::-1] outf = [''] * n for i in range(n): a = queue.index(0) outf[a]=str(i+1) for j in range(i+1): queue[j]=queue[j]-i-1 outf=outf[::-1] print(' '.join(outf))
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int M = 2e5 + 10; const int inf = 0x3f3f3f3f; const long long mod = 1e9 + 7; set<int> s; long long a[M], ans[M]; long long sum[4 * M]; int v[M * 4]; void pushup(int x) { sum[x] = 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); } long long query(int l, int r, int i) { if (l == r) { sum[i] = inf; return l; } 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); pushup(i); 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; } for (int i = 1; i <= n; i++) { printf("%d ", ans[i]); } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main0(); int main() { ios::sync_with_stdio(false); main0(); return 0; } const long long INF = 0x3f3f3f3f; const int N = 1e6; struct node { long long id, v; } a[N]; int ans[N]; bool operator<(const node& a, const node& b) { if (a.v == b.v) return a.id > b.id; return a.v < b.v; } int main0() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i].v; a[i].id = i; } sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) { ans[a[i].id] = i; } for (int i = 1; i <= n; i++) { cout << ans[i] << ' '; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int read() { int 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 { int C[1000005]; inline void Add(int x, int val) { for (register int i = x; i < 1000005; i += (i & (-i))) { C[i] += val; } } inline int Query(int x) { int ans = 0; for (register int i = x; i > 0; i -= (i & (-i))) { ans += C[i]; } return ans; } }; // namespace BIT using namespace BIT; int a[1000005], n; inline int BinSearch(int pos) { int l = 0, r = 1000005 - 1, ans; while (l < r - 1) { int mid = (l + r) >> 1; if (Query(mid) <= a[pos]) l = mid; else r = mid; } return r; } int ans[1000005]; int main() { n = read(); for (register int i = 1; i <= n; ++i) a[i] = read(); for (register int i = 1; i <= n; ++i) { Add(i, i); } for (register int i = n; i >= 1; --i) { int pos = BinSearch(i); Add(pos, -pos); ans[i] = pos; } for (register int i = 1; i <= n; ++i) { printf("%d ", ans[i]); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const double EPS = 1e-9; const long long MOD = (long long)(1e9 + 7); const long long MAXV = (long long)(2e5 + 10); const long long MAXE = (long long)(1e6 + 10); int seg[MAXV * 4]; long long lazy[MAXV * 4]; long long A[MAXV * 4]; void init(int n, int s, int e, int p) { if (s + 1 == e) { seg[n] = s; } else { int m = (s + e) >> 1; if (p < m) init(n * 2, s, m, p); else init(n * 2 + 1, m, e, p); int l = seg[n * 2]; int r = seg[n * 2 + 1]; if (A[l] < A[r]) seg[n] = l; else seg[n] = r; } } void add_lazy(int n, int s, int e, long long x) { lazy[n] = x; A[seg[n]] += x; } void push(int n, int s, int e, long long x) { if (s + 1 == e) return; int m = (s + e) >> 1; add_lazy(n * 2, s, m, lazy[n]); add_lazy(n * 2 + 1, m, e, lazy[n]); lazy[n] = 0; } void update(int n, int s, int e, int a, int b, long long x) { if (s >= b || e <= a) return; if (s == a && e == b) add_lazy(n, s, e, x); else { if (lazy[n]) push(n, s, e, x); int m = (s + e) >> 1; if (b <= m) update(n * 2, s, m, a, b, x); else if (a >= m) update(n * 2 + 1, m, e, a, b, x); else { update(n * 2, s, m, a, m, x); update(n * 2 + 1, m, e, m, b, x); } int l = seg[n * 2]; int r = seg[n * 2 + 1]; if (A[l] < A[r]) seg[n] = l; else seg[n] = r; } } int n; long long ans[MAXV]; int main() { cin.sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> A[i]; for (int i = 0; i < n; i++) init(1, 0, n, i); long long curr = 1; for (int i = 0; i < n; i++) { long long p = seg[1]; ans[p] = curr; update(1, 0, n, p + 1, n, -curr); update(1, 0, n, p, p + 1, INF); curr++; } for (int i = 0; i < n; i++) cout << ans[i] << " "; cout << "\n"; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashSet; import java.util.Set; 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); Set<Integer> usedIndexes = new HashSet<>(); 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) { for(int nextIndex = currIndex + 1; nextIndex <= n; nextIndex++) { if(!usedIndexes.contains(nextIndex)) { res[i] = nextIndex; usedIndexes.add(nextIndex); fenwickTree.updateValue(nextIndex, -nextIndex); break; } } } } for (int i = 0; i < res.length; i++) { System.out.print(res[i] + " "); } } } class FenwickTree { private int[] arr; FenwickTree(int size) { this.arr = new int[size + 1]; for (int i = 0; i < arr.length; i++) { updateValue(i, i); } } int sum(int range) { int 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; } int getFirstNotNullValue(int index) { for (int i = index + 1; i < arr.length; i++) { if (arr[i] != 0) { arr[i] = 0; return i; } } return 0; } } 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 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long double EPS = 1e-9, PI = acos(-1.); const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const long long LINF = 0x3f3f3f3f3f3f3f3f; const int N = 2e5 + 5; int n, ans[N]; long long mi[4 * N], pos[4 * N], v[N], lz[4 * N]; void build(int p, int l, int r) { if (l == r) { mi[p] = v[l], pos[p] = l; return; } build(2 * p, l, (l + r) / 2); build(2 * p + 1, (l + r) / 2 + 1, r); mi[p] = min(mi[2 * p], mi[2 * p + 1]); if (mi[p] == mi[2 * p + 1]) pos[p] = pos[2 * p + 1]; else pos[p] = pos[2 * p]; } void push(int p, int l, int r) { if (lz[p]) { mi[p] += lz[p]; if (l != r) { lz[2 * p] += lz[p]; lz[2 * p + 1] += lz[p]; } lz[p] = 0; } } pair<long long, long long> query(int p, int l, int r, int i, int j) { push(p, l, r); if (r < i or l > j) return {LINF, 0}; if (i <= l and r <= j) return {mi[p], pos[p]}; pair<long long, long long> a = query(2 * p, l, (l + r) / 2, i, j); pair<long long, long long> b = query(2 * p + 1, (l + r) / 2 + 1, r, i, j); if (a.first < b.first) return a; else return b; } void update(int p, int l, int r, int i, int j, int v) { push(p, l, r); if (r < i or l > j) return; if (i <= l and r <= j) { lz[p] = v; push(p, l, r); return; } update(2 * p, l, (l + r) / 2, i, j, v); update(2 * p + 1, (l + r) / 2 + 1, r, i, j, v); mi[p] = min(mi[2 * p], mi[2 * p + 1]); if (mi[p] == mi[2 * p + 1]) pos[p] = pos[2 * p + 1]; else pos[p] = pos[2 * p]; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &v[i]); build(1, 1, n); memset(ans, -1, sizeof(ans)); for (int i = 1; i <= n; i++) { auto p = query(1, 1, n, 1, n); assert(p.first != LINF); ans[p.second] = i; update(1, 1, n, p.second + 1, n, -i); update(1, 1, n, p.second, p.second, LINF); } for (int i = 1; i <= n; i++) printf("%d ", ans[i]); printf("\n"); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import static java.lang.Math.*; public class Solution{ static InputReader sc; static PrintWriter wc; static int[] arr,tree,lazy; static void build(int l,int r,int i){ if(l==r){ tree[i]=arr[l]; return; } int m=(l+r)>>1; build(l,m,2*i+1); build(m+1,r,2*i+2); tree[i]=Math.min(tree[2*i+1],tree[2*i+2]); } static void update(int i,int val){ tree[i]+=val; lazy[i]+=val; } static void lazy_update(int ql,int qr,int l,int r,int i,int val){ if(l>qr||r<ql){ return; } else{ if(l>=ql&&r<=qr){ tree[i]+=val; lazy[i]+=val; } else{ int m=(l+r)>>1; lazy_update(ql,qr,l,m,2*i+1,val); lazy_update(ql,qr,m+1,r,2*i+2,val); tree[i]=Math.min(tree[2*i+1],tree[2*i+2]); } } } static int getMin(int l,int r,int i){ if(l==r){ return l; } else{ if(lazy[i]!=0){ update(2*i+1,lazy[i]); update(2*i+2,lazy[i]); lazy[i]=0; } int m=(l+r)>>1,res; if(tree[2*i+2]==0){ res = getMin(m+1,r,2*i+2); } else res = getMin(l,m,2*i+1); tree[i]=Math.min(tree[2*i+1],tree[2*i+2]); return res; } } static void print(int arr[]){ for(int i=0;i<arr.length;i++){ wc.print(arr[i]+" "); } wc.println(); } public static void main(String[] args) { sc = new InputReader(System.in); wc = new PrintWriter(System.out); int n=sc.nextInt(); int i,tlen,res[]=new int[n],j,inf=(int) 2e9; tlen=(int)Math.ceil(Math.log(n)/Math.log(2))+1; tlen=(1<<tlen)-1; arr=new int[n]; tree=new int[tlen]; lazy = new int[tlen]; for(i=0;i<n;i++){ arr[i]=sc.nextInt(); } build(0,n-1,0); for(i=1;i<=n;i++){ j=getMin(0,n-1,0); res[j]=i; lazy_update(j,j,0,n-1,0,inf); lazy_update(j+1,n-1,0,n-1,0,-i); } print(res); wc.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, m, q, x, k, t, y, w, z, a[200500], ans[200500]; set<long long> s; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 0; i < n; ++i) cin >> a[i], s.insert(i + 1); for (int i = n - 1; i >= 0; --i) { k = sqrt(2 * a[i]); x = *(s.upper_bound(k)); ans[i] = x; s.erase(x); } for (int i = 0; i < n; ++i) cout << ans[i] << " "; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
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<int> > segtre(20); void fix(int i, int j) { int 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<int> a(n); for (int i = 0; i < n; i += 1) { int 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(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 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #define pb push_back #define A first #define B second using namespace std; typedef long long ll; typedef pair <int, int> pi; constexpr double PI = atan(1) * 4; constexpr int inf = 0x3f3f3f3f; inline int LSB(int i) {return (i & -i);} int n; ll s[200005], ans[200005]; struct Segment{ Segment* c[2]; ll lazy, sum; Segment(){ memset(c, 0, sizeof(c)); lazy = 0; sum = inf; } void combine(){ sum = inf; for(int i = 0; i < 2; i++){ if(c[i]) sum = min(sum, c[i]->sum); } } void down(){ if(lazy){ for(int i = 0; i < 2; i++){ if(c[i]){ c[i]->sum += lazy; c[i]->lazy += lazy; } } lazy = 0; } } } *root = new Segment(); void build(Segment*& node, int l, int r){ if(l > r) return; if(!node) node = new Segment(); if(l == r){ node->sum = s[l]; return; } int mid = (l + r) >> 1; build(node->c[0], l, mid); build(node->c[1], mid + 1, r); node->combine(); } void update(Segment*& node, int l, int r, int ql, int qr, ll val){ if(l > r || l > qr || r < ql || ql > qr) return; if(!node) node = new Segment(); if(ql <= l && qr >= r){ node->sum += val; node->lazy += val; return; } node->down(); int mid = (l + r) >> 1; update(node->c[0], l, mid, ql, qr, val); update(node->c[1], mid + 1, r, ql, qr, val); node->combine(); } int query(Segment*& node, int l, int r){//find right-most 0 if(!node) node = new Segment(); if(l == r) return l; node->down(); int mid = (l + r) >> 1; if(node->c[1]->sum == 0) return query(node->c[1], mid + 1, r); else return query(node->c[0], l, mid); } int main() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); cin >> n; for(int i = 1; i <= n; i++) cin >> s[i]; build(root, 1, n); for(int i = 1; i <= n; i++){ int pos = query(root, 1, n); ans[pos] = i; update(root, 1, n, pos, pos, inf); update(root, 1, n, pos + 1, n, -i); } for(int i = 1; i <= n; i++) cout << ans[i] << " "; cout << endl; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back typedef long long ll ; #define inf 1000000000 ll tree[1000000]={0}; ll index[1000000]={0}; ll lazy[1000000]={0}; ll a[1000000]={0}; void build(ll node,ll l,ll r){ if(l>r)return ; if(l==r){ tree[node]=a[l]; index[node]=l; return ; } build(2*node+1,l,(l+r)/2); build(2*node+2,(l+r)/2+1,r); if(tree[2*node+1]<tree[2*node+2]){ tree[node]=tree[2*node+1]; index[node]=index[2*node+1]; return ; } if(tree[2*node+1]>tree[2*node+2]){ tree[node]=tree[2*node+2]; index[node]=index[2*node+2]; return ; } tree[node]=tree[2*node+1]; index[node]=max(index[2*node+1],index[2*node+2]); } pair<ll ,ll > get(ll node,ll l,ll r,ll x,ll y){ if(lazy[node]!=0){ tree[node]+=lazy[node]; if(l!=r){ lazy[2*node+1]+=lazy[node]; lazy[2*node+2]+=lazy[node];} lazy[node]=0; } if((r<x)||(l>y)){ return mp(-inf,inf); } if(l>=x&&r<=y)return mp(index[node],tree[node]); pair<ll ,ll >ans1=get(2*node+1,l,(l+r)/2,x,y); pair<ll ,ll >ans2=get(2*node+2,(l+r)/2+1,r,x,y); if(tree[2*node+1]<tree[2*node+2]){ tree[node]=tree[2*node+1]; index[node]=index[2*node+1];} else if(tree[2*node+1]>tree[2*node+2]){ tree[node]=tree[2*node+2]; index[node]=index[2*node+2]; } else { tree[node]=tree[2*node+1]; index[node]=max(index[2*node+1],index[2*node+2]);} if(ans1.second<ans2.second){ return ans1; } else if(ans1.second>ans2.second){ return ans2; } else { return mp(max(ans1.first,ans2.first),ans2.second); } } void update(ll node,ll l,ll r,ll x,ll y,ll z){ if(lazy[node]!=0){ tree[node]+=lazy[node]; if(l!=r){ lazy[2*node+1]+=lazy[node]; lazy[2*node+2]+=lazy[node];} lazy[node]=0; } if(l>r)return ; if(r<x||l>y||x>y)return ; if(l>=x&&r<=y){ tree[node]+=z; if(l!=r){ lazy[2*node+1]+=z; lazy[2*node+2]+=z;} return ; } update(2*node+1,l,(l+r)/2,x,y,z); update(2*node+2,(l+r)/2+1,r,x,y,z); if(tree[2*node+1]<tree[2*node+2]){ tree[node]=tree[2*node+1]; index[node]=index[2*node+1]; return ; } if(tree[2*node+1]>tree[2*node+2]){ tree[node]=tree[2*node+2]; index[node]=index[2*node+2]; return ; } tree[node]=tree[2*node+1]; index[node]=max(index[2*node+1],index[2*node+2]); } int main(){ ll n; cin>>n; for(ll i=0;i<n;i++){ cin>>a[i]; } fill (tree,tree+1000000,inf); fill (lazy,lazy+1000000,0); fill (index,index+1000000,-inf); vector<ll>p(n); build(0,0,n-1); for(ll i=1;i<=n;i++){ ll is=get(0,0,n-1,0,n-1).first; ll d=get(0,0,n-1,0,n-1).second; //cout<<is<<' '<<d<<'\n'; p[is]=i; update(0,0,n-1,is,is,inf); update(0,0,n-1,is+1,n-1,-i); } for(ll i=0;i<n;i++){ cout<<p[i]<<' '; } cout<<'\n'; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) pos = [0]*n for i in range(2,n+1)[::-1]: p = i-1 s = p*(p+1)//2 for j in range(n)[::-1]: if not pos[j]: if a[j] == s: pos[j] = i break else: s-=p p-=1 for i in range(n): if not pos[i]: pos[i] = 1 break print(' '.join(list(map(str,pos))))
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
import java.util.Scanner; public class Ishu { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int n,i,j,temp; int[][] s=new int[2][200000]; int[] p=new int[200000]; n=scan.nextInt(); temp=n; for(i=0;i<n;++i) { s[0][i]=scan.nextInt(); s[1][i]=i+1; } for(i=0;i<n;++i) for(j=0;j<n-1;++j) if(s[0][j]<s[0][j+1]) { s[0][j]=s[0][j]+s[0][j+1]; s[0][j+1]=s[0][j]-s[0][j+1]; s[0][j]=s[0][j]-s[0][j+1]; s[1][j]=s[1][j]+s[1][j+1]; s[1][j+1]=s[1][j]-s[1][j+1]; s[1][j]=s[1][j]-s[1][j+1]; } for(i=0;i<n;++i) { p[s[1][i]-1]=temp; --temp; } for(i=0;i<n;++i) System.out.print(p[i]+" "); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 2 * 1e5 + 10; const long long MOD = 1e9 + 7; const long long INF = 1e18 + 10; long long n; long long a[4 * N]; long long t[4 * N]; long long mod[4 * N]; void printVector(vector<long long> s) { for (long long &x : s) { cout << x << " "; } cout << endl; } void pull(long long v) { t[v] = min(t[2 * v + 1], t[2 * v + 2]); } void apply(long long v, long long val) { t[v] += val; mod[v] += val; } void push(long long v) { if (mod[v] != 0) { apply(2 * v + 1, mod[v]); apply(2 * v + 2, mod[v]); mod[v] = 0; } } void build(long long v, long long l, long long r) { if (l + 1 == r) { t[v] = a[l]; } else { long long m = (r + l) >> 1; build(2 * v + 1, l, m); build(2 * v + 2, m, r); pull(v); } } void add(long long v, long long l, long long r, long long ql, long long qr, long long val) { if (r <= ql || qr <= l) { return; } else if (ql <= l && r <= qr) { apply(v, val); } else { push(v); long long m = (r + l) >> 1; add(2 * v + 1, l, m, ql, qr, val); add(2 * v + 2, m, r, ql, qr, val); pull(v); } } long long go_down(long long v, long long l, long long r) { if (l + 1 == r) { return l; } else { push(v); long long m = (r + l) >> 1; long long res = -1; if (t[2 * v + 2] == 0) { res = go_down(2 * v + 2, m, r); } else { res = go_down(2 * v + 1, l, m); } pull(v); return res; } } void init(long long n_) { n = n_; fill(t, t + 4 * N, 0); fill(mod, mod + 4 * N, 0); build(0, 0, n); } void add(long long l, long long r, long long val) { add(0, 0, n, l, r, val); } void add(long long pos, long long val) { add(0, 0, n, pos, pos + 1, val); } void solve() { long long n; cin >> n; fill(a, a + 4 * N, 0); vector<long long> ans(n); for (long long i = 0; i < n; i++) cin >> a[i]; init(n); for (long long i = 1; i <= n; i++) { long long pos = go_down(0, 0, n); ans[pos] = i; add(pos, INF); add(pos + 1, n, -1); } printVector(ans); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; t = 1; for (long long i = 0; i < t; i++) { solve(); } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.StringTokenizer; public class Main { private static int KvoElem = 131072 * 2; private static int KvoForSum = 262144 * 2; private static int tree[] = new int[KvoForSum]; private static int t[] = new int[KvoForSum * 2]; private static int arr[] = new int[KvoElem + 1]; private static int Result = 0; public static PrintWriter out; public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); Main user = new Main(); for (int i = KvoElem; i < KvoForSum; i++) user.update(i - KvoElem, 1); int n = sc.nextInt(); for (int i = 0; i < KvoElem; i++) { arr[i] = 0; } build(1, 0, KvoElem); long[] value = new long[n]; for (int i = 0; i < n; i++) { value[i] = sc.nextLong(); } List <Integer> list = new ArrayList <>(); for (int i = n - 1; i >= 0; i--) { long x = user.find_need(value[i]); user.searchNext((int) x); list.add(Result); update(1, 0, n, Result, Result); } for (int i = n - 1; i >= 0; i--) { System.out.print(list.get(i) + " "); } } private long find_need(long x) { int l = 0; int r = 200005; while (r > l) { long mid = (l + r) / 2; long val = get_sum(mid); if (val == x) { return mid + 1; } else if (val > x) { r = (int) mid; } else { l = (int) (mid + 1); } } return l + 1; } private static long get_sum(long x) { return x * (x + 1) / 2 - sum1(1, 0, KvoElem, 0, (int) x); } private static int sum(int index) { index += KvoElem; int res = tree[index]; while (index > 1) { if (index % 2 == 1) res += tree[index - 1]; index /= 2; } return res; } private void searchNext(int index) { index += KvoElem; if (tree[index] == 1) { update(index - KvoElem, 0); Result = index - KvoElem; } else { int SearcInd = sum(index - KvoElem) + 1; index = 1; while (Main.tree[index] != SearcInd && (index * 2 + 1) < Main.KvoForSum) { if (Main.tree[index] > SearcInd) { index *= 2; } else if (Main.tree[index] < SearcInd) { SearcInd -= Main.tree[index]; index++; } } UpdateIfRavno(index); } } private void UpdateIfRavno(int index) { while ((index * 2 + 1) < Main.KvoForSum) { if (Main.tree[index * 2 + 1] == 0) index *= 2; else index = index * 2 + 1; } Result = index - KvoElem; update(index - Main.KvoElem, 0); } private void update(int index, int value) { index += Main.KvoElem; int a = value - Main.tree[index]; while (index >= 1) { Main.tree[index] += a; index /= 2; } } public static void build(int v, int tl, int tr) { if (tl == tr) t[v] = arr[tl]; else { int tm = (tl + tr) / 2; build(v * 2, tl, tm); build(v * 2 + 1, tm + 1, tr); t[v] = t[v * 2] + t[v * 2 + 1]; } } public static long sum1(int v, int tl, int tr, int l, int r) { if (l > r) return 0; if (l == tl && r == tr) return t[v]; int tm = (tl + tr) / 2; return sum1(v * 2, tl, tm, l, Math.min(r, tm)) + sum1(v * 2 + 1, tm + 1, tr, Math.max(l, tm + 1), r); } public static void update(int v, int tl, int tr, int pos, int new_val) { if (tl == tr) t[v] = new_val; else { int tm = (tl + tr) / 2; if (pos <= tm) update(v * 2, tl, tm, pos, new_val); else update(v * 2 + 1, tm + 1, tr, pos, new_val); t[v] = t[v * 2] + t[v * 2 + 1]; } } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int v[201000]; int tree[201000 * 4]; int lazy[201000 * 4]; void build(int i, int j, int n) { if (i == j) { tree[n] = v[i]; lazy[n] = 0; return; } int mid = (i + j) >> 1; build(i, mid, n << 1); build(mid + 1, j, (n << 1) | 1); tree[n] = min(tree[n << 1], tree[(n << 1) | 1]); lazy[n] = 0; } void f(int n) { if (!lazy[n]) return; lazy[n << 1] += lazy[n]; lazy[(n << 1) | 1] += lazy[n]; tree[n] += lazy[n]; lazy[n] = 0; } void update(int l, int r, int i, int j, int n, int k) { f(n); if (i > r || j < l) return; if (i >= l && j <= r) { lazy[n] += k; f(n); return; } int mid = (i + j) >> 1; update(l, r, i, mid, n << 1, k); update(l, r, mid + 1, j, (n << 1) | 1, k); tree[n] = min(tree[n << 1], tree[(n << 1) | 1]); } int query(int l, int r, int i, int j, int n) { f(n); if (i > r || j < l) return 1e9; if (i >= l && j <= r) { return tree[n]; } int mid = (i + j) >> 1; return min(query(l, r, i, mid, n << 1), query(l, r, mid + 1, j, (n << 1) | 1)); } int ans[201000]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> v[i]; } build(1, n, 1); for (int p = 0; p < n; p++) { int i = 1, j = n + 1; while (j - i > 1) { int mid = (i + j) >> 1; if (query(mid, n, 1, n, 1) > 0) { j = mid; } else i = mid; } ans[i] = p + 1; update(i, i, 1, n, 1, 1e9); update(i + 1, n, 1, n, 1, -(p + 1)); } for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class Codeforce { static final long INF = (int) 1e15; static final int MAXN = (int) (2e6 + 10); static long[] TREE = new long[MAXN * 4]; static long[] LAZY = new long[MAXN * 4]; static int[] ANS = new int[MAXN]; public static void main(String[] args) { solve(); } static void update1(int left, int right, int idx, long val, int pos) { if (left > idx || right < idx) { return; } if (left == right) { TREE[pos] = val; return; } int mid = (left + right) / 2; update1(left, mid, idx, val, 2 * pos + 1); update1(mid + 1, right, idx, val, 2 * pos + 2); TREE[pos] = Math.min(TREE[2 * pos + 1], TREE[2 * pos + 2]); } static void update2(int left, int right, int qleft, int qright, long val, int pos) { if (left > qright || right < qleft) { return; } if (left >= qleft && right <= qright) { TREE[pos] += val; LAZY[pos] += val; return; } int mid = (left + right) / 2; update2(left, mid, qleft, qright, val, 2 * pos + 1); update2(mid + 1, right, qleft, qright, val, 2 * pos + 2); TREE[pos] = Math.min(TREE[2 * pos + 1], TREE[2 * pos + 2]); } static int query(int left, int right, int pos) { if (left == right) { return left; } TREE[2 * pos + 1] += LAZY[pos]; TREE[2 * pos + 2] += LAZY[pos]; LAZY[2 * pos + 1] += LAZY[pos]; LAZY[2 * pos + 2] += LAZY[pos]; LAZY[pos] = 0; int mid = (left + right) / 2; if (TREE[2 * pos + 2] == 0) { return query(mid + 1, right, 2 * pos + 2); } else { return query(left, mid, 2 * pos + 1); } } static void solve() { FastScanner sc = new FastScanner(); int n = sc.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); } for (int i = 0; i < n; i++) { update1(0, n - 1, i, arr[i], 0); } for (int i = 1; i <= n; i++) { int zero_pos = query(0, n - 1, 0); ANS[zero_pos] = i; update1(0, n - 1, zero_pos, INF, 0); update2(0, n - 1, zero_pos + 1, n - 1, -i, 0); } for (int i = 0; i < n-1; i++) { System.out.print(ANS[i]+" "); } System.out.print(ANS[n-1]); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { if (System.getProperty("ONLINE_JUDGE") == null) { // Input is a file ClassLoader classloader = Thread.currentThread().getContextClassLoader(); InputStream is = classloader.getResourceAsStream("input.txt"); InputStreamReader isr = new InputStreamReader(is); br = new BufferedReader(isr); } else { br = new BufferedReader(new InputStreamReader(System.in)); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
python3
class segTree(): def __init__(self, n): self.t = [0] * (n << 2) def update(self, node, l, r, index, value): if l == r: self.t[node] = value return mid = (l + r) / 2 if index <= mid: self.update(node*2, l, mid, index, value) else: self.update(node*2 + 1, mid + 1, r, index, value) self.t[node] = self.t[node*2] + self.t[node*2 + 1] def query(self, node, l, r, value): if l == r: return self.t[node] mid = (l + r) / 2 if self.t[node*2] >= value: return self.query(node*2, l, mid, value) return self.query(node*2 + 1, mid + 1, r, value - self.t[node*2]) def do(): n = int(input()) nums = [int(i) for i in input().split(" ")] return "1" # res = [0]*n # weightTree = segTree(n) # for i in range(1, n+1): # weightTree.update(1, 1, n, i, i) # # print(weightTree.t) # for i in range(n-1, -1, -1): # res[i] = weightTree.query(1, 1, n, nums[i] + 1) # weightTree.update(1, 1, n, res[i], 0) # return " ".join([str(c) for c in res]) print(do())
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long t[4 * 200005]; long long lazy[4 * 200005], ans[200005], a[200005]; long long val = 1, pos; void build(long long v, long long tl, long long tr) { if (tl == tr) { t[v] = a[tl]; } else { long long tm = (tl + tr) / 2; build(v * 2, tl, tm); build(v * 2 + 1, tm + 1, tr); t[v] = min(t[v * 2], t[2 * v + 1]); } } void point_update(long long v, long long tl, long long tr, long long l, long long r, long long add) { if (l > r) return; if (l == tl && r == tr) { t[v] = add; } else { long long tm = (tl + tr) / 2; point_update(v * 2, tl, tm, l, min(r, tm), add); point_update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, add); t[v] = min(t[v * 2], t[v * 2 + 1]); } } void range_update(long long v, long long tl, long long tr, long long l, long long r, long long add) { if (l > r) return; if (l == tl && r == tr) { t[v] += add; lazy[v] += add; } else { long long tm = (tl + tr) / 2; range_update(v * 2, tl, tm, l, min(r, tm), add); range_update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, add); t[v] = min(t[v * 2], t[v * 2 + 1]); } } void get(long long v, long long tl, long long tr) { if (tl == tr) { pos = tl; ans[tl] = val; return; } long long tm = (tl + tr) / 2; if (lazy[v] != 0) { t[v * 2] += lazy[v]; t[v * 2 + 1] += lazy[v]; lazy[v * 2 + 1] += lazy[v]; lazy[v * 2] += lazy[v]; lazy[v] = 0; } if (t[v * 2 + 1] == 0) { get(v * 2 + 1, tm + 1, tr); } else { get(v * 2, tl, tm); } } void solve() { long long n; cin >> n; for (long long i = 1; i <= n; i++) cin >> a[i]; build(1, 1, n); for (long long i = 1; i <= n; i++) { get(1, 1, n); point_update(1, 1, n, pos, pos, 1e9); range_update(1, 1, n, pos + 1, n, -val); val++; } for (long long i = 1; i <= n; i++) cout << ans[i] << " "; } int32_t main() { long long 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 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, ar[200005], br[200005], up[800555]; pair<long long, long long> tr[800555]; void build(long long, long long, long long), update(long long, long long, long long, long long), upd(long long, long long, long long, long long, long long, long long); int main() { cin >> n; for (int i = 0; i < n; i++) cin >> ar[i]; build(0, 0, n - 1); long long pos; for (int i = 1; i < n + 1; i++) { pos = tr[0].second; br[pos] = i; if (pos + 1 < n) upd(0, 0, n - 1, pos + 1, n - 1, i); update(0, 0, n - 1, pos); } for (int i = 0; i < n; i++) cout << br[i] << " "; } void build(long long idx, long long l, long long r) { if (l == r) { tr[idx].first = ar[l]; tr[idx].second = l; return; } long long mid = (l + r) / 2; build(2 * idx + 1, l, mid); build(2 * idx + 2, mid + 1, r); if (tr[2 * idx + 1].first < tr[2 * idx + 2].first) tr[idx] = tr[2 * idx + 1]; else tr[idx] = tr[2 * idx + 2]; } void update(long long idx, long long l, long long r, long long pos) { if (up[idx]) { tr[idx].first -= up[idx]; if (l != r) { up[2 * idx + 1] += up[idx]; up[2 * idx + 2] += up[idx]; } up[idx] = 0; } if (pos < l || pos > r) return; if (l == r) { tr[idx].first = 12345678123456ll; return; } long long mid = (l + r) / 2; update(2 * idx + 1, l, mid, pos); update(2 * idx + 2, mid + 1, r, pos); if (tr[2 * idx + 1].first < tr[2 * idx + 2].first) tr[idx] = tr[2 * idx + 1]; else tr[idx] = tr[2 * idx + 2]; } void upd(long long idx, long long l, long long r, long long low, long long high, long long val) { if (up[idx]) { tr[idx].first -= up[idx]; if (l != r) { up[2 * idx + 1] += up[idx]; up[2 * idx + 2] += up[idx]; } up[idx] = 0; return; } if (high < l || low > r) { return; } if (low <= l && r <= high) { tr[idx].first -= val; if (l != r) { up[2 * idx + 1] += val; up[2 * idx + 2] += val; } return; } long long mid = (l + r) / 2; upd(2 * idx + 1, l, mid, low, high, val); upd(2 * idx + 2, mid + 1, r, low, high, val); if (tr[2 * idx + 1].first < tr[2 * idx + 2].first) tr[idx] = tr[2 * idx + 1]; else tr[idx] = tr[2 * idx + 2]; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; const long long inf = 1e11; struct Node { int l, r, num; long long tag; long long val; } node[4 * maxn]; long long a[maxn]; int ans[maxn]; void up(int no) { if (node[no << 1].val < node[no << 1 | 1].val) { node[no].val = node[no << 1].val; node[no].num = node[no << 1].num; } else { node[no].val = node[no << 1 | 1].val; node[no].num = node[no << 1 | 1].num; } } void build(int l, int r, int no) { node[no].l = l; node[no].r = r; node[no].tag = 0; if (l == r) { node[no].val = a[l]; node[no].num = l; return; } int mid = l + r >> 1; build(l, mid, no << 1); build(mid + 1, r, no << 1 | 1); up(no); } void pushdown(int no) { node[no << 1].tag += node[no].tag; node[no << 1].val += node[no].tag; node[no << 1 | 1].tag += node[no].tag; node[no << 1 | 1].val += node[no << 1 | 1].tag; node[no].tag = 0; } void change(int l, int r, int no, long long num) { if (node[no].r < l || node[no].l > r) return; if (node[no].l >= l && node[no].r <= r) { node[no].tag = num; node[no].val += num; return; } if (node[no].tag != 0) { pushdown(no); } change(l, r, no << 1, num); change(l, r, no << 1 | 1, num); up(no); } int main() { int n, i; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%lld", &a[i]); build(1, n, 1); for (i = 1; i <= n; i++) { int x = node[1].num; ans[x] = i; change(x, x, 1, inf); change(x + 1, n, 1, -i); } for (i = 1; i <= n; i++) { printf("%d ", ans[i]); } printf("\n"); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; public class D1208 { private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer st; private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } public static void main(String[] args) throws IOException { int n = nextInt(); Integer[] ss = new Integer[n]; for (int i = 0; i < n; i++) { ss[i] = nextInt(); } Integer[] result = new D1208().solve(n, ss); System.out.println(Arrays.asList(result)); } public Integer[] solve(Integer n, Integer[] ss) { List<Integer> result = new ArrayList<>(n); LinkedList<Integer> left = new LinkedList<>(IntStream.rangeClosed(1, n).boxed().collect(Collectors.toList())); for (Integer i = n - 1; i >= 0; i--) { Integer s = ss[i]; int y = 1; int x = 0; while (s > 0) { s -= y++; x++; } result.add(left.remove(x)); } Collections.reverse(result); return result.toArray(new Integer[n]); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<long long> st; vector<long long> nums; int n; long long buildst(int l, int r, int no) { if (l == r) return st[no] = nums[l]; return st[no] = buildst(l, (l + r) / 2, 2 * no + 1) + buildst((l + r) / 2 + 1, r, 2 * (no + 1)); } long long sr(int l, int r, int no, long long q) { if (q > st[no]) return -1; if (st[no] == q) { if (r != (n - 1) && nums[r + 1]) return r; else return max(sr(l, (l + r) / 2, 2 * no + 1, q), sr((l + r) / 2 + 1, r, 2 * (no + 1), q - st[2 * no + 1])); } if (st[2 * no + 1] < q) return sr((l + r) / 2 + 1, r, 2 * (no + 1), q - st[2 * no + 1]); else return sr(l, (l + r) / 2, 2 * no + 1, q); } long long upd(int l, int r, int no, int nou) { if (nou < l || r < nou) return st[no]; if (l == r) return st[no] = nums[l] = 0; return st[no] = upd(l, (l + r) / 2, 2 * no + 1, nou) + upd((l + r) / 2 + 1, r, 2 * (no + 1), nou); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int cmini = 1; cin >> n; vector<long long> s(n); for (int i = 0; i < n; i++) cin >> s[i]; nums.resize(n); for (int i = 0; i < n; i++) nums[i] = i + 1; st.resize(4 * n); buildst(0, n - 1, 0); vector<int> ans(n); for (int i = n - 1; i >= 0; i--) { if (s[i] == 0) ans[i] = cmini; else ans[i] = sr(0, n - 1, 0, s[i]) + 2; if (cmini == ans[i]) { for (int i = cmini; i < n; i++) { if (nums[i]) { cmini = i + 1; break; } } } upd(0, n - 1, 0, ans[i] - 1); } for (int i = 0; i < n; i++) cout << ans[i] << ' '; cout << '\n'; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
import javax.print.DocFlavor; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.nio.file.Paths; import java.util.*; public class Main extends PrintWriter { static BufferedReader s = new BufferedReader(new InputStreamReader(System.in));Main () { super(System.out); }public static void main(String[] args) throws IOException{ Main d1=new Main ();d1.main();d1.flush(); } void main() throws IOException { StringBuilder sb = new StringBuilder(); int t=1; // t=i(s()[0]); while(t-->0) { String[] s1=s(); int n=i(s1[0]);long[] a=new long[n]; arr(a,n); Pair[] p=new Pair[n]; for(int i=0;i<n;i++){ p[i]=new Pair((int)a[i],i); } Arrays.sort(p, new Comparator<Pair>() { @Override public int compare(Pair o1, Pair o2) { if(o1.val==o2.val) return o2.ind-o1.ind; return o1.val-o2.val; } });int[] ans=new int[n]; int num=1; for(int i=0;i<n;i++){ ans[p[i].ind]=num; num++; } for(int i:ans){ sb.append(i+" "); } // System.out.println(sb); } System.out.println(sb); } static String[] s() throws IOException { return s.readLine().trim().split("\\s+"); } static int i(String ss) {return Integer.parseInt(ss); } static long l(String ss) {return Long.parseLong(ss); } public void arr(long[] a,int n) throws IOException {String[] s2=s();for(int i=0;i<n;i++){ a[i]=l(s2[i]); }} } class Pair{ int ind,val; public Pair(int a,int b){ this.val=a;this.ind=b; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int P[200001]; ll sum[200001]; ll fen[200001]; int ans[200001]; void build() { for (int i = 1; i <= 200000; i++) { sum[i] = sum[i - 1] + ll(i - 1); fen[i] = sum[i] - sum[i - (i & -i)]; } } ll query(int l, int r) { if (l != 1LL) return query(1, r) - query(1, l - 1); ll ans = 0LL; while (r > 0) { ans += fen[r]; r -= r & -r; } return ans; } void update(int i) { ll taking = i; i++; while (i <= 200000) { fen[i] -= taking; i += (i & -i); } } int find(ll s) { ll cur = 0LL; int ind = 0; for (int b = 17; b >= 0; b--) { if (ind + (1 << b) <= 200000 && cur + fen[ind + (1 << b)] <= s) { cur += fen[ind + (1 << b)]; ind += (1 << b); } } return ind; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> P[i]; build(); for (int i = n; i >= 1; i--) { ans[i] = find(P[i]); update(ans[i]); } for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } cout << endl; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int is; int main() { int n; cin >> n; vector<long long> s(n); for (int i = 0; i < n; i++) cin >> s[i]; vector<long long> p(n); set<int> done; for (int i = n - 1; i >= 0; i--) { int l = 1, r = n; long long is = n; while (l <= r) { long long mid = (l + r) / 2; if (((mid) * (mid + 1) / 2) >= s[i]) { is = min(mid, is); r = mid - 1; } else l = mid + 1; } while (done.count(is)) { is++; } p[i] = is; done.insert(is); } for (int i = 0; i < n; i++) { cout << p[i] << ' '; } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int infI = 1e9 + 5; const long long infL = 1e18; const int MOD = 1e9 + 7; const int MAX = 1e6 + 5; long long tree[4 * MAX], delta[4 * MAX], hi[4 * MAX], lo[4 * MAX], pos[4 * MAX], a[MAX], b[MAX]; void uptree(int st) { if (tree[st * 2 + 1] + delta[st * 2 + 1] <= tree[st * 2] + delta[st * 2]) { tree[st] = tree[st * 2 + 1]; pos[st] = pos[st * 2 + 1]; } else { tree[st] = tree[st * 2]; pos[st] = pos[st * 2]; } } void init(int l, int r, int st) { hi[st] = r; lo[st] = l; if (l == r) { pos[st] = l; tree[st] = a[l]; return; } int mid = (l + r) / 2; init(l, mid, 2 * st); init(mid + 1, r, 2 * st + 1); uptree(st); } void up_delta(int st) { delta[st * 2] += delta[st]; delta[2 * st + 1] += delta[st]; delta[st] = 0; } void update(int l, int r, int st, int val) { if (l <= lo[st] && hi[st] <= r) { delta[st] += val; return; } if (l > hi[st] || r < lo[st]) { return; } up_delta(st); update(l, r, st * 2, val); update(l, r, st * 2 + 1, val); uptree(st); } int rmq(int l, int r, int st) { if (l <= lo[st] && hi[st] <= r) { return pos[st]; } if (l > hi[st] || r < lo[st]) { return -1; } int a1 = rmq(l, r, st * 2), a2 = rmq(l, r, st * 2 + 1); if (a[a2] <= a[a1]) { return a2; } else { return a1; } } int main() { int n; cin >> n; for (int i = 1; i < n + 1; i++) { cin >> a[i]; } init(1, n, 1); for (int i = 1; i < n + 1; i++) { int pos = rmq(1, n, 1); b[pos] = i; update(pos, pos, 1, infI); update(pos, n, 1, -i); } for (int i = 1; i < n + 1; i++) { cout << b[i] << " "; } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int n, s[N], ans[N]; struct node { int l, r; long long minn, lazy; } t[N * 4]; void build(int p, int l, int r) { t[p].l = l; t[p].r = r; if (l == r) { t[p].minn = s[l]; t[p].lazy = 0; return; } int mid = (l + r) >> 1; build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r); t[p].minn = min(t[p << 1].minn, t[p << 1 | 1].minn); return; } void push_down(int p) { if (!t[p].lazy) return; t[p << 1].minn += t[p].lazy; t[p << 1 | 1].minn += t[p].lazy; t[p << 1].lazy += t[p].lazy; t[p << 1 | 1].lazy += t[p].lazy; t[p].lazy = 0; } int find_0(int p, int l, int r) { if (l == r) { return l; } push_down(p); int mid = (l + r) >> 1; if (t[p << 1 | 1].minn == 0) { return find_0(p << 1 | 1, mid + 1, r); } else return find_0(p << 1, l, mid); } void add(int p, int l, int r, long long val) { if (t[p].l >= l && t[p].r <= r) { t[p].lazy += val; t[p].minn += val; return; } int mid = (t[p].l + t[p].r) >> 1; if (l <= mid) { add(p << 1, l, r, val); } if (r > mid) add(p << 1 | 1, l, r, val); t[p].minn = min(t[p << 1].minn, t[p << 1 | 1].minn); } int main(void) { cin >> n; for (int i = 1; i <= n; i++) scanf("%d", &s[i]); build(1, 1, n); for (int i = 1; i <= n; i++) { int pos = find_0(1, 1, n); ans[pos] = i; add(1, pos, pos, 0x7f7f7f7f); if (pos + 1 <= n) add(1, pos + 1, n, -i); } for (int i = 1; i <= n; i++) cout << ans[i] << " "; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void ECHO(string _s) { cout << endl; (void)_s; } template <typename T, typename... Args> void ECHO(string _s, T x, Args... args) { unsigned _i; string _s2 = ""; for (_i = 0; _i < _s.length(); ++_i) { if (_s[_i] == ',') break; if (_s[_i] != ' ') _s2 += _s[_i]; } if (_i == _s.length()) { --_i; } cout << " (" << _s2 << "):" << x; ECHO(_s.substr(_i + 1, _s.length() - _i - 1), args...); } template <typename T0, typename T1> inline ostream& operator<<(ostream& os, pair<T0, T1>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename T> inline ostream& operator<<(ostream& os, vector<T>& v) { for (unsigned i = 0; i < v.size(); ++i) { cout << v[i] << "_"; } cout << endl; return os; } template <typename T> inline T _min(T x1, T x2, T x3) { return min(x1, min(x2, x3)); } template <typename T> inline T _min(T x1, T x2, T x3, T x4) { return min(min(x1, x2), min(x2, x3)); } inline int _gcd(int a, int b) { while (b) b %= a ^= b ^= a ^= b; return a; } pair<long long, long long> tree[800000]; int lazy[800000]; int dat[200000]; pair<long long, long long> merge(pair<long long, long long> a, pair<long long, long long> b) { if (b.second <= a.second) return b; return a; } void build(int k, int l, int r) { if (r == l) { tree[k] = make_pair(l, dat[l]); return; } int mid = (r + l) / 2; build(2 * k, l, mid); build(2 * k + 1, mid + 1, r); tree[k] = merge(tree[k * 2], tree[k * 2 + 1]); } void propagate(int k) { if (!lazy[k]) return; lazy[2 * k] += lazy[k]; lazy[2 * k + 1] += lazy[k]; tree[k].second += lazy[k]; lazy[k] = 0; } void update(int k, int l, int r, int val, int ul, int ur) { if (l != r) propagate(k); else tree[k].second += lazy[k]; if (r < ul || l > ur) return; if (r <= ur && l >= ul) { lazy[k] += val; if (l != r) propagate(k); else tree[k].second += lazy[k]; return; } int mid = (r + l) / 2; update(k * 2, l, mid, val, ul, ur); update(k * 2 + 1, mid + 1, r, val, ul, ur); tree[k] = merge(tree[k * 2], tree[k * 2 + 1]); } int main() { ios::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> dat[i]; } build(1, 0, n - 1); vector<int> res(n); for (int i = 0; i < n; i++) { propagate(1); pair<long long, long long> m = tree[1]; res[m.first] = i + 1; if (m.first + 1 <= n - 1) update(1, 0, n - 1, -(i + 1), (int)m.first + 1, n - 1); update(1, 0, n - 1, INT_MAX, (int)m.first, (int)m.first); } for (int i = 0; i < n; i++) { if (i) cout << " "; cout << res[i]; } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DRestorePermutation solver = new DRestorePermutation(); solver.solve(1, in, out); out.close(); } static class DRestorePermutation { public void solve(int testNumber, FastReader s, PrintWriter out) { int n = s.nextInt(); int[] arr = s.nextIntArray(n); int[] ans = new int[n]; DRestorePermutation.SegmentTree st = new DRestorePermutation.SegmentTree(n); for (int i = 0; i < n; i++) { st.increment(i, i, arr[i]); } for (int i = 1; i <= n; i++) { st.minimum(0, n - 1); int ans1 = (int) st.ans[1]; ans[ans1] = i; st.increment(ans1 + 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 boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }