Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; class { public: pair<long long, long long> T[200009 << 2]; long long lz[200009 << 2]; long long n, f[200009], ans[200009]; void solve() { cin >> n; for (int i = 1; i <= n; ++i) cin >> f[i]; build(1, 1, n); for (int i = 1; i <= n; ++i) { auto res = query(1, 1, n, 1, n); ans[res.second] = i; update(1, 1, n, res.second + 1, n, -i); update(1, 1, n, res.second, res.second, 1e18); } for (int i = 1; i <= n; ++i) cout << ans[i] << " "; } void build(int rt, int l, int r) { lz[rt] = 0; if (l == r) { T[rt] = {f[l], l}; return; } int mid = l + r >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); up(rt); } void up(int rt) { if (T[rt << 1].first == T[rt << 1 | 1].first) { T[rt] = T[rt << 1 | 1]; } else { T[rt] = min(T[rt << 1], T[rt << 1 | 1]); } } void update(int rt, int l, int r, int ll, int rr, long long 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 " ] }
CORRECT
java
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { static final long MOD = 998244353; public static void main(String[] args) throws IOException { FastScanner sc=new FastScanner(); int N = sc.nextInt(); long[] nums = new long[N]; for (int i = 0; i < N; i++) nums[i] = sc.nextLong(); BinaryIndexedTree bit = new BinaryIndexedTree(N); for (int i = 1; i <= N; i++) { bit.add(i,i-1); } int[] ans = new int[N]; for (int i = N-1; i >= 0; i--) { long target = nums[i]; int L = 0; int R = N; while (L < R-1) { int mid = (L+R+1)/2; if (bit.sum(0,mid) <= target) { L = mid; } else { R = mid; } } ans[i] = L+1; bit.add(-1 * ans[i],L); } for (int i = 0; i < N; i++) System.out.print(ans[i] + " "); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class Node { public int n; public HashSet<Node> children; public Node(int n) { this.n = n; children = new HashSet<Node>(); } public void addChild(Node node) { children.add(node); } public void removeChild(Node node) { children.remove(node); } } class BinaryIndexedTree { public long[] arr; public BinaryIndexedTree (int N) { arr = new long[N+1]; arr[0] = 0; } //add k to the i-th element. public void add(long k, int i) { int node = i+1; while (node < arr.length) { arr[node] += k; node += node & (-node); } } //sum up the elements from input[s_i] to input[e_i], from [s_i,e_i). public long sum(int s_i, int e_i) { return sum(e_i) - sum(s_i); } public long sum(int i) { long total = 0; int node = i; while (node > 0) { total += arr[node]; node -= node & (-node); } return total; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.io.IOException; import java.util.*; //import javafx.util.Pair; //import java.util.concurrent.LinkedBlockingDeque; import javax.swing.Icon; //import sun.net.www.content.audio.wav; import java.text.DecimalFormat; public class Codeforces { public static long mod = (long)Math.pow(10, 9)+7 ; public static double epsilon=0.00000000008854;//value of epsilon public static InputReader sc = new InputReader(System.in); public static PrintWriter pw = new PrintWriter(System.out); public static int countSet(int a){ int c=0; while(a>0){ a&=(a-1); c++; } return c; } public static void Zfunction(String s){ int n=s.length(); int a[]=new int[n]; int r1=0,r2=1; for(int i=1;i<n;){ int f=0; while(r2<n&&s.charAt(r1)==s.charAt(r2)){ r1++; r2++; } a[i]=r1; i++; int l2=1; for(;i<r2;i++){ if(i+a[l2]<r2) a[i]=a[l2++]; else{ f=1; r1=r2-i; break; } } if(f==0){ r2=i; r1=0; } } pw.println(Arrays.toString(a)); } public static ArrayList<ArrayList <Integer>> GetGraph(int n,int m){ ArrayList<ArrayList <Integer>> a=new ArrayList<>(); for(int i=0;i<n;i++){ a.add(new ArrayList<>()); } for(int i=0;i<m;i++){ int u=sc.nextInt()-1; int v=sc.nextInt()-1; a.get(u).add(v); a.get(v).add(u); } return a; } public static int firstNode(int a,int p[]){ if(a!=p[a]){ p[a]=firstNode(p[a], p); } return p[a]; } public static void Union(int a,int b,int p[]){ //int a=firstNode(a1, p); //int b=firstNode(b1, p); /*if(a!=b){ if(r[a]<r[b]){ p[a]=b; } else if(r[a]>r[b]){ p[b]=a; } else{ r[b]++; p[a]=b; } }*/ if(a!=b) p[firstNode(a,p)]=firstNode(b,p);//if no rank than } public static void main(String[] args) { // code starts.. int n=sc.nextInt(); long a[]=scanLongArray(n); Bit h=new Bit(n); for(int i=1;i<=n;i++){ h.update(i, i); } int ans[]=new int[n]; for(int i=n;i>0;i--){ int in=h.searchNumber(a[i-1]); ans[i-1]=in+1; h.update(in+1, -(in+1)); //pw.println(h.query(1)); } for(int i=0;i<n;i++) pw.println(ans[i]); // Code ends... pw.flush(); pw.close(); } public static Comparator<Integer> C(){ return new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2.compareTo(o1);//for descending } }; } static class tripletL implements Comparable<tripletL> { Long x, y, z; tripletL(long x, long y, long z) { this.x = x; this.y = y; this.z = z; } public int compareTo(tripletL o) { int result = x.compareTo(o.x); if (result == 0) result = y.compareTo(o.y); if (result == 0) result = z.compareTo(o.z); return result; } public boolean equlas(Object o) { if (o instanceof tripletL) { tripletL p = (tripletL) o; return (x - p.x == 0) && (y - p.y ==0 ) && (z - p.z == 0); } return false; } public String toString() { return x + " " + y + " " + z; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode() + new Long(z).hashCode(); } } public static String Doubleformate(double a,int n){ String s=""; while(n-->0){ s+='0'; } DecimalFormat f =new DecimalFormat("#0."+s); return f.format(a); } public static Comparator<Integer[]> column(int i){ return new Comparator<Integer[]>() { @Override public int compare(Integer[] o1, Integer[] o2) { return o1[i].compareTo(o2[i]);//for ascending //return o2[i].compareTo(o1[i]);//for descending } }; } public static Comparator<Long[]> column1(int i){ return new Comparator<Long[]>() { @Override public int compare(Long[] o1, Long[] o2) { return o1[i].compareTo(o2[i]);//for ascending //return o2[i].compareTo(o1[i]);//for descending } }; } public static Comparator<Integer[]> pair(){ return new Comparator<Integer[]>() { @Override public int compare(Integer[] o1, Integer[] o2) { int result=o1[0].compareTo(o2[0]); if(result==0) result=o1[1].compareTo(o2[1]); return result; } }; } public static Comparator<Integer[]> Triplet(){ return new Comparator<Integer[]>() { @Override public int compare(Integer[] o1, Integer[] o2) { for(int i=0;i<3;i++){ for(int j=i+1;j<3;j++){ for(int k=0;k<3;k++){ for(int p=k+1;p<3;p++){ if((o1[i]==o2[k]&&o1[j]==o2[p])||(o1[j]==o2[k]&&o1[i]==o2[p])){ } } } } } int result=o1[0].compareTo(o2[0]); if(result==0) result=o1[1].compareTo(o2[1]); if(result==0) result=o1[2].compareTo(o2[2]); return result; } }; } public static String reverseString(String s){ StringBuilder input1 = new StringBuilder(); input1.append(s); input1 = input1.reverse(); return input1.toString(); } public static int[] scanArray(int n){ int a[]=new int [n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); return a; } public static long[] scanLongArray(int n){ long a[]=new long [n]; for(int i=0;i<n;i++) a[i]=sc.nextLong(); return a; } public static String [] scanStrings(int n){ String a[]=new String [n]; for(int i=0;i<n;i++) a[i]=sc.nextLine(); return a; } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class Bit{//1...n long a[];int n; Bit(int n){ this.n=n; a=new long[n+1]; } void update(int i,int delta){ while(i<a.length){ a[i]+=delta; i+=i&(-i); } } long query(int i){ long sum=0; while(i>0){ sum+=a[i]; i-=i&(-i); } return sum; } int searchNumber(long sum){ int l=1,r=n,ans=0; while(l<=r){ int m=(l+r)/2; long v=query(m); if(v>sum){ r=m-1; } else if(v<sum){ l=m+1; } else{ ans=m; l=m+1; } } return ans; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const long long inf = 1000000000000; const int N = 200005; const long long LLMAX = 2e18; const long long MOD = 1000000009ll; const double eps = 1e-8; const int MAXN = 1e6 + 10; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } 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 * 10 + (ch - '0'); ch = getchar(); } return x * f; } int n, ans[N]; long long a[N]; struct SegmentTree { int l, r, id; long long v, add; } tree[N * 4]; void push_up(int i) { if (tree[i << 1 | 1].v == tree[i << 1].v) { if (tree[i << 1 | 1].id > tree[i << 1].id) { tree[i].id = tree[i << 1 | 1].id; tree[i].v = tree[i << 1 | 1].v; } else { tree[i].id = tree[i << 1].id; tree[i].v = tree[i << 1].v; } } else { if (tree[i << 1].v < tree[i << 1 | 1].v) { tree[i].id = tree[i << 1].id; tree[i].v = tree[i << 1].v; } else { tree[i].id = tree[i << 1 | 1].id; tree[i].v = tree[i << 1 | 1].v; } } return; } void build(int p, int l, int r) { tree[p].l = l, tree[p].r = r; if (l == r) { tree[p].v = a[l]; tree[p].id = r; return; } int mid = (l + r) >> 1; build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r); push_up(p); } void spread(int p) { if (tree[p].add) { tree[p << 1].add += tree[p].add; tree[p << 1 | 1].add += tree[p].add; tree[p << 1].v += tree[p].add; tree[p << 1 | 1].v += tree[p].add; tree[p].add = 0; } } void change(int p, int l, int r, long long d) { if (l <= tree[p].l && r >= tree[p].r) { tree[p].v += d; tree[p].add += d; return; } spread(p); int mid = (tree[p].l + tree[p].r) >> 1; if (l <= mid) change(p << 1, l, r, d); if (r > mid) change(p << 1 | 1, l, r, d); push_up(p); } int main(int argc, char const *argv[]) { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]); build(1, 1, n); for (int i = 1; i <= n; ++i) { ans[tree[1].id] = i; int d = tree[1].id; change(1, d, d, 10000000000000); change(1, d + 1, n, -i); } for (int i = 1; i <= n; ++i) { printf("%d%c", ans[i], " \n"[i == n]); } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int INF = 2e9; const long long INFLL = 1e18; const int MAX_N = 1; int N; vector<long long> v; struct SEG { struct NODE { int l, r; long long data; }; vector<NODE> seg; int SZ; void add() { seg.push_back((NODE){-1, -1, (long long)0}); } void Init(int x) { SZ = x; add(); init(0, 1, SZ); } void init(int idx, int s, int e) { if (s == e) return; seg[idx].l = seg.size(); add(); seg[idx].r = seg.size(); add(); init(seg[idx].l, s, (s + e) / 2); init(seg[idx].r, (s + e) / 2 + 1, e); } void Update(int x, long long y) { update(0, 1, SZ, x, y); } void update(int idx, int s, int e, int x, long long y) { seg[idx].data += y; if (s == e) return; if (x <= (s + e) / 2) update(seg[idx].l, s, (s + e) / 2, x, y); else update(seg[idx].r, (s + e) / 2 + 1, e, x, y); } long long Sum(int x, int y) { return sum(0, 1, SZ, x, y); } long long sum(int idx, int s, int e, int x, int y) { if (x <= s && e <= y) return seg[idx].data; else if (x > e || y < s) return 0; return sum(seg[idx].l, s, (s + e) / 2, x, y) + sum(seg[idx].r, (s + e) / 2 + 1, e, x, y); } }; SEG Seg; vector<long long> ans; set<int> st; int main() { cin >> N; Seg.Init(N); for (int i = 0; i < N; i++) { long long x; scanf("%lld", &x); v.push_back(x); } for (int i = 1; i <= N; i++) { Seg.Update(i, i); st.insert(i); } while (!v.empty()) { long long x = v.back(); v.pop_back(); int s = 1, e = N, m; while (s < e) { m = (s + e) / 2 + 1; if (Seg.Sum(1, m - 1) > x) { e = m - 1; } else { s = m; } } s = (*st.lower_bound(s)); st.erase(s); ans.push_back(s); Seg.Update(s, -s); } while (!ans.empty()) { printf("%d ", (int)ans.back()); ans.pop_back(); } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.lang.*; import java.util.*; import java.io.*; public class Main { void solve() { int n=ni(); a=new long[n+1]; for(int i=1;i<=n;i++) a[i]=nl(); tree=new Node[4*n+1]; lazy=new long[4*n+1]; build(1,1,n); int ans[]=new int[n+1]; for(int i=1;i<=n;i++){ int id=query(1,1,n,1,n).id; update(1,id,id,1,n,Long.MAX_VALUE); update(1,id+1,n,1,n,-i); ans[id]=i; } for(int i=1;i<=n;i++) pw.print(ans[i]+" "); pw.println(""); } long a[]; long lazy[]; Node tree[]; void build(int id,int l,int r){ if(l==r){ tree[id]=new Node(l,a[l]); }else { int mid=(l+r)>>1; build(2*id,l,mid); build(2*id+1,mid+1,r); merge(id); } } void update(int id,int x,int y,int l,int r,long val){ if(x>y) return; push(id,l,r); if(r<x || l>y) return; if(x<=l && r<=y){ tree[id].val+=val; if(l!=r){ lazy[2*id]+=val; lazy[2*id+1]+=val; } return; } int mid=(l+r)>>1; update(2*id,x,y,l,mid,val); update(2*id+1,x,y,mid+1,r,val); merge(id); } Node query(int id,int x,int y,int l,int r){ push(id,l,r); if(r<x || l>y) return new Node(Integer.MAX_VALUE,Long.MAX_VALUE); if(x<=l && r<=y) return tree[id]; int mid=(l+r)>>1; Node nd1=query(2*id,x,y,l,mid); Node nd2=query(2*id+1,x,y,mid+1,r); if(nd1.val<nd2.val) return nd1; else if(nd1.val>nd2.val) return nd2; return new Node(Math.max(nd1.id,nd2.id),nd1.val); } void push(int id,int l,int r){ if(lazy[id]==0) return; tree[id].val+=lazy[id]; if(l!=r){ lazy[2*id]+=lazy[id]; lazy[2*id+1]+=lazy[id]; } lazy[id]=0; } void merge(int id){ if(tree[2*id].val<tree[2*id+1].val){ tree[id]=new Node(tree[2*id].id,tree[2*id].val); }else if(tree[2*id].val>tree[2*id+1].val){ tree[id]=new Node(tree[2*id+1].id,tree[2*id+1].val); }else tree[id]=new Node(Math.max(tree[2*id].id,tree[2*id+1].id),tree[2*id].val); } class Node { int id; long val; public Node(int id,long val){ this.id=id; this.val=val; } } long M = (long)1e9+7; // END PrintWriter pw; StringTokenizer st; BufferedReader br; void run() throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); pw.flush(); } public static void main(String[] args) throws Exception { new Main().run(); } String ns() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } String nextLine() throws Exception { String str = ""; try { str = br.readLine(); } catch (IOException e) { throw new Exception(e.toString()); } return str; } int ni() { return Integer.parseInt(ns()); } long nl() { return Long.parseLong(ns()); } double nd() { return Double.parseDouble(ns()); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> const int inf = INT_MAX; const int MAX = 3e6 + 9; const long long MOD = 1e9 + 7; const int TOT_PRIMES = 1e6 + 9; const int MAX_A = 71; const int LN = 20; using namespace std; long long bt[MAX]; long long arr[MAX]; void update(int x, long long val) { for (int i = x; i < MAX; i += i & -i) bt[i] += val; } long long query(int x) { long long res = 0; for (int i = x; i > 0; i -= i & -i) res = res + bt[i]; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> arr[i]; for (int i = 1; i <= n; i++) update(i, i); int ans[n + 1]; for (int i = n; i >= 1; i--) { int l = 0, r = n; while (l <= r) { int mid = (l + r) / 2; if (query(mid) > arr[i]) r = mid - 1; else l = mid + 1; } ans[i] = l; update(l, -l); } for (int i = 1; i <= n; i++) cout << ans[i] << " "; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200010; long long fen[N]; void modify(int x, long long v) { while (x < N) { fen[x] += v; x |= x + 1; } } long long get(int x) { long long r = 0; while (x >= 0) { r += fen[x]; x = (x & (x + 1)) - 1; } return r; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<long long> s(n); for (int(i) = 0; (i) < (int)(n); ++(i)) cin >> s[i]; for (int(i) = 0; (i) < (int)(n); ++(i)) modify(i, i + 1); vector<int> a(n); for (int i = n - 1; i >= 0; i--) { int low = 0, high = n - 1; while (low < high) { int mid = (low + high) >> 1; if (get(mid) > s[i]) { high = mid; } else { low = mid + 1; } } a[i] = low + 1; modify(low, -low - 1); } for (int(i) = 0; (i) < (int)(n); ++(i)) cout << a[i] << ' '; cout << '\n'; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.util.*; import java.util.stream.IntStream; public class Laba { FScanner fs; PrintWriter pw; int n; boolean[] used; int[][] g; int[][] dst; long[] t; public static void main(String[] args) throws IOException { new Laba().start(); } public void start() throws IOException { fs = new FScanner(new InputStreamReader(System.in)); // reader = new FScanner(new FileReader("input")); //pw = new PrintWriter("input.txt"); pw = new PrintWriter(System.out); int n = fs.nextInt(); long[] arr = fs.larr(n); long[] ss = new long[n + 1]; long ann = 1; t = new long[(n+1) * 4]; HashMap<Long, Integer> map = new HashMap<>(); map.put(0l, 1); map.put(1l, 2); for (int i = 2; i < ss.length; i++) { map.put(ann + i, i + 1); ann += i; } long[] ar = new long[n + 1]; for (int i = 1; i < n+1; i++) ar[i] = i; build(ar, 1, 0, n); int[] ans = new int[n]; for (int i = n - 1; i >= 0; i--) { //pw.println(sum(1, 0, n, 0, 3)); ans[i] = findss(1, 0, n, arr[i]) ; update(1, 0, n, ans[i], 0); } for (int i =0 ; i <n; i++) pw.print(ans[i] + " "); pw.close(); } void build (long a[], int v, int tl, int tr) { if (tl == tr) t[v] = a[tl]; else { int tm = (tl + tr) / 2; build (a, v*2, tl, tm); build (a, v*2+1, tm+1, tr); t[v] = t[v*2] + t[v*2+1]; } } void update (int v, int tl, int tr, int pos, long 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]; } } long sum (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 sum (v*2, tl, tm, l, Math.min(r,tm)) + sum (v*2+1, tm+1, tr, Math.max(l,tm+1), r); } int findss(int v, int tl, int tr, long sum) { if (sum > t[v]) return -1; if (tl == tr) return tl; int tm = (tl + tr) /2; if (t[v*2] > sum) { return findss(v * 2, tl, tm, sum); } else { return findss(v * 2 + 1, tm + 1, tr, sum - t[v*2]); } } } class pair { long x, y, c; pair(long x1, long y1) { x = x1; y = y1; } } class FScanner { StringTokenizer st; BufferedReader reader; FScanner(InputStreamReader isr) throws IOException { reader = new BufferedReader(isr); } String nextLine() throws IOException { return reader.readLine(); } String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { String s = reader.readLine(); if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } char nextChar() throws IOException { return (char) reader.read(); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } int[] iarr(int n) throws IOException { int[] mas = new int[n]; for (int i = 0; i < n; i++) mas[i] = nextInt(); return mas; } double[] darr(int n) throws IOException { double[] mas = new double[n]; for (int i = 0; i < n; i++) mas[i] = nextDouble(); return mas; } char[][] cmas2(int n, int m) throws IOException { char[][] mas = new char[n][m]; for (int i = 0; i < n; i++) mas[i] = nextLine().toCharArray(); return mas; } long[] larr(int n) throws IOException { long[] mas = new long[n]; for (int i = 0; i < n; i++) mas[i] = nextLong(); return mas; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
python3
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!! # TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!! # TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!! from sys import stdin, stdout from collections import defaultdict from collections import deque import math import copy #T = int(input()) N = int(input()) #s1 = input() #s2 = input() #N,Q = [int(x) for x in stdin.readline().split()] arr = [int(x) for x in stdin.readline().split()] bit = [0]*(N+1) series = [0] + [x for x in range(N)] def lowbit(x): return x&(-x) def update(idx,delta): while idx<=N: bit[idx] += delta idx += lowbit(idx) def query(x): s = 0 while x>0: s += bit[x] x -= lowbit(x) return s # init for i in range(1,N+1): bit[i] += series[i] y = i + lowbit(i) if y<=N: series[y] += series[i] visited = [0]*(N+1) ans = [0]*N for i in range(N-1,-1,-1): # find left = 1 right = N target = arr[i] while left<=right: mid = (left+right)//2 q = query(mid) #print(mid,q) if q<target: left = mid + 1 elif q>target: right = mid - 1 else: if visited[mid]==1: left = mid + 1 else: visited[mid] = 1 ans[i] = mid break # update update(mid+1,-mid) print(*ans)
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 9; long long s[maxn]; int p[maxn]; int vis[maxn]; int temp; struct node { int l, r; long long minn, lazy; } tree[maxn << 2]; void BT(int p, int l, int r) { tree[p].l = l, tree[p].r = r; if (l == r) { tree[p].minn = s[l]; tree[p].lazy = 0; return; } int mid = (l + r) >> 1; BT(p << 1, l, mid); BT(p << 1 | 1, mid + 1, r); tree[p].minn = min(tree[p << 1].minn, tree[p << 1 | 1].minn); tree[p].lazy = 0; } void spread(int p) { tree[p << 1].lazy += tree[p].lazy; tree[p << 1].minn -= tree[p].lazy; tree[p << 1 | 1].lazy += tree[p].lazy; tree[p << 1 | 1].minn -= tree[p].lazy; tree[p].lazy = 0; } void Query(int p) { if (tree[p].l == tree[p].r) { tree[p].minn = 0x3f3f3f3f3f3f3f3f; temp = tree[p].l; return; } spread(p); if (tree[p << 1 | 1].minn == 0) { Query(p << 1 | 1); } else Query(p << 1); tree[p].minn = min(tree[p << 1].minn, tree[p << 1 | 1].minn); } void Update(int p, int l, int r, int v) { if (tree[p].l >= l && tree[p].r <= r) { tree[p].lazy += v; tree[p].minn -= v; return; } spread(p); int mid = (tree[p].l + tree[p].r) >> 1; if (l > mid) { Update(p << 1 | 1, l, r, v); } else if (r <= mid) Update(p << 1, l, r, v); else { Update(p << 1 | 1, l, r, v); Update(p << 1, l, r, v); } tree[p].minn = min(tree[p << 1].minn, tree[p << 1 | 1].minn); } int main() { int n; while (scanf("%d", &n) != EOF) { memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) { scanf("%lld", &s[i]); } BT(1, 1, n); for (int i = 1; i <= n; i++) { Query(1); p[temp] = i; if (temp < n) Update(1, temp + 1, n, i); } for (int i = 1; i <= n; i++) { if (i == 1) printf("%d", p[i]); else printf(" %d", p[i]); } printf("\n"); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.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 Egor Kulikov (egor@egork.net) */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); long[] s = in.readLongArray(n); FenwickTree tree = new FenwickTree(n + 1); for (int i = 1; i <= n; i++) { tree.add(i, i); } int[] answer = new int[n]; for (int j = n - 1; j >= 0; j--) { int left = 0; int right = n; while (left < right) { int mid = (left + right) >> 1; if (tree.get(0, mid) <= s[j]) { left = mid + 1; } else { right = mid; } } answer[j] = left; tree.add(left, -left); } out.printLine(answer); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public long[] readLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) { array[i] = readLong(); } return array; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void printLine(int[] array) { print(array); writer.println(); } public void close() { writer.close(); } } static class FenwickTree { private final long[] value; public FenwickTree(int size) { value = new long[size]; } public long get(int from, int to) { if (from > to) { return 0; } return get(to) - get(from - 1); } private long get(int to) { to = Math.min(to, value.length - 1); long result = 0; while (to >= 0) { result += value[to]; to = (to & (to + 1)) - 1; } return result; } public void add(int at, long value) { while (at < this.value.length) { this.value[at] += value; at = at | (at + 1); } } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
python3
def sum_number(n,j): j[0]=0 j[1]=0 for i in range(2,n+1): j[i]=j[i-1]+(i-1) return(j) po=int(input()) l=[0]*(po+1) l1=[int(i) for i in input().split()] def getsum(BITTree,i): s = 0 while i > 0: s += BITTree[i] i -= i & (-i) return(s) def updatebit(BITTree , n , i ,v): #print('n',n) while i <= n: #print('i',i) BITTree[i] += v i += i & (-i) #print(BITTree) for i in range(1,po+1): updatebit(l,po,i,i) output=[0]*po for i in range(po-1,-1,-1): min_=0 max_=po k=l1[i] while True: x=(min_+max_+1)//2 if getsum(l,x)>k: if getsum(l,x-1)==k: output[i]=x break else: #print(x) max_=x else : #print(x) min_=x updatebit(l,po,x,-x) print(*output)
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10, eps = 1e-10; int n; long long a[N]; long long tree[N]; int ans[N]; long long sum(int x) { long long res = 0; while (x) { res += tree[x]; x -= (x & (-x)); } return res; } void add(int x, long long sum) { while (x <= n) { tree[x] += sum; x += (x & (-x)); } return; } int main() { std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = (1); i < (n + 1); ++i) cin >> a[i], add(i + 1, i); for (int i = (n); i >= (1); --i) { long long l = 1, r = n; while (l <= r) { if (sum(((l + r) >> 1)) > a[i]) r = ((l + r) >> 1) - 1; else l = ((l + r) >> 1) + 1; } ans[i] = r; add(r + 1, -r); } for (int i = (1); i < (n + 1); ++i) cout << ans[i] << " "; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; long long cl[202020]; long long T[202020]; int chk[202020]; int ans[202020]; void m_Tree(int x, int t) { for (int i = x; i <= n; i += i & (-i)) { T[i] += t; } } long long g_Tree(int x) { long long ret = 0; for (int i = x; i > 0; i -= i & (-i)) ret += T[i]; return ret; } int BS(int s, int e, long long t) { if (s == e) return s; int m = (s + e) / 2 + 1; if (t >= g_Tree(m - 1)) return BS(m, e, t); return BS(s, m - 1, t); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", cl + i), m_Tree(i, i); for (int i = n; i > 0; i--) { ans[i] = BS(1, n, cl[i]); m_Tree(ans[i], -ans[i]); chk[ans[i]] = 1; } for (int i = 1; i <= n; i++) printf("%d ", ans[i]); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashMap; import java.util.HashSet; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader sc = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, sc, out); out.close(); } static class Task { public long[] C; public int lowBit(int x) { return x&(-x); } public void add(int x,int y) { for(int i=x;i<C.length;i+=lowBit(i)) C[i]+=y; } public long ask(int x) { long res=0; for(int i=x;i>0;i-=lowBit(i)) res+=C[i]; return res; } public void solve(int testNumber, InputReader sc, PrintWriter out) { int n=sc.nextInt(); C=new long[n+1]; long[] s=new long[n+1]; long[] res=new long[n+1]; boolean[] jud=new boolean[n+1]; for(int i=1;i<=n;i++) { s[i]=sc.nextLong(); add(i,i); } for(int i=n;i>=1;i--) { int l=1; int r=n; int ans=0; while(l<=r) { int mid=(l+r)>>1; long sum=ask(mid-1); if(s[i]==sum) { if(jud[mid]) { l=mid+1; continue; } ans=mid; jud[ans]=true; add(mid,-mid); break; } if(s[i]<sum) r=mid-1; else l=mid+1; } res[i]=ans; } for(int i=1;i<=n;i++) out.print(res[i]+" "); out.println(); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; typedef long double ld; const long double PI = 3.141592653589793238462643383279502884197169399375105820974944; const long long mod = 998244353; const long long inf = 1e18; const long long maxn = 1e5 + 5; struct BIT { vector<long long> bit; long long maxn; void init(long long n) { maxn = n; bit.assign(n + 1, 0); } void update(long long i, long long val) { for (; i <= maxn; i += (i & -i)) bit[i] += val; } long long sum(long long i) { long long res = 0; for (; i > 0; i -= (i & -i)) res += bit[i]; return res; } long long rsum(long long l, long long r) { return sum(r) - sum(l - 1); } }; long long n, a[maxn]; void solve() { long long n; cin >> n; BIT bit; bit.init(n + 5); long long a[n + 5]; set<long long> s; for (long long i = 1; i <= n; i++) { cin >> a[i]; bit.update(i, i); s.insert(i); } for (long long i = n; i >= 1; --i) { long long lb = 1, rb = n; while (lb < rb) { long long mid = (lb + rb + 1) / 2; if (bit.sum(mid - 1) > a[i]) rb = mid - 1; else if (bit.sum(mid - 1) < a[i]) lb = mid + 1; else lb = rb = mid; } auto it = s.lower_bound(lb); a[i] = *it; bit.update(*it, -*it); s.erase(it); } for (long long i = 1; i <= n; i++) cout << a[i] << " "; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long tc = 1; while (tc--) { solve(); cout << '\n'; } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<unsigned long long> t, lazy; void build(unsigned long long a[], int v, int tl, int tr) { if (tl == tr) { t[v] = a[tl]; } else { int tm = (tl + tr) / 2; build(a, v * 2 + 1, tl, tm); build(a, v * 2 + 2, tm + 1, tr); t[v] = min(t[v * 2 + 1], t[v * 2 + 2]); } } void add(int v, int tl, int tr, int l, int r, unsigned long long addend) { if (l > r) return; if (l == tl && tr == r) { t[v] += addend; lazy[v] += addend; } else { t[v * 2 + 1] += lazy[v]; lazy[v * 2 + 1] += lazy[v]; t[v * 2 + 2] += lazy[v]; lazy[v * 2 + 2] += lazy[v]; lazy[v] = 0; int tm = (tl + tr) / 2; add(v * 2 + 1, tl, tm, l, min(r, tm), addend); add(v * 2 + 2, tm + 1, tr, max(l, tm + 1), r, addend); t[v] = min(t[v * 2 + 1], t[v * 2 + 2]); } } int last_zero(int v, int l, int r) { if (l == r) { return l; } else { int m = (r + l) / 2; t[v * 2 + 1] += lazy[v]; lazy[v * 2 + 1] += lazy[v]; t[v * 2 + 2] += lazy[v]; lazy[v * 2 + 2] += lazy[v]; lazy[v] = 0; t[v] = min(t[v * 2 + 1], t[v * 2 + 2]); if (t[2 * v + 2]) { return last_zero(2 * v + 1, l, m); } return last_zero(2 * v + 2, m + 1, r); } } int main() { const unsigned long long INF = 1e16 + 239; int n; cin >> n; unsigned long long s[n]; int p[n]; t.assign(n * 4, INF); lazy.assign(n * 4, 0); for (int i = 0; i < n; i++) { cin >> s[i]; } build(s, 0, 0, n - 1); for (int i = 1; i <= n; i++) { int pos = last_zero(0, 0, n - 1); p[pos] = i; add(0, 0, n - 1, pos, pos, INF); add(0, 0, n - 1, pos + 1, n - 1, -i); } for (int i = 0; i < n; i++) { cout << p[i] << ' '; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int MAXN = 2e5 + 5; const int inf = 0x3f3f3f3f; long long s[MAXN]; struct node { int l, r; long long sum; } node[MAXN << 2]; void build(int l, int r, int num) { node[num].l = l; node[num].r = r; if (l == r) { node[num].sum = l; return; } int mid = (l + r) >> 1; build(l, mid, num << 1); build(mid + 1, r, num << 1 | 1); node[num].sum = node[num << 1].sum + node[num << 1 | 1].sum; } void updata(int pos, int num) { if (node[num].l == node[num].r) { node[num].sum = 0; return; } int mid = (node[num].l + node[num].r) >> 1; if (pos <= mid) updata(pos, num << 1); else updata(pos, num << 1 | 1); node[num].sum = node[num << 1 | 1].sum + node[num << 1].sum; } int query(long long val, int num) { if (node[num].l == node[num].r) { return node[num].l; } long long sum1 = node[num << 1].sum; if (sum1 <= val) return query(val - sum1, num << 1 | 1); else return query(val, num << 1); } int dp[MAXN]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i]; } build(1, n, 1); for (int i = n; i >= 1; i--) { int ans = query(s[i], 1); dp[i] = ans; updata(ans, 1); } for (int i = 1; i <= n; i++) { cout << dp[i] << " "; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; long long a[300000], b[300000], C[300000]; int lowbit(int x) { return x & (-x); } long long getSum(int x) { long long sum = 0; for (int i = x; i > 0; i -= lowbit(i)) sum += C[i]; return sum; } void add(int x, int val) { for (int i = x; i < n; i += lowbit(i)) C[i] += val; } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; add(i, i); } for (int i = n; i >= 1; i--) { int l = 1, r = n; int mid; while (l < r) { mid = (l + r) / 2; if (getSum(mid) > a[i]) r = mid; else l = mid + 1; } b[i] = r; add(r, -r); } for (int i = 1; i <= n; i++) { cout << b[i] << " "; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
/* Editorial Approach */ import java.util.*; import java.io.*; public class lp{ static PrintWriter out = new PrintWriter(System.out); static class node { long val; int in; node(long v,int i) { val=v; in =i; } node() { val=0;in=0;} } static long[] lazy; static node[] tree; static long max= 10000000000000000l; static void update(int si, int ss, int se, int us,int ue,long diff){ if (lazy[si] != 0) { tree[si].val += lazy[si]; if (ss != se) { lazy[si * 2 + 1] += lazy[si]; lazy[si * 2 + 2] += lazy[si]; } lazy[si] = 0; } if (ss > se || ss > ue || se < us) return; if (ss >= us && se <= ue) { tree[si].val += diff; if (ss != se) { lazy[si * 2 + 1] += diff; lazy[si * 2 + 2] += diff; } return; } int mid = (ss + se) / 2; update(si * 2 + 1, ss, mid, us, ue, diff); update(si * 2 + 2, mid + 1, se, us, ue, diff); // tree[si] = Math.min(tree[si * 2 + 1] , tree[si * 2 + 2]); if(tree[2*si+1].val< tree[2*si+2].val) { tree[si].val = tree[2*si+1].val; tree[si].in = tree[2*si+1].in; } else if(tree[2*si+1].val> tree[2*si+2].val) { tree[si].val = tree[2*si+2].val; tree[si].in = tree[2*si+2].in; } else{ tree[si].val = tree[2*si+1].val; tree[si].in = Math.max(tree[2*si+1].in ,tree[2*si+2].in); } } static node merge(node n1,node n2) { if(n1.val<n2.val) return n1; if(n2.val<n1.val) return n2; else { if(n1.in>n2.in) return n1; return n2; } } static node get(int l, int r, int ql, int qr, int pos){ if (lazy[pos] != 0) { tree[pos].val += lazy[pos]; if (l != r) { lazy[pos * 2 + 1] += lazy[pos]; lazy[pos * 2 + 2] += lazy[pos]; } lazy[pos] = 0; } if (l > r || l > qr || r < ql) return new node(max,-1); if (l >= ql && r <= qr) return tree[pos]; int mid = (l + r) / 2; node n1 = get(l, mid, ql, qr, 2 * pos + 1); node n2 = get(mid + 1, r, ql, qr, 2 * pos + 2); return merge(n1,n2); } static void constructSTUtil(long arr[], int l, int r, int pos){ if (l == r) { tree[pos] =new node(arr[l],l); return; } int mid = (l + r) / 2; constructSTUtil(arr, l, mid, 2*pos + 1); constructSTUtil(arr, mid + 1, r, 2*pos + 2); if(tree[2*pos+1].val< tree[2*pos+2].val) { tree[pos].val = tree[2*pos+1].val; tree[pos].in = tree[2*pos+1].in; } else if(tree[2*pos+1].val> tree[2*pos+2].val) { tree[pos].val = tree[2*pos+2].val; tree[pos].in = tree[2*pos+2].in; } else{ tree[pos].val = tree[2*pos+1].val; tree[pos].in = Math.max(tree[2*pos+1].in ,tree[2*pos+2].in); } } public static void main(String[] args){ int n = ni(); long a[] = al(n); tree = new node[4*n]; lazy = new long[4*n]; for(int i=0;i<4*n;i++) tree[i] = new node(); int ans[] = new int[n]; constructSTUtil(a,0,n-1,0); for(int i=1;i<=n;i++) { node n1 = get(0,n-1,0,n-1,0); // System.out.println(" in :"+n1.in+" val :"+n1.val+" I :"+i); ans[n1.in]=i; update(0,0,n-1,n1.in,n-1,-i); update(0,0,n-1,n1.in,n1.in,max); } for(int i=0;i<n;i++) p(ans[i]+" "); out.flush(); } static int[] ai(int n) // it will give in array of size n { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = ni(); return a; } static long[] al(int n) // it will give in array of size n { long a[] = new long[n]; for(int i=0;i<n;i++) a[i] = nl(); return a; } static void p(Object o) { out.print(o); } static void pn(Object o) { out.println(o); } static int abs(int x) { return x>0 ? x : -x; } static long gcd(long a,long b) { if(b%a==0) return a; return gcd(b%a,a); } static void subtract_1(char s[]) // it will subtract 1 from the given number. number should be positive { if(s[0]=='0') // number is zero return; int n = s.length,i=n-1; while(s[i]=='0') i--; s[i] = (char)((int)(s[i]-'0') + 47); for(int j=i+1;j<n;j++) s[j]='9'; } static long pow(long a,long b,long md) { long ans=1; while(b>0) { if(b%2==1) ans = (ans*a)%md; a = (a*a)%md; b = b/2; } return ans; } static long min(long a,long b){ return a<b ? a : b; } static long max(long a,long b){ return a>b ? a : b; } static boolean pal(String s) { int n = s.length(),i1=0,i2=n-1; while(i1<i2) { if(s.charAt(i1)!=s.charAt(i2)) return false; i1++; i2--; } return true; } static String rev(String r) { String s = ""; int i= r.length()-1; while(i>=0) { s=s+r.charAt(i); i--; } return s; } static FastReader sc=new FastReader(); static int ni(){ int x = sc.nextInt(); return(x); } static long nl(){ long x = sc.nextLong(); return(x); } static String n(){ String str = sc.next(); return(str); } static String ns(){ String str = sc.nextLine(); return(str); } static double nd(){ double d = sc.nextDouble(); return(d); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; struct node { long long w, change; int in; }; vector<long long> a; vector<node> t; const long long INF = 1e18; node combine(node a, node b, long long change) { a.w += a.change; b.w += b.change; node res; if (a.w != b.w) res = (a.w < b.w) ? a : b; else res = (a.in > b.in) ? a : b; res.change = change; return res; } void build(int v, int tl, int tr) { if (tl == tr) { t[v] = {a[tl], 0, tl}; return; } int tm = (tl + tr) / 2; build(2 * v, tl, tm); build(2 * v + 1, tm + 1, tr); t[v] = combine(t[2 * v], t[2 * v + 1], t[v].change); } void delete_el(int v, int tl, int tr, int pos) { if (tl == tr) { t[v] = {INF, 0, 0}; return; } int tm = (tl + tr) / 2; if (pos <= tm) delete_el(2 * v, tl, tm, pos); else delete_el(2 * v + 1, tm + 1, tr, pos); t[v] = combine(t[2 * v], t[2 * v + 1], t[v].change); } void update(int v, int tl, int tr, int l, int r, long long val) { if (l > r) return; if (l == tl && r == tr) { t[v].change -= val; return; } int tm = (tl + tr) / 2; update(2 * v, tl, tm, l, min(r, tm), val); update(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, val); t[v] = combine(t[2 * v], t[2 * v + 1], t[v].change); } int main() { int n; cin >> n; a.resize(n); t.resize(4 * n); for (int i = 0; i < n; i++) cin >> a[i]; build(1, 0, n - 1); vector<long long> ans(n); for (int i = 0; i < n; i++) { int in = t[1].in; ans[in] = i + 1; delete_el(1, 0, n - 1, in); update(1, 0, n - 1, in + 1, n - 1, 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 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy[8] = {1, -1, -1, 0, 1, -1, 0, 1}; long long tree[4 * 200005], lazy[4 * 200005], ans[4 * 200005], a[200005]; void build(long long int start, long long int last, long long int node) { if (start == last) { tree[node] = a[start]; ans[node] = start; return; } long long int mid = (start + last) >> 1, l = node << 1; build(start, mid, l), build(mid + 1, last, l + 1); long long t1 = tree[l], t2 = tree[l + 1]; if (t1 < t2) ans[node] = ans[l]; else ans[node] = ans[l + 1]; tree[node] = (tree[l] > tree[l + 1] ? tree[l + 1] : tree[l]); return; } void update(long long int start, long long int last, long long int l, long long int r, long long int node, long long val) { if (lazy[node]) { tree[node] += lazy[node]; if (start != last) { lazy[node << 1] += lazy[node]; lazy[(node << 1) + 1] += lazy[node]; } lazy[node] = 0; } if (start > r || last < l) return; if (l <= start && r >= last) { tree[node] += val; if (start != last) { lazy[node << 1] += val; lazy[(node << 1) + 1] += val; } return; } long long int mid = (start + last) >> 1; update(start, mid, l, r, node << 1, val); update(mid + 1, last, l, r, (node << 1) + 1, val); if (tree[node << 1] < tree[(node << 1) + 1]) ans[node] = ans[node << 1]; else ans[node] = ans[(node << 1) + 1]; tree[node] = (tree[node << 1] > tree[(node << 1) + 1] ? tree[(node << 1) + 1] : tree[node << 1]); return; } signed main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long int n; cin >> n; long long int n1 = n, ans1[n + 1], cnt = 1; for (long long int i = 1; i <= n; i++) cin >> a[i]; build(1, n, 1); while (n1--) { long long int idx = ans[1]; ans1[idx] = cnt; update(1, n, idx, idx, 1, 1000000000000); if (idx < n) update(1, n, idx + 1, n, 1, -cnt); cnt++; } for (long long int i = 1; i <= n; i++) cout << ans1[i] << " "; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
python3
import sys input = sys.stdin.readline nn = 18 bit=[0]*(2**nn+1) def addbit(i, x): while i <= 2**nn: bit[i] += x i += i & (-i) def getsum(i): ret = 0 while i != 0: ret += bit[i] i -= i&(-i) return ret def searchbit(x): l, sl = 0, 0 d = 2**(nn-1) while d: m = l + d sm = sl + bit[m] if sm <= x: l, sl = m, sm d //= 2 return l + 1 n = int(input()) l = list(map(int, input().split())) for i in range(1, n + 1): addbit(i, i) ans = [0 for _ in range(n)] for i in range(n - 1, -1, -1): a = searchbit(l[i]) addbit(a, -a) ans[i] = a print(*ans)
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 5; long long n, m, s[N], a[N], C[N], vis[N]; long long lowbit(long long x) { return (x & (-x)); } long long sum(long long x) { long long res = 0; while (x > 0) { res += C[x]; x -= lowbit(x); } return res; } void add(long long x, long long d) { d = d * x; while (x < N) { C[x] += d; x += lowbit(x); } } int main() { cin >> n; for (long long i = 1; i <= n; i++) { cin >> s[i], add(i, 1); } for (long long i = n; i >= 1; i--) { long long l = 1, r = n, ans = 0; while (l <= r) { long long mid = (r + l) >> 1, su = (sum(mid - 1)); if (su == s[i]) { if (vis[mid]) { l = mid + 1; continue; } ans = mid; add(mid, -1); break; } if (su < s[i]) { l = mid + 1; } else r = mid - 1; } a[i] = ans; vis[ans] = 1; } for (long long i = 1; i <= n; i++) { cout << a[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 " ] }
CORRECT
java
import java.io.*; import java.util.StringTokenizer; public class mainD { public static PrintWriter out = new PrintWriter(System.out); public static FastScanner enter = new FastScanner(System.in); public static long[] answer; public static void main(String[] args) throws IOException { int n = enter.nextInt(); long[] arr = new long[n+1]; long[] answer = new long[n+1]; int[] build = new int[n + 1]; for (int i = 1; i < n + 1; i++) { build[i] = i; } for (int i = 1; i < n+1; i++) { arr[i] = enter.nextLong(); } SegmentTree tree=new SegmentTree(build); tree.build(1, 1, n); for (int i = n; i >= 1; i--) { int element=find(arr[i], tree, n); tree.update(1, 1, n, element); answer[i]=element; } for (int i = 1; i <n+1 ; i++) { out.print(answer[i]+" "); } out.close(); } public static int find(long summ, SegmentTree tree, int n){ int l=1; int r=n; while(r-l>1){ int m=(r+l)/2; long ans=tree.query(1,1,n,1,m-1); if(ans<=summ){ l=m; } else{ //summ<ans r=m-1; } } if(tree.query(1,1, n,1, r-1)==summ) return r; return l; } static class SegmentTree { int[] arr; long[] t; public SegmentTree(int[] arr) { this.arr = arr; t = new long[arr.length * 4 + 1]; } public void build(int v, int vl, int vr) { if (vl == vr) { t[v] = arr[vl]; return; } int tmp = (vl + vr) / 2; build(2 * v, vl, tmp); build(2 * v + 1, tmp + 1, vr); t[v] = t[2 * v + 1] + t[2 * v]; } public long query(int v, int vl, int vr, int l, int r) { if (l > r) return 0; if (vl == l && vr == r) return t[v]; int tmp = (vl + vr) / 2; return query(2 * v, vl, tmp, l, Math.min(tmp, r)) + query(2 * v + 1, tmp + 1, vr, Math.max(l, tmp + 1), r); } public void update(int v, int vl, int vr, int pos) { if (vl == vr) { t[v] = 0; return; } int tmp = (vl + vr) / 2; if(pos<=tmp){ update(2*v, vl, tmp, pos); } else{ update(2*v+1, tmp+1, vr, pos); } t[v] = t[2 * v + 1] + t[2 * v]; } } static class FastScanner { BufferedReader br; StringTokenizer stok; FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } char nextChar() throws IOException { return (char) (br.read()); } String nextLine() throws IOException { return br.readLine(); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.util.*; import java.io.*; public class D { public static void main(String[] args) { FastScanner scanner = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int n = scanner.nextInt(); long[] s = new long[n]; int[] ans = new int[n]; SegTree sg= new SegTree(n, 0, n-1); for(int i = 0; i < n; i++) { s[i] = scanner.nextLong(); sg.increment(1, i,i, s[i]); } for(int i = 1; i <= n; i++) { int loc = sg.minimum(1, 0, n-1); sg.increment(1, loc, loc, Long.MAX_VALUE/3); ans[loc] = i; sg.increment(1, loc, n-1, -i); } for(int i = 0; i < n; i++) { if (i > 0) out.print(" "); out.print(ans[i]); } out.println(); out.flush(); } static class SegTree { int[] hi, lo; long[] min, delta; int sz; public SegTree (int ss, int l, int r) { hi = new int[4 * ss + 1]; lo = new int[4 * ss + 1]; min = new long[4 * ss + 1]; delta = new long[4 * ss + 1]; sz = ss; init(1, l, r); } void init(int cur, int l, int r) { hi[cur] = r; lo[cur] = l; if (l == r) { return; } int mid = (l + r)/2; init(cur * 2, l, mid); init(cur*2+1, mid + 1, r); } void increment(int cur, int l, int r, long amt) { if (l > hi[cur] || r < lo[cur]) return; if (r >= hi[cur] && l <= lo[cur]) { delta[cur] += amt; return; } prop(cur); increment(cur*2, l, r, amt); increment(cur*2 + 1, l, r, amt); update(cur); } void prop(int cur) { delta[cur * 2] += delta[cur]; delta[cur * 2 + 1] += delta[cur]; delta[cur] = 0; } void update(int cur) { min[cur] = Math.min(min[cur*2] + delta[cur*2], min[cur*2+1] + delta[cur*2+1]); } int minimum(int cur, int l, int r) { if (l > hi[cur] || r < lo[cur]) { return Integer.MAX_VALUE/2; } if (lo[cur] == hi[cur]) return min[cur] + delta[cur] == 0 ? lo[cur] : Integer.MAX_VALUE/2; prop(cur); if (delta[cur*2+1] + min[cur*2+1] == 0) { int min = minimum(cur*2+1, l, r); update(cur); return min; } int min = minimum(cur*2, l, r); update(cur); return min; } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner() { this(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 readNextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> bool mini(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool maxi(T &a, T b) { return a < b ? (a = b, true) : false; } const int N = 2e5 + 5; int n, ans[N]; long long bit[N], p[N]; void up(int i) { assert(i > 0); for (long long val = i; i <= n; i += i & -i) bit[i] += val; } long long get(int i) { long long res = 0; for (; i > 0; i -= i & -i) res += bit[i]; return res; } inline void sol() { cin >> n; for (int i = 0, _ = (n); i < _; i++) cin >> p[i]; for (int i = (int)(n)-1; i >= 0; --i) { long long l = 0, r = n - 1, mid; while (l < r) { mid = (l + r + 1) >> 1; if (mid * (mid + 1) / 2 > get(mid) + p[i]) r = mid - 1; else l = mid; } up(ans[i] = l + 1); } for (int i = 0, _ = (n); i < _; i++) cout << ans[i] << ' '; cout << "\n"; } signed main() { { ios_base::sync_with_stdio(false), cin.tie(NULL); }; cout.precision(10); cout << fixed; sol(); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Aman Kumar Singh */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DRestorePermutation solver = new DRestorePermutation(); solver.solve(1, in, out); out.close(); } static class DRestorePermutation { long INF = (long) 1e18 + 1; int MAXN = 200005; PrintWriter out; InputReader in; int n; public void solve(int testNumber, InputReader in, PrintWriter out) { this.out = out; this.in = in; n = ni(); long[] arr = new long[n]; int i = 0; Segment_Tree st = new Segment_Tree(); st.init(); for (i = 0; i < n; i++) { arr[i] = nl(); st.update_tree(1, 0, n - 1, i, i, arr[i]); } int[] ans = new int[n]; int curr = 1; for (i = 0; i < n; i++) { int lo = 0, hi = n - 1; while (lo <= hi) { int mid = (lo + hi) >> 1; if (st.query_tree(1, 0, n - 1, mid, n - 1) == 0) lo = mid + 1; else hi = mid - 1; } lo--; ans[lo] = curr; st.update_tree(1, 0, n - 1, lo, lo, (long) 1e18); st.update_tree(1, 0, n - 1, lo + 1, n - 1, -curr); curr++; } for (i = 0; i < n; i++) p(ans[i] + " "); } int ni() { return in.nextInt(); } long nl() { return in.nextLong(); } void p(Object o) { out.print(o); } class Segment_Tree { long[] tree = new long[4 * MAXN]; long[] lazy = new long[4 * MAXN]; void init() { Arrays.fill(tree, INF); Arrays.fill(lazy, INF); } void update_tree(int node, int a, int b, int i, int j, long value) { if (lazy[node] != INF) { if (tree[node] == INF) tree[node] = 0; tree[node] += lazy[node]; if (a != b) { if (lazy[node * 2] == INF) lazy[node * 2] = 0; if (lazy[node * 2 + 1] == INF) lazy[node * 2 + 1] = 0; lazy[node * 2] += lazy[node]; lazy[node * 2 + 1] += lazy[node]; } lazy[node] = INF; } if (a > b || a > j || b < i) return; if (a >= i && b <= j) { if (tree[node] == INF) tree[node] = 0; tree[node] += value; if (a != b) { if (lazy[node * 2] == INF) lazy[node * 2] = 0; if (lazy[node * 2 + 1] == INF) lazy[node * 2 + 1] = 0; lazy[node * 2] += value; lazy[node * 2 + 1] += value; } return; } update_tree(node * 2, a, (a + b) / 2, i, j, value); update_tree(1 + node * 2, 1 + (a + b) / 2, b, i, j, value); tree[node] = Math.min(tree[node * 2], tree[node * 2 + 1]); } long query_tree(int node, int a, int b, int i, int j) { if (a > b || a > j || b < i) return INF; if (lazy[node] != INF) { if (tree[node] == INF) tree[node] = 0; tree[node] += lazy[node]; if (a != b) { if (lazy[node * 2] == INF) lazy[node * 2] = 0; if (lazy[node * 2 + 1] == INF) lazy[node * 2 + 1] = 0; lazy[node * 2] += lazy[node]; lazy[node * 2 + 1] += lazy[node]; } lazy[node] = INF; } if (a >= i && b <= j) return tree[node]; long q1 = query_tree(node * 2, a, (a + b) / 2, i, j); long q2 = query_tree(1 + node * 2, 1 + (a + b) / 2, b, i, j); long res = Math.min(q1, q2); return res; } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new UnknownError(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, lo[(long long)(2e5 + 5) * 4], hi[(long long)(2e5 + 5) * 4], ans[(long long)(2e5 + 5)]; long long base[(long long)(2e5 + 5)], delta[(long long)(2e5 + 5) * 4]; pair<long long, int> mn[(long long)(2e5 + 5) * 4]; void update(int i); void ini(int i, int a, int b) { lo[i] = a; hi[i] = b; if (a == b) { mn[i].first = base[a]; mn[i].second = a; return; } int mid = (a + b) / 2; ini(2 * i, a, mid); ini(2 * i + 1, mid + 1, b); update(i); } void update(int i) { if (mn[i * 2].first + delta[i * 2] == mn[i * 2 + 1].first + delta[i * 2 + 1]) mn[i] = mn[i * 2 + 1], mn[i].first += delta[i * 2 + 1]; else if (mn[i * 2].first + delta[i * 2] < mn[i * 2 + 1].first + delta[i * 2 + 1]) mn[i] = mn[i * 2], mn[i].first += delta[i * 2]; else mn[i] = mn[i * 2 + 1], mn[i].first += delta[i * 2 + 1]; } void prop(int i) { delta[i * 2] += delta[i]; delta[i * 2 + 1] += delta[i]; delta[i] = 0; } void inc(int i, int a, int b, long long val) { if (lo[i] >= a && hi[i] <= b) { delta[i] += val; return; } if (lo[i] > b || hi[i] < a) return; prop(i); inc(i * 2, a, b, val); inc(i * 2 + 1, a, b, val); update(i); } void change(int i, int loc) { if (lo[i] == loc && hi[i] == loc) { mn[i].first = 1e18, mn[i].second = -1; delta[i] = 1e18; return; } if (lo[i] > loc || hi[i] < loc) return; prop(i); change(i * 2, loc); change(i * 2 + 1, loc); update(i); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n; for (int i = 1; i <= n; i++) cin >> base[i]; ini(1, 1, n); for (int i = 1; i <= n; i++) { pair<long long, int> loc = mn[1]; change(1, loc.second); inc(1, loc.second + 1, n, -1 * i); ans[loc.second] = i; } for (int i = 1; i <= n; i++) { cout << ans[i]; if (i != n) cout << " "; } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.DataInputStream; import java.io.IOException; import java.io.PrintWriter; public class P1208D7 { public static void main(String[] args) throws IOException { InputReader2 ir = new InputReader2(); PrintWriter pw = new PrintWriter(System.out); int n = ir.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = ir.nextLong(); } Fenwick f = new Fenwick(n); for (int i = 1; i <= n; i++) { f.add(i - 1, i); } int[] list = new int[n]; for (int i = n - 1; i >= 0; i--) { int ind = f.indexWithGivenCumFreq(a[i]); f.add(ind, -(ind + 1)); list[i] = ind + 1; } for (int i = 0; i < n; i++) { pw.print(list[i] + " "); } pw.close(); } static class Fenwick { public final long[] bit; public final int size; public Fenwick(int size) { bit = new long[size + 1]; this.size = size + 1; } public void add(int i, long delta) { for (++i; i < size; i += (i & -i)) { bit[i] += delta; } } public int indexWithGivenCumFreq(long v) { int i = 0, n = size; for (int b = Integer.highestOneBit(n); b != 0; b >>= 1) { if ((i | b) < n && bit[i | b] <= v) { i |= b; v -= bit[i]; } } return i; } } private static class InputReader2 { final private int BUFFER_SIZE = 1 << 16; private final DataInputStream dis; private final byte[] buffer; private int bufferPointer, bytesRead; public InputReader2() { dis = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return ret; } private long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return ret; } private void fillBuffer() throws IOException { bytesRead = dis.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.util.*; public class RestorePermutation { static class FenwickTree { int n; long[] BIT; FenwickTree(int n) { this.n = n; BIT = new long[n + 1]; } void update(int i, long x) { for (; i <= n; i += i&-i) { BIT[i] += x; } } int searchNum(long prefSum) { int num = 0; long sum = 0; for (int i = 19; i >= 0; --i) { if (num + (1 << i) <= n && sum + BIT[num + (1 << i)] <= prefSum) { num += (1 << i); sum += BIT[num]; } } return num + 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(); long[] s = new long[n + 1]; FenwickTree ft = new FenwickTree(n); for (int i = 1; i <= n; ++i) { ft.update(i, i); s[i] = in.nextLong(); } int[] p = new int[n + 1]; for (int i = n; i >= 1; --i) { p[i] = ft.searchNum(s[i]); ft.update(p[i], -p[i]); } for (int i = 1; i <= n; ++i) out.print(p[i] + " "); out.println(); out.close(); } private static class FastReader { BufferedReader br; StringTokenizer st; FastReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } Integer nextInt() { return Integer.parseInt(next()); } Long nextLong() { return Long.parseLong(next()); } Double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } String nextLine() { String x = ""; try { x = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return x; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long int n; long long int tree[200001 * 10]; long int a[200002]; void build(long int node, long int start, long int end) { if (start == end) { tree[node] = a[start]; } else { long int mid = (start + end) / 2; build(2 * node + 1, start, mid); build(2 * node + 2, mid + 1, end); tree[node] = tree[2 * node + 1] + tree[2 * node + 2]; } } void update(long int node, long int start, long int end, long int ind) { if (start == end) tree[node] = a[ind]; else { long int mid = (start + end) / 2; if (start <= ind && ind <= mid) { update(2 * node + 1, start, mid, ind); } else { update(2 * node + 2, mid + 1, end, ind); } tree[node] = tree[2 * node + 1] + tree[2 * node + 2]; } } long long int query(long int node, long int start, long int end, long int l, long int r) { if (r < start || end < l) { return 0; } if (l <= start && end <= r) { return tree[node]; } long int mid = (start + end) / 2; long long int p1 = query(2 * node + 1, start, mid, l, r); long long int p2 = query(2 * node + 2, mid + 1, end, l, r); return (p1 + p2); } long int idx[200002]; long int root(long int x) { while (idx[x] != x) { idx[x] = idx[idx[x]]; x = idx[x]; } return x; } int main() { cin >> n; long int ans = 0; for (long int i = 0; i <= n + 1; i++) a[i] = i, idx[i] = i; build(0, 0, n); long long int b[n]; for (long int i = 0; i < n; i++) cin >> b[i]; vector<long int> x; for (long int i = n - 1; i >= 0; i--) { long int l = 0, r = n; long int ans = -1; while (l <= r) { long int mid = (l + r) / 2; long long int temp1 = query(0, 0, n, 0, mid); if (temp1 == b[i]) { ans = mid + 1; break; } else if (temp1 < b[i]) { l = mid + 1; } else r = mid - 1; } ans = root(ans); x.push_back(ans); a[ans] = 0; update(0, 0, n, ans); idx[root(ans)] = root(ans + 1); } for (long int i = 0; i < x.size(); i++) cout << x[x.size() - 1 - i] << " "; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long Nmax = 2e6 + 5; long long v[Nmax]; long long ans[Nmax]; long long lazy[4 * Nmax]; pair<long long, long long> all[4 * Nmax]; long long p, q, val; void add(long long &first, long long val) { if (first != LLONG_MAX) first += val; } void propagate(long long node) { if (lazy[node]) { add(lazy[(node << 1)], lazy[node]); add(lazy[((node << 1) + 1)], lazy[node]); add(all[(node << 1)].first, lazy[node]); add(all[((node << 1) + 1)].first, lazy[node]); lazy[node] = 0; } } void build(long long lo, long long hi, long long node) { if (lo == hi) { all[node] = make_pair(v[lo], -lo); } else { long long mid = (lo + hi) >> 1; build(lo, mid, (node << 1)); build(mid + 1, hi, ((node << 1) + 1)); all[node] = min(all[(node << 1)], all[((node << 1) + 1)]); } } void update(long long lo, long long hi, long long node) { propagate(node); if (p <= lo and hi <= q) { lazy[node] += val; all[node].first += val; } else { long long mid = (lo + hi) >> 1; if (p <= mid) update(lo, mid, (node << 1)); if (mid < q) update(mid + 1, hi, ((node << 1) + 1)); all[node] = min(all[(node << 1)], all[((node << 1) + 1)]); } } int main() { ios_base ::sync_with_stdio(false); cin.tie(nullptr); long long n; cin >> n; for (long long i = 1; i <= n; ++i) { cin >> v[i]; } build(1, n, 1); for (long long pos, i = 1; i <= n; ++i) { pos = -all[1].second; ans[pos] = i; p = q = pos; val = LLONG_MAX; update(1, n, 1); p = pos + 1; q = n; val = -i; if (p <= q) update(1, n, 1); } for (long long i = 1; i <= n; ++i) cout << ans[i] << ' '; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; int z[200020]; long long a[200020]; long long c[200020]; void R(int x, int y) { for (; x <= n; x += x & -x) { c[x] += y; } } int A(long long x) { int re = 0; for (int i = 1 << 20; i > 0; i >>= 1) { if (re + i <= n && c[re + i] <= x) { x -= c[re += i]; } } return re + 1; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); R(i, i); } for (int i = n; i > 0; i--) { z[i] = A(a[i]); R(z[i], -z[i]); } for (int i = 1; i <= n; i++) { printf("%d%c", z[i], i == n ? '\n' : ' '); } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
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] = 1ll * 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, long long 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(long long 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, -a[i], 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 " ] }
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 int find(int v, int vl, int vr){ if(vl == vr - 1){ return v; } push(v); min[v] = Math.min(get_val(v * 2), get_val(v * 2 + 1)); if(get_val(v * 2 + 1) == 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; } 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(get_val(ver * 2), get_val(ver * 2 + 1)); } add(1, 0, new_n, node, new_n, now); ++now; } for(int i = 0; i < n; ++i){ writer.print(ans[i] + " "); } writer.close(); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class cf1208d { public static void main(String[] args) throws IOException { int n = ri(), ans[] = new int[n]; long a[] = rla(n); sgt sgt = new sgt(a, Long::sum, 0); for (int i = 0; i < n; ++i) { int ind = sgt.qry(); // prln(ind); // flush(); ans[ind] = i + 1; sgt.upd(ind + 1, n, -i - 1); sgt.set(ind, LMAX >> 1); } prln(ans); close(); } @FunctionalInterface interface LongOperator { long merge(long a, long b); } static class sgt { LongOperator upd_op, qry_op; int n; long sgt[], lazy[], id; sgt(int sz, LongOperator operator, long identity) { n = sz; sgt = new long[4 * n + 5]; upd_op = qry_op = operator; id = identity; } sgt(int sz, LongOperator update_operator, LongOperator query_operator, long identity) { n = sz; sgt = new long[4 * n + 5]; upd_op = update_operator; qry_op = query_operator; id = identity; } sgt(long[] a, LongOperator operator, long identity) { n = a.length; sgt = new long[4 * n + 5]; lazy = new long[4 * n + 5]; upd_op = qry_op = operator; id = identity; build(1, a, 0, n - 1); } sgt(long[] a, LongOperator update_operator, LongOperator query_operator, long identity) { n = a.length; sgt = new long[4 * n + 5]; upd_op = update_operator; qry_op = query_operator; id = identity; build(1, a, 0, n - 1); } void set(int i, long x) { set(1, i, x, 0, n - 1); } void upd(int i, int j, long x) { upd(1, i, j - 1, x, 0, n - 1); } int qry() { return qry(1, 0, n - 1); } void set(int node, int i, long x, int l, int r) { push(node, l, r); if (l == r) { sgt[node] = x; } else { int m = l + (r - l) / 2; if (i <= m) { set(node << 1, i, x, l, m); } else { set(node << 1 | 1, i, x, m + 1, r); } pull(node); } } void upd(int node, int i, int j, long x, int l, int r) { push(node, l, r); if (r < i || j < l) { return; } else if (i <= l && r <= j) { lazy[node] = x; push(node, l, r); } else { int m = l + (r - l) / 2; upd(node << 1, i, j, x, l, m); upd(node << 1 | 1, i, j, x, m + 1, r); pull(node); } } int qry(int node, int l, int r) { push(node, l, r); if (l == r) { // prln(node, l, r, sgt[node]); return sgt[node] == 0 ? l : -1; } else { int m = l + (r - l) / 2; push(node << 1, l, m); push(node << 1 | 1, m + 1, r); if (sgt[node << 1 | 1] == 0) { return qry(node << 1 | 1, m + 1, r); } else { return qry(node << 1, l, m); } } } void build(int node, long[] a, int l, int r) { if (l == r) { sgt[node] = a[l]; } else { int m = l + (r - l) / 2; build(node << 1, a, l, m); build(node << 1 | 1, a, m + 1, r); pull(node); } } void push(int node, int l, int r) { sgt[node] += lazy[node]; if (l != r) { lazy[node << 1] += lazy[node]; lazy[node << 1 | 1] += lazy[node]; } lazy[node] = 0; } void pull(int node) { sgt[node] = min(sgt[node << 1], sgt[node << 1 | 1]); } } static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random __rand = new Random(); // references // IBIG = 1e9 + 7 // IMAX ~= 2e9 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final int IMIN = -2147483648; static final long LMAX = 9223372036854775807L; static final long LMIN = -9223372036854775808L; // math util static int minof(int a, int b, int c) {return min(a, min(b, c));} static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;} static long minof(long a, long b, long c) {return min(a, min(b, c));} static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;} static int maxof(int a, int b, int c) {return max(a, max(b, c));} static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;} static long maxof(long a, long b, long c) {return max(a, max(b, c));} static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;} static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static int fli(double d) {return (int) d;} static int cei(double d) {return (int) ceil(d);} static long fll(double d) {return (long) d;} static long cel(double d) {return (long) ceil(d);} static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);} static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);} static int lcm(int a, int b) {return a * b / gcf(a, b);} static long lcm(long a, long b) {return a * b / gcf(a, b);} static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;} static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);} // array util static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void rsort(int[] a) {shuffle(a); sort(a);} static void rsort(long[] a) {shuffle(a); sort(a);} static void rsort(double[] a) {shuffle(a); sort(a);} static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} // graph util static List<List<Integer>> g(int n) {List<List<Integer>> g = new ArrayList<>(); for (int i = 0; i < n; ++i) g.add(new ArrayList<>()); return g;} static List<Set<Integer>> sg(int n) {List<Set<Integer>> g = new ArrayList<>(); for (int i = 0; i < n; ++i) g.add(new HashSet<>()); return g;} static void c(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v); g.get(v).add(u);} static void cto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v);} static void dc(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v); g.get(v).remove(u);} static void dcto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v);} // input static void r() throws IOException {input = new StringTokenizer(rline());} static int ri() throws IOException {return Integer.parseInt(rline());} static long rl() throws IOException {return Long.parseLong(rline());} static double rd() throws IOException {return Double.parseDouble(rline());} static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;} static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;} static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;} static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;} static char[] rcha() throws IOException {return rline().toCharArray();} static String rline() throws IOException {return __in.readLine();} static String n() {return input.nextToken();} static int rni() throws IOException {r(); return ni();} static int ni() {return Integer.parseInt(n());} static long rnl() throws IOException {r(); return nl();} static long nl() {return Long.parseLong(n());} static double rnd() throws IOException {r(); return nd();} static double nd() {return Double.parseDouble(n());} static List<List<Integer>> rg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for (int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;} static void rg(List<List<Integer>> g, int m) throws IOException {for (int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);} static List<List<Integer>> rdg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for (int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;} static void rdg(List<List<Integer>> g, int m) throws IOException {for (int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);} static List<Set<Integer>> rsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for (int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;} static void rsg(List<Set<Integer>> g, int m) throws IOException {for (int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);} static List<Set<Integer>> rdsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for (int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;} static void rdsg(List<Set<Integer>> g, int m) throws IOException {for (int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);} // output static void pr(int i) {__out.print(i);} static void prln(int i) {__out.println(i);} static void pr(long l) {__out.print(l);} static void prln(long l) {__out.println(l);} static void pr(double d) {__out.print(d);} static void prln(double d) {__out.println(d);} static void pr(char c) {__out.print(c);} static void prln(char c) {__out.println(c);} static void pr(char[] s) {__out.print(new String(s));} static void prln(char[] s) {__out.println(new String(s));} static void pr(String s) {__out.print(s);} static void prln(String s) {__out.println(s);} static void pr(Object o) {__out.print(o);} static void prln(Object o) {__out.println(o);} static void prln() {__out.println();} static void pryes() {prln("yes");} static void pry() {prln("Yes");} static void prY() {prln("YES");} static void prno() {prln("no");} static void prn() {prln("No");} static void prN() {prln("NO");} static void pryesno(boolean b) {prln(b ? "yes" : "no");}; static void pryn(boolean b) {prln(b ? "Yes" : "No");} static void prYN(boolean b) {prln(b ? "YES" : "NO");} static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();} static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();} static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();} static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();} static void h() {prln("hlfd"); flush();} static void flush() {__out.flush();} static void close() {__out.close();}}
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> const int MAXN = 2e5 + 10; const long long inf = 1e17 + 10; using namespace std; struct Seg { long long v[MAXN * 4], lazy[MAXN * 4]; int m(int l, int r) { return (l + r) / 2; } void updPoint(int pos, int l, int r, int x, long long val) { if (l == r) { v[pos] = val; return; } if (x <= m(l, r)) updPoint(pos * 2, l, m(l, r), x, val); else updPoint(pos * 2 + 1, m(l, r) + 1, r, x, val); v[pos] = min(v[pos * 2], v[pos * 2 + 1]); } void refresh(int pos, int l, int r) { long long k = lazy[pos]; lazy[pos] = 0; v[pos] -= k; if (l == r) return; for (int i = 0; i < 2; i++) lazy[pos * 2 + i] += k; } void updInterval(int pos, int l, int r, int beg, int en, long long val) { refresh(pos, l, r); if (l > en || r < beg) return; if (l >= beg && r <= en) { lazy[pos] += val; refresh(pos, l, r); return; } updInterval(pos * 2, l, m(l, r), beg, en, val); updInterval(pos * 2 + 1, m(l, r) + 1, r, beg, en, val); v[pos] = min(v[pos * 2], v[pos * 2 + 1]); } int buscab(int pos, int l, int r) { refresh(pos, l, r); if (l == r) return l; refresh(pos * 2, l, m(l, r)); refresh(pos * 2 + 1, m(l, r) + 1, r); if (v[pos * 2] > 0) return buscab(pos * 2 + 1, m(l, r) + 1, r); return buscab(pos * 2, l, m(l, r)); } }; int n; long long vet[MAXN]; int seq[MAXN]; Seg seg; int main() { scanf("%d", &n); for (int i = 1; i < n + 1; i++) scanf("%lld", &vet[i]); reverse(vet + 1, vet + 1 + n); for (int i = 1; i < n + 1; i++) seg.updPoint(1, 1, n, i, vet[i]); for (int i = 1; i < n + 1; i++) { int t = seg.buscab(1, 1, n); seg.updInterval(1, 1, n, 1, t - 1, i); seg.updInterval(1, 1, n, t, t, -inf); seq[n - t + 1] = i; } for (int i = 1; i < n + 1; i++) printf("%d ", seq[i]); printf("\n"); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename Tp> void read(Tp &x) { x = 0; long long f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') { f = -1; } c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } x *= f; } long long n, m; long long s[200005], a[200005]; long long st[200005 << 2], ps[200005 << 2], laz[200005 << 2]; void pushup(long long x) { st[x] = min(st[(x << 1)], st[((x << 1) | 1)]); if (st[x] == st[((x << 1) | 1)]) ps[x] = ps[((x << 1) | 1)]; else ps[x] = ps[(x << 1)]; } void build(long long x, long long l, long long r) { if (l == r) { st[x] = s[l]; ps[x] = l; return; } build((x << 1), l, ((l + r) >> 1)); build(((x << 1) | 1), ((l + r) >> 1) + 1, r); pushup(x); } void pushdown(long long x) { if (laz[x]) { laz[(x << 1)] += laz[x]; laz[((x << 1) | 1)] += laz[x]; st[(x << 1)] += laz[x]; st[((x << 1) | 1)] += laz[x]; laz[x] = 0; } } void upd(long long x, long long l, long long r, long long L, long long R, long long v) { if (l > R || r < L) return; if (l >= L && r <= R) { st[x] += v; laz[x] += v; return; } pushdown(x); upd((x << 1), l, ((l + r) >> 1), L, R, v); upd(((x << 1) | 1), ((l + r) >> 1) + 1, r, L, R, v); pushup(x); } signed main() { read(n); for (long long i = 1; i <= n; i++) read(s[i]); build(1, 1, n); long long pos; for (long long i = 1; i <= n; i++) { pos = ps[1]; a[pos] = i; upd(1, 1, n, pos, pos, 0x3f3f3f3f3f3f3fll); upd(1, 1, n, pos + 1, n, -i); } for (long long i = 1; i <= n; i++) printf("%I64d ", a[i]); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const long long INF = 1e16 + 239; const int MAXN = 1e6 + 239; long long a[MAXN]; namespace SegmentTree { int n; long long t[4 * MAXN]; long long mod[4 * MAXN]; void pull(int v) { t[v] = min(t[2 * v + 1], t[2 * v + 2]); } void apply(int v, long long val) { t[v] += val; mod[v] += val; } void push(int v) { if (mod[v] != 0) { apply(2 * v + 1, mod[v]); apply(2 * v + 2, mod[v]); mod[v] = 0; } } void build(int v, int l, int r) { if (l + 1 == r) { t[v] = a[l]; } else { int m = (r + l) >> 1; build(2 * v + 1, l, m); build(2 * v + 2, m, r); pull(v); } } void add(int v, int l, int r, int ql, int qr, long long val) { if (r <= ql || qr <= l) { return; } else if (ql <= l && r <= qr) { apply(v, val); } else { push(v); int m = (r + l) >> 1; add(2 * v + 1, l, m, ql, qr, val); add(2 * v + 2, m, r, ql, qr, val); pull(v); } } int go_down(int v, int l, int r) { if (l + 1 == r) { return l; } else { push(v); int m = (r + l) >> 1; int res = -1; if (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(int n_) { n = n_; build(0, 0, n); } void add(int l, int r, long long val) { add(0, 0, n, l, r, val); } void add(int pos, long long val) { add(0, 0, n, pos, pos + 1, val); } int last_zero() { return go_down(0, 0, n); } } // namespace SegmentTree signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } SegmentTree::init(n); vector<int> ans(n, -1); for (int i = 1; i <= n; i++) { int pos = SegmentTree::last_zero(); ans[pos] = i; SegmentTree::add(pos, INF); SegmentTree::add(pos + 1, n, -i); } for (auto t : ans) { cout << t << ' '; } 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 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, ar[200005], br[200005], up[800555], dr[100]; 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; } 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 " ] }
CORRECT
java
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeSet; public class C { public static void main(String[] args) { FastReader scan = new FastReader(); PrintWriter out = new PrintWriter(System.out); Task solver = new Task(); int t = 1; while(t-->0) solver.solve(1, scan, out); out.close(); } static class Task { static long[] a; static long INF = Long.MAX_VALUE/100; public void solve(int testNumber, FastReader scan, PrintWriter out) { int n = scan.nextInt(); a = new long[n]; for(int i = 0; i < n; i++) a[i] = scan.nextLong(); SegmentTree tree = new SegmentTree(0, n-1); int[] res = new int[n]; for(int i = 1; i <= n; i++) { int get = tree.walk(); res[get] = i; if(get != n-1) tree.update(get+1, n-1, -i); tree.update(get, get, INF); } for(int i : res) out.print(i + " "); } static class SegmentTree { int s, e; long modify; long minVal; SegmentTree left, right; public SegmentTree(int ss, int ee) { s = ss; e = ee; modify = 0; if(s == e) { minVal = a[s]; left = null; right = null; } else { int mid = (s+e)/2; left = new SegmentTree(s, mid); right = new SegmentTree(mid+1, e); minVal = Math.min(left.minVal, right.minVal); } } public void update(int start, int end, long val) { push(); if(s > end || e < start) return; if(s >= start && e <= end) { modify += val; return; } if(s != e) { left.update(start, end, val); right.update(start, end, val); minVal = Math.min(left.minVal+left.modify, right.minVal+right.modify); } } public int walk() { push(); if(s == e) return s; if(right.minVal + right.modify == 0) { return right.walk(); } if(left.minVal + left.modify == 0) { return left.walk(); } return -1; } public void push() { minVal += modify; if(s != e) { left.modify += modify; right.modify += modify; } modify = 0; } } } static void shuffle(int[] a) { Random get = new Random(); for(int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } } static void shuffle(long[] a) { Random get = new Random(); for(int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long bit[200005]; void add(long long idx, long long val) { for (int i = idx; i < 200005; i += (i & (-i))) { bit[i] += val; } } long long query(long long i) { long long ans = 0; for (; i > 0; i -= (i & (-i))) ans += bit[i]; return ans; } long long b[200005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, i, j, k, m; cin >> n; long long a[n]; for (i = 0; i < n; i++) cin >> a[i]; set<long long> s, s1; unordered_map<long long, long long> mp; long long sm = 0; long long sum[n + 5]; for (i = 1; i <= n; i++) { sm += i; mp[sm] = i; s.insert(i); s1.insert(sm); sum[i] = sm; } for (i = 1; i <= n; i++) { add(i, i); } for (i = n - 1; i >= 0; i--) { long long f = 1, l = n; long long mid; long long anss = 0; while (f <= l) { mid = (f + l) / 2; if (query(mid) > a[i]) { l = mid - 1; } else { f = mid + 1; anss = mid; } } anss++; b[i] = anss; add(anss, -anss); } for (i = 0; i < n; i++) cout << b[i] << " "; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.util.*; public class Test{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); BIT bit=new BIT(n); long[] s=new long[n+1],ori=new long[n+1]; for(int i=1;i<=n;++i){ s[i]=sc.nextLong(); } for(int i=n;i>0;i--){ int l=0,r=n; while(l+1<r){ int mid=(l+r)>>1; if(bit.sum(mid)>s[i]){ r=mid; }else{ l=mid; } } ori[i]=r; bit.add(r,-r); } for(int i=1;i<=n;i++){ System.out.printf("%d ",ori[i]); } System.out.printf("\n"); sc.close(); } } class BIT{ public long[] bit,val; public int n; public long sum(int i){ long res=0; while(i>0){ res+=bit[i]; i-=i&-i; } return res; } public void add(int i,int x){ val[i]+=i; while(i<=n){ bit[i]+=x; i+=i&-i; } } public BIT(int n){ this.n=n; this.bit=new long[n+1]; this.val=new long[n+1]; for(int i=1;i<=n;++i){ add(i,i); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MM = 10000000000000001; int n; long long a[200005]; int rs[200005]; long long lazy[4 * 200005]; pair<long long, int> it[4 * 200005]; pair<long long, int> minp(pair<long long, int> A, pair<long long, int> B) { if (A.first < B.first) return A; if (A.first > B.first) return B; if (A.second > B.second) return A; return B; } void build(int x, int l, int r) { if (l == r) { it[x] = make_pair(a[l], l); return; } int mid = (l + r) / 2; build(2 * x, l, mid); build(2 * x + 1, mid + 1, r); it[x] = minp(it[2 * x], it[2 * x + 1]); } void lazy_upd(int x, int l, int r) { it[x].first += lazy[x]; if (l != r) { lazy[2 * x] += lazy[x]; lazy[2 * x + 1] += lazy[x]; } lazy[x] = 0; } void update(int x, int l, int r, int u, int v, long long val) { lazy_upd(x, l, r); if ((r < u) || (v < l)) return; if ((u <= l) && (r <= v)) { lazy[x] += val; lazy_upd(x, l, r); return; } int mid = (l + r) / 2; update(2 * x, l, mid, u, v, val); update(2 * x + 1, mid + 1, r, u, v, val); it[x] = minp(it[2 * x], it[2 * x + 1]); } int main() { ios::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; build(1, 1, n); for (int i = 1; i <= n; i++) { pair<long long, int> p = it[1]; int id = p.second; rs[id] = i; update(1, 1, n, id, id, MM); update(1, 1, n, id + 1, n, -i); } for (int i = 1; i <= n; i++) cout << rs[i] << ' '; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import static java.lang.Math.*; public class Solution{ static InputReader sc; static PrintWriter wc; static long[] tree,lazy,arr; static void build(int l,int r,int i){ if(l==r){ tree[i]=arr[l]; return; } int m=(l+r)>>1; build(l,m,2*i+1); build(m+1,r,2*i+2); tree[i]=Math.min(tree[2*i+1],tree[2*i+2]); } static void update(int i,long val){ tree[i]+=val; lazy[i]+=val; } static void lazy_update(int ql,int qr,int l,int r,int i,long val){ if(l>qr||r<ql){ return; } else{ if(l>=ql&&r<=qr){ tree[i]+=val; lazy[i]+=val; } else{ int m=(l+r)>>1; lazy_update(ql,qr,l,m,2*i+1,val); lazy_update(ql,qr,m+1,r,2*i+2,val); tree[i]=Math.min(tree[2*i+1],tree[2*i+2]); } } } static int getMin(int l,int r,int i){ if(l==r){ return l; } else{ if(lazy[i]!=0){ update(2*i+1,lazy[i]); update(2*i+2,lazy[i]); lazy[i]=0; } int m=(l+r)>>1,res; if(tree[2*i+2]==0){ res = getMin(m+1,r,2*i+2); } else res = getMin(l,m,2*i+1); tree[i]=Math.min(tree[2*i+1],tree[2*i+2]); return res; } } static void print(int a[]){ for(int i=0;i<a.length;i++){ wc.print(a[i]+" "); } wc.println(); //wc.println(a[0]); } public static void main(String[] args) { sc = new InputReader(System.in); wc = new PrintWriter(System.out); int n=sc.nextInt(); int i,tlen,r[]=new int[n],j; long inf=(long) 2e11; tlen=(int)Math.ceil(Math.log(n)/Math.log(2))+1; tlen=(1<<tlen)-1; arr=new long[n]; tree=new long[tlen]; lazy = new long[tlen]; for(i=0;i<n;i++){ arr[i]=sc.nextLong(); } build(0,n-1,0); for(i=1;i<=n;i++){ j=getMin(0,n-1,0); r[j]=i; lazy_update(j,j,0,n-1,0,inf); lazy_update(j+1,n-1,0,n-1,0,-i); } print(r); //wc.println(r[0]); wc.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; long long s[200066]; long long c[200066]; long long ans[200066]; long long lowbit(long long x) { return x & (-x); } long long q(long long x) { long long ans = 0; while (x > 0) { ans += c[x]; x -= lowbit(x); } return ans; } void update(long long x, long long v) { while (x <= n) { c[x] += v; x += lowbit(x); } } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i]; update(i, i); } for (int i = n; i >= 1; i--) { long long l = 1; long long r = n; long long mid; while (l < r) { mid = (l + r + 1) / 2; if (q(mid - 1) <= s[i]) { l = mid; } else r = mid - 1; } ans[i] = l; update(l, -l); } for (int i = 1; i <= n; i++) { if (i != n) cout << ans[i] << " "; else cout << ans[i] << 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 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = (int)2E5; long long arr[MAXN + 1]; long long sum[MAXN + 1]; long long bit[MAXN + 1]; int ans[MAXN + 1]; long long query(int x) { long long ans = 0; while (x > 0) { ans += bit[x]; x -= x & -x; } return ans; } void increment(int x, int amt, int n) { while (x <= n) { bit[x] += amt; x += x & -x; } } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { sum[i] = sum[i - 1] + i; } for (int i = 1; i <= n; i++) { cin >> arr[i]; } for (int i = n; i >= 1; i--) { int temp = 1; for (int jump = n / 2 + 1; jump > 0; jump /= 2) { while (temp + jump <= n && sum[temp + jump - 1] - query(temp + jump - 1) <= arr[i]) temp += jump; } ans[i] = temp; increment(temp, temp, n); } for (int i = 1; i <= n; i++) cout << ans[i] << " "; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MX = 200005; vector<int> v; long long n, a[MX], w[MX], tree[MX * 4 + 5]; long long con(int num, int nodeL, int nodeR) { if (nodeL == nodeR) return tree[num] = w[nodeL]; long long mid = (nodeL + nodeR) / 2; return tree[num] = con(num * 2, nodeL, mid) + con(num * 2 + 1, mid + 1, nodeR); } long long sum(int L, int R, int num, int nodeL, int nodeR) { if (nodeR < L || nodeL > R) return 0; if (nodeL >= L && nodeR <= R) return tree[num]; int mid = (nodeL + nodeR) / 2; return sum(L, R, num * 2, nodeL, mid) + sum(L, R, num * 2 + 1, mid + 1, nodeR); } long long upd(int idx, int num, int nodeL, int nodeR) { if (nodeL > idx || nodeR < idx) return 0; if (nodeL == nodeR) return tree[num] = 0; int mid = (nodeL + nodeR) / 2; upd(idx, num * 2, nodeL, mid); upd(idx, num * 2 + 1, mid + 1, nodeR); if (nodeL <= idx && nodeR >= idx) { tree[num] = tree[num * 2] + tree[num * 2 + 1]; } return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { w[i] = i + 1; } for (int i = 0; i < n; i++) { cin >> a[i]; } con(1, 0, n - 1); for (int i = n - 1; i >= 0; i--) { int s = 0, e = n - 1; while (s <= e) { int m = (s + e) / 2; long long u = sum(0, m, 1, 0, n - 1); if (u <= a[i]) { s = m + 1; } else { e = m - 1; } } v.push_back(s + 1); upd(s, 1, 0, n - 1); } for (int i = v.size() - 1; i >= 0; i--) { cout << v[i] << " "; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int M = 200010; struct BIT { long long tree[M]; BIT() { memset(tree, 0, sizeof tree); } void update(int idx, int val) { while (idx < M) { tree[idx] += val; idx += idx & (-idx); } } long long query(int idx) { long long sum = 0; while (idx > 0) { sum += tree[idx]; idx -= idx & (-idx); } return sum; } }; long long a[200010]; BIT B; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long sum = 0; for (int i = 1; i <= n; i++) { B.update(i, i); } vector<int> ans; for (int i = n - 1; i >= 0; i--) { int l = 1, h = n; while (l < h) { int m = (l + h) / 2; if (B.query(m) > a[i]) h = m; else l = m + 1; } ans.push_back(h); B.update(h, -h); } for (int i = ans.size() - 1; i >= 0; i--) cout << ans[i] << " "; cout << endl; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int max_n = 200222; const long long inf = 1000111222000111222LL; struct tree { long long mx[4 * max_n], add[4 * max_n]; tree() { memset(mx, 0, sizeof(mx)); memset(add, 0, sizeof(add)); } void push(int v) { if (add[v]) { mx[2 * v] += add[v]; mx[2 * v + 1] += add[v]; add[2 * v] += add[v]; add[2 * v + 1] += add[v]; add[v] = 0; } } void update(int v, int tl, int tr, int l, int r, long long x) { if (tl == l && tr == r) { add[v] += x; mx[v] += x; return; } push(v); int mid = (tl + tr) / 2; if (r <= mid) { update(2 * v, tl, mid, l, r, x); } else if (l > mid) { update(2 * v + 1, mid + 1, tr, l, r, x); } else { update(2 * v, tl, mid, l, mid, x); update(2 * v + 1, mid + 1, tr, mid + 1, r, x); } mx[v] = max(mx[2 * v], mx[2 * v + 1]); } int get_more(int v, int l, int r, long long x) { if (l == r) { return l; } push(v); int mid = (l + r) / 2; if (mx[2 * v] >= x) { return get_more(2 * v, l, mid, x); } return get_more(2 * v + 1, mid + 1, r, x); } }; int n, ans[max_n]; long long s[max_n]; tree t; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%I64d", &s[i]); } for (int i = 1; i < n; ++i) { t.update(1, 1, n, i + 1, n, i); } for (int i = n - 1; i >= 0; --i) { ans[i] = t.get_more(1, 1, n, s[i]); t.update(1, 1, n, ans[i], ans[i], -inf); if (ans[i] + 1 < n) { t.update(1, 1, n, ans[i] + 1, n, -ans[i]); } } for (int i = 0; i < n; ++i) { printf("%d ", ans[i]); } puts(""); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { long[] add; long[] min; boolean[] fixed; public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt(); long[] s = new long[n]; for (int i = 0; i < n; i++) { s[i] = in.nextLong(); } initTree(n, s); int[] p = new int[n]; for (int i = 1; i <= n; i++) { int pos = rightmostZero(0, 0, n); add(0, 0, n, pos, pos + 1, (long) n * n); add(0, 0, n, pos + 1, n, -i); p[pos] = i; } for (int i = 0; i < n; i++) { if (i > 0) { out.print(" "); } out.print(p[i]); } out.println(); } void initTree(int n, long[] s) { add = new long[4 * n]; min = new long[4 * n]; fixed = new boolean[4 * n]; recInit(0, 0, n, s); } private void recInit(int root, int rl, int rr, long[] s) { if (rr - rl == 1) { add[root] = s[rl]; min[root] = s[rl]; return; } int rm = (rl + rr) / 2; recInit(2 * root + 1, rl, rm, s); recInit(2 * root + 2, rm, rr, s); pull(root); } void add(int root, int rl, int rr, int l, int r, long val) { if (l < rl) { l = rl; } if (r > rr) { r = rr; } if (l >= r) { return; } if (l == rl && r == rr) { add[root] += val; min[root] += val; return; } int rm = (rl + rr) / 2; push(root); add(2 * root + 1, rl, rm, l, r, val); add(2 * root + 2, rm, rr, l, r, val); pull(root); } void push(int root) { add[2 * root + 1] += add[root]; add[2 * root + 2] += add[root]; min[2 * root + 1] += add[root]; min[2 * root + 2] += add[root]; add[root] = 0; } void pull(int root) { min[root] = add[root] + Math.min(min[2 * root + 1], min[2 * root + 2]); } int rightmostZero(int root, int rl, int rr) { if (rr - rl == 1) { return rl; } int rm = (rl + rr) / 2; push(root); if (min[2 * root + 2] == 0) { return rightmostZero(2 * root + 2, rm, rr); } return rightmostZero(2 * root + 1, rl, rm); } } static class FastScanner { private BufferedReader in; private StringTokenizer st; public FastScanner(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { String rl = in.readLine(); if (rl == null) { return null; } st = new StringTokenizer(rl); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> const int inf = INT_MAX; const int MAX = 3e6 + 9; const long long MOD = 1e9 + 7; const int TOT_PRIMES = 1e6 + 9; const int MAX_A = 71; const int LN = 20; using namespace std; long long bt[MAX]; long long arr[MAX]; void update(int x, long long val) { for (int i = x; i < MAX; i += i & -i) bt[i] += val; } long long query(int x) { long long res = 0; for (int i = x; i > 0; i -= i & -i) res = res + bt[i]; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> arr[i]; for (int i = 1; i <= n; i++) update(i, i); int ans[n + 1]; for (int i = n; i >= 1; i--) { int l = 0, r = n; while (l <= r) { int mid = (l + r) / 2; if (query(mid) > arr[i]) r = mid - 1; else l = mid + 1; } ans[i] = l; update(l, -l); } for (int i = 1; i <= n; i++) cout << ans[i] << " "; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.DataInputStream; import java.io.IOException; import java.io.PrintWriter; public class P1208D9 { public static void main(String[] args) throws IOException { InputReader2 ir = new InputReader2(); PrintWriter pw = new PrintWriter(System.out); int n = ir.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = ir.nextLong(); } Fenwick f = new Fenwick(n); for (int i = 1; i <= n; i++) { f.add(i, i); } int[] list = new int[n]; for (int i = n - 1; i >= 0; i--) { int ind = f.indexWithGivenCumFreq(a[i]); f.add(ind, -ind); list[i] = ind; } for (int i = 0; i < n; i++) { pw.print(list[i] + " "); } pw.close(); } static class Fenwick { public final long[] bit; public final int size; public Fenwick(int size) { this.size = size + 1; bit = new long[this.size]; } public void add(int i, long delta) { for (++i; i < size; i += (i & -i)) { bit[i] += delta; } } public int indexWithGivenCumFreq(long v) { int i = 0, n = size; for (int b = Integer.highestOneBit(n); b != 0; b >>= 1) { if ((i | b) < n && bit[i | b] <= v) { i |= b; v -= bit[i]; } } return i; } } private static class InputReader2 { final private int BUFFER_SIZE = 1 << 16; private final DataInputStream dis; private final byte[] buffer; private int bufferPointer, bytesRead; public InputReader2() { dis = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return ret; } private long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return ret; } private void fillBuffer() throws IOException { bytesRead = dis.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.util.*; public class CP { public static void main(String[] args) throws Exception { /*new Thread(null, new Runnable() { @Override public void run() { try { new Solver().solve(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }, "Solver", 1l << 30).start();*/ new Solver().solve(); } } class Solver { IO io = new IO(System.in, System.out); int N; long[] S; SegmentTree segT; BitSet added; void solve() throws Exception { int i, j, k, l; N = io.nextInt(); S = new long[N]; for (i = 0; i < N; ++i) S[i] = io.nextLong(); segT = new SegmentTree(N + 7); added = new BitSet(); added.set(0, N + 3); int[] A = new int[N]; for (i = N - 1; i >= 0; --i) { int ele = bs(i); segT.update(ele, ele); added.clear(ele); A[i] = ele; //System.out.println(Arrays.toString(A)); } for (long ele : A) io.printsp(ele); io.println(); io.flush(); } int bs(int i) { int l = 1, r = N; while (true) { int mid = l + r >> 1; //mid = added.nextSetBit(mid); //if (mid == r) mid = added.previousSetBit(mid - 1); //System.out.println(l + " " + r + " = " + mid + " " + (S[i] + segT.query(0, mid))); if (l + 1 >= r) { //System.out.println(l + " " + r); if (calc(r) == S[i] + segT.query(0, r)) return r; else if (calc(l) == S[i] + segT.query(0, l)) return l; else return 7 / 0; } else if (S[i] + segT.query(0, mid) < calc(mid)) r = mid; else l = mid; } } long calc(long ele) { return ele * (ele - 1) >> 1; } } class SegmentTree { int N; long[] tree; SegmentTree(int n) { N = 1; while (N < n) N <<= 1; tree = new long[N * 2 - 1]; } void update(int i, long val) { i += N - 1; while (i >= 0) { tree[i] += val; i = i - 1 >> 1; } } long query(int i, int l, int r, int ql, int qr) { int mid = l + r >> 1, i2 = i << 1; if (l > qr || r < ql) return 0; else if (l >= ql && r <= qr) return tree[i]; else return query(i2 + 1, l, mid, ql, qr) + query(i2 + 2, mid + 1, r, ql, qr); } long query(int l, int r) { return query(0, 0, N - 1, l, r - 1); } } class IO { static byte[] buf = new byte[2048]; static int index, total; static InputStream in; static BufferedWriter bw; IO(InputStream is, OutputStream os) { try { in = is; bw = new BufferedWriter(new OutputStreamWriter(os)); } catch (Exception e) { } } IO(String inputFile, String outputFile) { try { in = new FileInputStream(inputFile); bw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(outputFile))); } catch (Exception e) { } } int scan() throws Exception { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) return -1; } return buf[index++]; } String next() throws Exception { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) sb.append((char) c); return sb.toString(); } int nextInt() throws Exception { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') c = scan(); for (; c >= '0' && c <= '9'; c = scan()) val = (val << 3) + (val << 1) + (c & 15); return neg ? -val : val; } long nextLong() throws Exception { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') c = scan(); for (; c >= '0' && c <= '9'; c = scan()) val = (val << 3) + (val << 1) + (c & 15); return neg ? -val : val; } void print(Object a) throws Exception { bw.write(a.toString()); } void printsp(Object a) throws Exception { print(a); bw.write(" "); } void println() throws Exception { bw.write("\n"); } void println(Object a) throws Exception { print(a); println(); } void flush() throws Exception { bw.flush(); bw.close(); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; inline ll read(); const int M = 500016, MOD = 1000000007; struct BinaryIndexTree { ll bit[M], n; inline void modify(ll p, ll x) { for (; p <= n; p += p & -p) bit[p] += x; } inline ll sum(ll p) { ll res = 0; for (; p; p -= p & -p) res += bit[p]; return res; } inline ll sum(ll l, ll r) { return sum(r) - sum(l - 1); } } bit; ll s[M], save[M], vis[M]; int cal(ll s, int n) { ll lef = 1, rig = n; while (lef <= rig) { ll mid = (lef + rig) >> 1; ll val = mid * (mid - 1) / 2 - bit.sum(mid - 1); if (val == s && !vis[mid]) return mid; else if (val <= s) lef = mid + 1; else rig = mid - 1; } assert(0); return -1; } int main(void) { int n = read(); bit.n = n; for (int i = 1; i <= n; ++i) s[i] = read(); for (int i = n; i >= 1; --i) { save[i] = cal(s[i], n); bit.modify(save[i], save[i]); vis[save[i]] = 1; } for (int i = 1; i <= n; ++i) cout << save[i] << " "; cout << endl; return 0; } inline ll read() { ll x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
python3
# 1208D 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) >> 1 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) >> 1 if self.t[node*2] >= value: return self.query(node*2, l, mid, value) return self.query(node*2 + 1, mid + 1, r, value - self.t[node*2]) def do(): n = int(input()) nums = [int(i) for i in input().split(" ")] res = [0]*n weightTree = segTree(n) 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 " ] }
CORRECT
java
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.InputStreamReader; public class D1208 { public static void main(String[] args) throws Exception { // BufferedReader br = new BufferedReader(new FileReader("F:/books/input.txt")); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Integer n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split(" "); Long[] a = new Long[s.length]; for (int i = 0; i < s.length; i++) { a[i] = Long.parseLong(s[i]); } long[] tree = new long[4*n]; long[] lazy = new long[4*n]; for(int i=0;i<n;i++) { segmentTreeMinUpdateLazy(lazy,tree,0,n-1,0,i,i,a[i]); } for(int i=0;i<n;i++) { int pos = getRightZero(lazy,tree,n,0,n-1); segmentTreeMinUpdateLazy(lazy,tree,0,n-1,0,pos+1,n-1,-(i+1)); segmentTreeMinUpdateLazy(lazy,tree,0,n-1,0,pos,pos,Long.MAX_VALUE); a[pos] = (long) (i+1); } StringBuilder sb = new StringBuilder(); for(int i=0;i<n;i++) sb.append(a[i]+" "); System.out.println(sb.toString()); } private static int getRightZero(long[] lazy,long[] tree,int n,int s, int e) { if(s==e) { return s; } int m = (s+e)/2; if(segmentTreeMinQueryLazy(lazy,tree,0,n-1,0,m+1,e)==0) return getRightZero(lazy,tree,n,m+1,e); return getRightZero(lazy,tree,n,s,m); } private static void segmentTreeMinUpdateLazy(long[] lazy,long[] tree,int ss, int se,int si,int us,int ue,long v) { if(lazy[si]!=0) { tree[si] += lazy[si]; if(ss!=se) { lazy[si*2+1] += lazy[si]; lazy[si*2+2] += lazy[si]; } lazy[si] = 0; } if(us>se || ue<ss) return; if(us<=ss && ue>=se) { tree[si] += v; if(ss!=se) { lazy[si*2+1] += v; lazy[si*2+2] += v; } return; } int m = (ss+se)/2; segmentTreeMinUpdateLazy(lazy,tree,ss,m,si*2+1,us,ue,v); segmentTreeMinUpdateLazy(lazy,tree,m+1,se,si*2+2,us,ue,v); tree[si] = Math.min(tree[si*2+1], tree[si*2+2]); } private static long segmentTreeMinQueryLazy(long[] lazy,long[] tree,int ss, int se,int si,int qs,int qe) { if(lazy[si]!=0) { tree[si] += lazy[si]; if(ss!=se) { lazy[si*2+1] += lazy[si]; lazy[si*2+2] += lazy[si]; } lazy[si] = 0; } if(qs>se || qe<ss) return Long.MAX_VALUE; if(qs<=ss && qe>=se) return tree[si]; int m = (ss+se)/2; return Math.min(segmentTreeMinQueryLazy(lazy,tree,ss,m,si*2+1,qs,qe), segmentTreeMinQueryLazy(lazy,tree,m+1,se,si*2+2,qs,qe)); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double PI = 3.141592653589793; const long long I_MAX = 1LL << 60; long long mod = 1000000007; const long long maxN = 2E5; vector<long long> inv(maxN + 5, 1); vector<long long> fac(maxN + 5, 1); void ADD(long long &x, long long y) { x += y; if (x >= mod) x -= mod; if (x < 0) x += mod; } void MUL(long long &x, long long y) { x *= y; x = (x % mod + mod) % mod; } long long mypow(long long b, long long e) { long long ans = 1; while (e) { if (e & 1) MUL(ans, b); MUL(b, b); e >>= 1; } return ans; } long long modinv(long long n) { return mypow(n, mod - 2); } void calcInv(long long n) { for (int i = 2; i <= n; ++i) { inv[i] = (mod - mod / i) * inv[mod % i] % mod; } } void calcFac(long long n) { for (int i = 2; i <= n; ++i) { fac[i] = fac[i - 1] * i % mod; } } long long cm(long long a, long long b) { long long ans = 1; if (a < b) return 0; MUL(ans, inv[fac[a - b]]); MUL(ans, inv[fac[b]]); MUL(ans, fac[a]); return ans; } long long Lucas(long long n, long long m) { if (m == 0) return 1; return cm(n % mod, m % mod) * Lucas(n / mod, m / mod) % mod; } long long doLucas(long long n, long long m) { calcInv(mod); calcFac(mod); return Lucas(n, m); } const long long maxn = 2E5 + 5; long long arr[maxn + 1]; int lowbit(int x) { return x & (-x); } void change(int x, long long add, int size) { for (int i = x; i <= size; i += lowbit(i)) { arr[i] += add; } } long long query(int x, int size) { long long ret = 0; for (int i = x; i > 0; i -= lowbit(i)) { ret += arr[i]; } return ret; } void solve() { long long n; cin >> n; bool used[n + 1]; for (int i = 0; i <= n; ++i) used[i] = false; vector<long long> num(n), res(n, 0); for (int i = 0; i < n; ++i) { cin >> num[i]; } for (int i = 1; i <= n; ++i) { change(i, i - 1, n); } for (int i = n - 1; i >= 1; --i) { int lo = 1, hi = n, mi; while (lo < hi) { mi = (lo + hi) >> 1; long long tmp = query(mi, n); if (tmp < num[i]) { lo = mi + 1; } else if (tmp == num[i] and used[mi]) { lo = mi + 1; } else { hi = mi; } } change(lo + 1, -lo, n); used[lo] = true; res[i] = lo; } for (int i = 1; i <= n; ++i) { if (!used[i]) { res[0] = i; break; } } for (int i = 0; i < n; ++i) { cout << res[i] << ' '; } cout << '\n'; } int main() { int t = 1; ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); for (int i = 1; i <= t; ++i) { solve(); } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.util.*; import java.io.*; import java.text.*; public class Main{ //SOLUTION BEGIN //Into the Hardware Mode void pre() throws Exception{} void solve(int TC)throws Exception{ int n = ni(); long[] s = new long[n]; for(int i = 0; i< n; i++)s[i] = nl(); SegTree t = new SegTree(n+1); for(int i = 1; i<= n; i++)t.u(i, i); int[] p = new int[n]; for(int i = n-1; i>= 0; i--){ p[i] = t.q(s[i]); t.u(p[i], 0); } for(int i:p)p(i+" ");pn(""); } class SegTree{ int m= 1; long[] t; public SegTree(int n){ while(m<n)m<<=1; t = new long[m<<1]; } void u(int p, long add){ t[p+=m] = add; for(p>>=1;p>0;p>>=1)t[p] = t[p<<1]+t[p<<1|1]; } int q(long s){ return q(0, m-1, 1, s); } int q(int ll, int rr, int i, long s){ if(ll == rr)return i-m; int mid = (ll+rr)/2; if(t[i<<1] > s)return q(ll, mid, i<<1, s); return q(mid+1, rr, i<<1|1, s-t[i<<1]); } } //SOLUTION END void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");} void exit(boolean b){if(!b)System.exit(0);} long IINF = (long)1e18, mod = (long)1e9+7; final int INF = (int)1e9, MX = (int)2e6+5; DecimalFormat df = new DecimalFormat("0.00000000"); double PI = 3.141592653589793238462643383279502884197169399, eps = 1e-6; static boolean multipleTC = false, memory = false, fileIO = false; FastReader in;PrintWriter out; void run() throws Exception{ if(fileIO){ in = new FastReader("input.txt"); out = new PrintWriter("output.txt"); }else { in = new FastReader(); out = new PrintWriter(System.out); } //Solution Credits: Taranpreet Singh int T = (multipleTC)?ni():1; pre(); for(int t = 1; t<= T; t++)solve(t); out.flush(); out.close(); } public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start(); else new Main().run(); } int digit(long s){int ans = 0;while(s>0){s/=10;ans++;}return ans;} long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));} void p(Object o){out.print(o);} void pn(Object o){out.println(o);} void pni(Object o){out.println(o);out.flush();} String n()throws Exception{return in.next();} String nln()throws Exception{return in.nextLine();} int ni()throws Exception{return Integer.parseInt(in.next());} long nl()throws Exception{return Long.parseLong(in.next());} double nd()throws Exception{return Double.parseDouble(in.next());} class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next() throws Exception{ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception{ String str = ""; try{ str = br.readLine(); }catch (IOException e){ throw new Exception(e.toString()); } return str; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.util.*; import java.io.*; public class _1208_D { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int n = Integer.parseInt(in.readLine()); long[] p = new long[n]; long[] a = new long[n + 1]; StringTokenizer line = new StringTokenizer(in.readLine()); for(int i = 0; i < n; i++) { p[i] = Long.parseLong(line.nextToken()); a[i + 1] = i + 1; } int[] res = new int[n]; BIT bit = new BIT(a); for(int i = n - 1; i >= 0; i--) { int ind = bit.bsearch(p[i]) + 1; res[i] = ind; bit.update(ind, -ind); } StringBuilder sb = new StringBuilder(); for(int i = 0; i < n; i++) { sb.append(res[i]); sb.append(' '); } out.println(sb.toString()); in.close(); out.close(); } static class BIT { long[] bit; BIT(long[] a) { bit = new long[a.length + 1]; for(int i = 0; i < a.length; i++) { update(i, a[i]); } } void update(int index, long add) { index++; while(index < bit.length) { bit[index] += add; index += index & -index; } } long sum(int index) { index++; long res = 0; while(index > 0) { res += bit[index]; index -= index & -index; } return res; } int bsearch(long val) { int l = 0; int r = bit.length - 1; int res = -1; while(l <= r) { int avg = (l + r) / 2; if(sum(avg) <= val) { res = avg; l = avg + 1; }else { r = avg - 1; } } return res; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.util.*; import java.text.*; import java.lang.*; import java.math.*; public class Myclass{ static ArrayList[] a=new ArrayList[200001]; public void solve () { InputReader in = new InputReader(System.in); PrintWriter pw = new PrintWriter(System.out); int n =in.nextInt(); long a[]=new long [n]; for(int i=0;i<n;i++) a[i]=in.nextLong(); Segment s=new Segment(n+1); long ans[]=new long [n]; s.build(0, 0, n); boolean taken []=new boolean [n+1]; for(int i=n-1;i>=0;i--) { long start=1; long end=n; long fi=-1; while(start<=end) { long mid=(start+end)/2L; long val=(mid*(mid-1))/2L-s.query(0, 0, n, 0, (int)mid-1); if(val<=a[i]) { fi=mid; start=mid+1; } else { end=mid-1; } } s.update(0, 0, n, (int)fi, fi); ans[i]=fi; taken [(int) fi]=true; } for(int i=0;i<n;i++) { pw.print(ans[i]+" "); } pw.flush(); pw.close(); } public static void main(String[] args) throws Exception { new Thread(null,new Runnable() { public void run() { new Myclass().solve(); } },"1",1<<26).start(); } static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static long mod = 1000000007; public static int d; public static int p; public static int q; public void extended(int a,int b) { if(b==0) { d=a; p=1; q=0; } else { extended(b,a%b); int temp=p; p=q; q=temp-(a/b)*q; } } public static int binaryExponentiation(int x,int n) { int result=1; while(n>0) { if(n % 2 ==1) result=result * x; x=x*x; n=n/2; } return result; } public static long binaryExponentiation(long x,long n) { long result=1; while(n>0) { if(n % 2 ==1) result=result * x; x=x*x; n=n/2; } return result; } public static long[] shuffle(long[] a,Random gen) { int n = a.length; for(int i=0;i<n;i++) { int ind = gen.nextInt(n-i)+i; long temp = a[ind]; a[ind] = a[i]; a[i] = temp; } return a; } public static void swap(int a, int b){ int temp = a; a = b; b = temp; } public static HashSet<Integer> primeFactorization(int n) { HashSet<Integer> a =new HashSet<Integer>(); for(int i=2;i*i<=n;i++) { while(n%i==0) { a.add(i); n/=i; } } if(n!=1) a.add(n); return a; } public static void sieve(boolean[] isPrime,int n) { for(int i=1;i<n;i++) isPrime[i] = true; isPrime[0] = false; isPrime[1] = false; for(int i=2;i*i<n;i++) { if(isPrime[i] == true) { for(int j=(2*i);j<n;j+=i) isPrime[j] = false; } } } public static long GCD(long n,long m) { if(m==0) return n; else return GCD(m,n%m); } static class pair implements Comparable<pair> { Long x,y; pair(long l,long m) { this.x=l; this.y=m; } public int compareTo(pair o) { int result = x.compareTo(o.x); if(result==0) result = y.compareTo(o.y); return result; } public String toString() { return x+" "+y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair)o; return p.x.equals(x) && p.y.equals(y) ; } return false; } public int hashCode() { return new Long(x).hashCode()*31 + new Long(y).hashCode(); } } } /*class dsu{ int parent[]; dsu(int n){ parent=new int[n+1]; for(int i=0;i<=n;i++) { parent[i]=i; } } int root(int n) { while(parent[n]!=n) { parent[n]=parent[parent[n]]; n=parent[n]; } return n; } void union(int _a,int _b) { int p_a=root(_a); int p_b=root(_b); parent[p_a]=p_b; } boolean find(int a,int b) { if(root(a)==root(b)) return true; else return false; } }*/ class Segment{ long seg[]; long[] a; int lazy[]; Segment (int n){ seg=new long[4*n]; lazy=new int[4*n]; } public void build(int node,int start,int end) { if(start==end) { seg[node]=0; return ; } int mid=(start+end)/2; build(2*node+1,start,mid); build(2*node+2,mid+1,end); seg[node]=(seg[2*node+1]+seg[2*node+2]); } public void update(int node,int start,int end,int fi,long fi2) { if(start==end) { seg[node]=fi2; return; } int mid=(start+end)/2; if(fi>=start && fi<=mid) { update(2*node+1,start,mid,fi,fi2); } else update(2*node+2,mid+1,end,fi,fi2); seg[node]=seg[2*node+1]+seg[2*node+2]; } public long query(int node,int start,int end,int l,int r) { if(l>end || r<start) return 0; if(start>=l && end<=r) return seg[node]; int mid=(start+end)/2; return (query(2*node+1,start,mid,l,r)+query(2*node+2,mid+1,end,l,r)); } /*public void updateRange(int node,int start,int end,int l,int r,int val) { if(lazy[node]!=0) { if(start!=end) { lazy[2*node+1]+=lazy[node]; lazy[2*node+2]+=lazy[node]; } lazy[node]=0; } if(l>end || r<start) return ; if(start>=l && end<=r) { seg[node]=(end-start+1)*val; if(start!=end) { lazy[2*node+1]+=val; lazy[2*node+2]+=val; } lazy[node]=0; return ; } int mid=(start+end)/2; updateRange(2*node+1,start,mid,l,r,val); updateRange(2*node+2,mid+1,end,l,r,val); seg[node]=seg[2*node+1]+seg[2*node+2]; } public int queryRange(int node,int start,int end,int l,int r) { if(l>end || r<start) return 0; if(lazy[node]!=0) { seg[node]=(end-start+1)*lazy[node]; if(start!=end) { lazy[2*node+1]+=lazy[node]; lazy[2*node+2]+=lazy[node]; } lazy[node]=0; } if(start>=l && end<=r) return seg[node]; int mid=(start+end)/2; return (query(2*node+1,start,mid,l,r)+query(2*node+2,mid+1,end,l,r)); }*/ }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
python3
import sys input=sys.stdin.readline n=int(input()) s=list(map(int,input().split())) BIT=[0]*(n+1) def update(i,w): while i<=n: BIT[i]+=w i+=(i&-i) def get_sum(i): res=0 while i>0: res+=BIT[i] i-=(i&-i) return res for i in range(1,n+1): update(i,i) ans=[-1]*n for i in range(n)[::-1]: l=0;r=n ss=s[i] while True: mid=(l+r+1)//2 if get_sum(mid)>ss: if get_sum(mid-1)==ss: ans[i]=mid break else: r=mid else: l=mid update(mid,-mid) print(*ans)
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; long long ar[200005]; long long tree[800005]; long long valr[800005]; long long lazy[800005]; long long ans[200005]; int gl = 0; long long h; void build(int node, int start, int end) { if (start == end) { tree[node] = ar[start]; valr[node] = start; } else { int mid = (start + end) / 2; build(2 * node, start, mid); build(2 * node + 1, mid + 1, end); tree[node] = min(tree[node * 2], tree[node * 2 + 1]); if (tree[node * 2 + 1] == tree[node]) valr[node] = valr[node * 2 + 1]; else valr[node] = valr[node * 2]; } } void updateRange(int node, long long start, long long end, int l, long long r, long long val) { if (lazy[node] != 0) { tree[node] -= lazy[node]; if (start != end) { lazy[node * 2] += lazy[node]; lazy[node * 2 + 1] += lazy[node]; } lazy[node] = 0; } if (start > end or start > r or end < l) return; if (start >= l and end <= r) { tree[node] -= val; if (start != end) { lazy[node * 2] += val; lazy[node * 2 + 1] += val; } if (start == end && gl) { valr[node] = h; } lazy[node] = 0; return; } int mid = (start + end) / 2; updateRange(node * 2, start, mid, l, r, val); updateRange(node * 2 + 1, mid + 1, end, l, r, val); tree[node] = min(tree[node * 2], tree[node * 2 + 1]); if (tree[node * 2 + 1] == tree[node]) valr[node] = valr[node * 2 + 1]; else valr[node] = valr[node * 2]; } pair<long long, long long> queryRange(int node, int start, int end, int l, int r) { if (start > end or start > r or end < l) return {10004000000, -1}; if (lazy[node] != 0) { tree[node] -= lazy[node]; if (start != end) { lazy[node * 2] += lazy[node]; lazy[node * 2 + 1] += lazy[node]; } lazy[node] = 0; } if (start >= l and end <= r) return {tree[node], valr[node]}; int mid = (start + end) / 2; pair<int, int> p1 = queryRange(node * 2, start, mid, l, r); pair<int, int> p2 = queryRange(node * 2 + 1, mid + 1, end, l, r); if (p2.first <= p1.first) return p2; return p1; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) cin >> ar[i]; gl = 1; build(1, 0, n - 1); gl = 0; for (int i = 0; i < n; i++) { pair<long long, long long> h = queryRange(1, 0, n - 1, 0, n - 1); ans[h.second] = i + 1; updateRange(1, 0, n - 1, h.second + 1, n - 1, i + 1); updateRange(1, 0, n - 1, h.second, h.second, -200000000000); } for (int i = 0; i < n; i++) cout << ans[i] << " "; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
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, 1000000000000000000L); 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 " ] }
CORRECT
java
import java.io.*; import java.io.IOException; import java.util.*; //import javafx.util.Pair; //import java.util.concurrent.LinkedBlockingDeque; import javax.swing.Icon; //import sun.net.www.content.audio.wav; import java.text.DecimalFormat; public class Codeforces { public static long mod = (long)Math.pow(10, 9)+7 ; public static double epsilon=0.00000000008854;//value of epsilon public static InputReader sc = new InputReader(System.in); public static PrintWriter pw = new PrintWriter(System.out); public static int countSet(int a){ int c=0; while(a>0){ a&=(a-1); c++; } return c; } public static void Zfunction(String s){ int n=s.length(); int a[]=new int[n]; int r1=0,r2=1; for(int i=1;i<n;){ int f=0; while(r2<n&&s.charAt(r1)==s.charAt(r2)){ r1++; r2++; } a[i]=r1; i++; int l2=1; for(;i<r2;i++){ if(i+a[l2]<r2) a[i]=a[l2++]; else{ f=1; r1=r2-i; break; } } if(f==0){ r2=i; r1=0; } } pw.println(Arrays.toString(a)); } public static ArrayList<ArrayList <Integer>> GetGraph(int n,int m){ ArrayList<ArrayList <Integer>> a=new ArrayList<>(); for(int i=0;i<n;i++){ a.add(new ArrayList<>()); } for(int i=0;i<m;i++){ int u=sc.nextInt()-1; int v=sc.nextInt()-1; a.get(u).add(v); a.get(v).add(u); } return a; } public static int firstNode(int a,int p[]){ if(a!=p[a]){ p[a]=firstNode(p[a], p); } return p[a]; } public static void Union(int a,int b,int p[]){ //int a=firstNode(a1, p); //int b=firstNode(b1, p); /*if(a!=b){ if(r[a]<r[b]){ p[a]=b; } else if(r[a]>r[b]){ p[b]=a; } else{ r[b]++; p[a]=b; } }*/ if(a!=b) p[firstNode(a,p)]=firstNode(b,p);//if no rank than } public static void main(String[] args) { // code starts.. int n=sc.nextInt(); long a[]=scanLongArray(n); Bit h=new Bit(n); for(int i=1;i<=n;i++){ h.update(i, i); } int ans[]=new int[n]; for(int i=n;i>0;i--){ int in=h.searchNumber(a[i-1]); ans[i-1]=in+1; h.update(in+1, -(in+1)); //pw.println(h.query(1)); } for(int i=0;i<n;i++) pw.print(ans[i]+" "); // Code ends... pw.flush(); pw.close(); } public static Comparator<Integer> C(){ return new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2.compareTo(o1);//for descending } }; } static class tripletL implements Comparable<tripletL> { Long x, y, z; tripletL(long x, long y, long z) { this.x = x; this.y = y; this.z = z; } public int compareTo(tripletL o) { int result = x.compareTo(o.x); if (result == 0) result = y.compareTo(o.y); if (result == 0) result = z.compareTo(o.z); return result; } public boolean equlas(Object o) { if (o instanceof tripletL) { tripletL p = (tripletL) o; return (x - p.x == 0) && (y - p.y ==0 ) && (z - p.z == 0); } return false; } public String toString() { return x + " " + y + " " + z; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode() + new Long(z).hashCode(); } } public static String Doubleformate(double a,int n){ String s=""; while(n-->0){ s+='0'; } DecimalFormat f =new DecimalFormat("#0."+s); return f.format(a); } public static Comparator<Integer[]> column(int i){ return new Comparator<Integer[]>() { @Override public int compare(Integer[] o1, Integer[] o2) { return o1[i].compareTo(o2[i]);//for ascending //return o2[i].compareTo(o1[i]);//for descending } }; } public static Comparator<Long[]> column1(int i){ return new Comparator<Long[]>() { @Override public int compare(Long[] o1, Long[] o2) { return o1[i].compareTo(o2[i]);//for ascending //return o2[i].compareTo(o1[i]);//for descending } }; } public static Comparator<Integer[]> pair(){ return new Comparator<Integer[]>() { @Override public int compare(Integer[] o1, Integer[] o2) { int result=o1[0].compareTo(o2[0]); if(result==0) result=o1[1].compareTo(o2[1]); return result; } }; } public static Comparator<Integer[]> Triplet(){ return new Comparator<Integer[]>() { @Override public int compare(Integer[] o1, Integer[] o2) { for(int i=0;i<3;i++){ for(int j=i+1;j<3;j++){ for(int k=0;k<3;k++){ for(int p=k+1;p<3;p++){ if((o1[i]==o2[k]&&o1[j]==o2[p])||(o1[j]==o2[k]&&o1[i]==o2[p])){ } } } } } int result=o1[0].compareTo(o2[0]); if(result==0) result=o1[1].compareTo(o2[1]); if(result==0) result=o1[2].compareTo(o2[2]); return result; } }; } public static String reverseString(String s){ StringBuilder input1 = new StringBuilder(); input1.append(s); input1 = input1.reverse(); return input1.toString(); } public static int[] scanArray(int n){ int a[]=new int [n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); return a; } public static long[] scanLongArray(int n){ long a[]=new long [n]; for(int i=0;i<n;i++) a[i]=sc.nextLong(); return a; } public static String [] scanStrings(int n){ String a[]=new String [n]; for(int i=0;i<n;i++) a[i]=sc.nextLine(); return a; } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class Bit{//1...n long a[];int n; Bit(int n){ this.n=n; a=new long[n+1]; } void update(int i,int delta){ while(i<a.length){ a[i]+=delta; i+=i&(-i); } } long query(int i){ long sum=0; while(i>0){ sum+=a[i]; i-=i&(-i); } return sum; } int searchNumber(long sum){ int l=1,r=n,ans=0; while(l<=r){ int m=(l+r)/2; long v=query(m); if(v>sum){ r=m-1; } else if(v<sum){ l=m+1; } else{ ans=m; l=m+1; } } return ans; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.util.*; public class Test { static int readInt() { int ans = 0; boolean neg = false; try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (c == '-') { start = true; neg = true; continue; } else if (c >= '0' && c <= '9') { start = true; ans = ans * 10 + c - '0'; } else if (start) break; } } catch (IOException e) { } return neg ? -ans : ans; } static long readLong() { long ans = 0; boolean neg = false; try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (c == '-') { start = true; neg = true; continue; } else if (c >= '0' && c <= '9') { start = true; ans = ans * 10 + c - '0'; } else if (start) break; } } catch (IOException e) { } return neg ? -ans : ans; } static String readLine() { StringBuilder b = new StringBuilder(); try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (Character.isLetterOrDigit(c)) { start = true; b.append((char)c); } else if (start) break; } } catch (IOException e) { } return b.toString(); } static PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); long[] min = new long[1234567]; long[] add = new long[1234567]; long[] s = new long[1234567]; void push(int x) { if (add[x] != 0) { min[2*x] += add[x]; add[2*x] += add[x]; min[2*x+1] += add[x]; add[2*x+1] += add[x]; add[x] = 0; } } void pull(int x) { min[x] = Math.min(min[2*x], min[2*x+1]); } void build(int x, int ll, int rr) { if (ll == rr) { min[x] = s[ll]; return; } int mid = (ll+rr)/2; build(2*x, ll, mid); build(2*x+1, mid+1, rr); pull(x); } void upd(int x, int ll, int rr, int i, int j, long d) { if (j < ll || rr < i) return; if (i <= ll && rr <= j) { add[x] += d; min[x] += d; return; } push(x); int mid = (ll + rr) / 2; upd(2*x, ll, mid, i, j, d); upd(2*x+1, mid+1, rr, i, j, d); pull(x); } int zero(int x, int ll, int rr) { if (rr < ll || min[x] > 0) return -1; if (ll == rr) return ll; push(x); int mid = (ll + rr) / 2; int ans = zero(2*x+1, mid + 1, rr); if (ans < 0) ans = zero(2*x, ll, mid); pull(x); return ans; } void start() { int n = readInt(); for (int i = 1; i <= n; i++) { s[i] = readLong(); } build(1, 1, n); int[] ans = new int[n+1]; for (int i = 1; i <= n; i++) { int j = zero(1, 1, n); ans[j] = i; upd(1, 1, n, j, j, Long.MAX_VALUE - s[j]); upd(1, 1, n, j+1, n, -i); } for (int i = 1; i <= n; i++) writer.print(ans[i] + " "); } public static void main(String[] args) { Test te = new Test(); te.start(); writer.flush(); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class Solution { static long fen[]; static int n; public static void update(long x,int pos) { while(pos<=n) { fen[pos]+=x; pos=pos+(pos & -pos); } } public static long sum(int pos) { long sum=0; while(pos>0) { sum+=fen[pos]; pos=pos- (pos & -pos); } return sum; } public static int binary(long x,int l,int r) { if(l==r) return l; else if(l+1==r) { if(sum(r)==x) return r; return l; } else { int mid=(l+r)/2; long z=sum(mid); if(z>x) return binary(x,l,mid-1); else return binary(x,mid,r); } } public static void main(String[] args) throws IOException{ FastReader in=new FastReader(System.in); StringBuilder sb=new StringBuilder(); int i,j; n=in.nextInt(); long arr[]=new long[n]; int ans[]=new int[n]; fen=new long[n+1]; for(i=0;i<n;i++) arr[i]=in.nextLong(); for(i=0;i<n;i++) update(i,i+1); for(i=n-1;i>=0;i--) { ans[i]=binary(arr[i],1,n); update(-1*ans[i],ans[i]+1); } for(i=0;i<n;i++) sb.append(ans[i]).append(" "); System.out.print(sb); } } class Node implements Comparable<Node> { int val; int pos; public Node(int v,int p) { val=v; pos=p; } public int compareTo(Node n) { if(this.val>n.val) return 1; else if(this.val<n.val) return -1; else return 0; } } class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c != 10 && c != 13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; return (char) c; } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.DataInputStream; import java.io.IOException; import java.io.PrintWriter; public class P1208D4 { public static void main(String[] args) throws IOException { InputReader2 ir = new InputReader2(); PrintWriter pw = new PrintWriter(System.out); int n = ir.nextInt(); long[] s = new long[n]; for (int i = 0; i < n; i++) { s[i] = ir.nextLong(); } BIT tree = new BIT(n); for (int i = 1; i <= n; i++) { tree.add(i, i); } int[] ans = new int[n]; for (int i = n - 1; i >= 0; i--) { long cur = s[i]; if (cur == 0) { ans[i] = binSearch(tree, 0, n); tree.remove(ans[i]); } else { ans[i] = binSearch(tree, cur, n); tree.remove(ans[i]); } } for (int an : ans) { pw.print(an + " "); } pw.close(); } private static int binSearch(BIT tree, long key, int n) { int low = 0, high = n; while (low <= high) { int mid = (low + high) / 2; long amt = tree.prefixSum(mid); if (amt <= key) { low = mid + 1; } else { high = mid - 1; } } return low; } public static class BIT { // The size of the array holding the Fenwick tree values final int N; // This array contains the Fenwick tree ranges private final long[] tree; // Create an empty Fenwick Tree with 'sz' parameter zero based. public BIT(int sz) { tree = new long[(N = sz + 1)]; } // Returns the value of the least significant bit (LSB) private static int lsb(int i) { // Isolates the lowest one bit value return i & -i; } // Computes the prefix sum from [1, i], O(log(n)) private long prefixSum(int i) { long sum = 0L; while (i != 0) { sum += tree[i]; i &= ~lsb(i); // Equivalently, i -= lsb(i); } return sum; } // Returns the sum of the interval [left, right], O(log(n)) private long sum(int left, int right) { if (right < left) throw new IllegalArgumentException("Make sure right >= left"); return prefixSum(right) - prefixSum(left - 1); } // Add 'v' to index 'i', O(log(n)) public void add(int i, long v) { while (i < N) { tree[i] += v; i += lsb(i); } } // Set index i to be equal to v, O(log(n)) public void remove(int i) { add(i, -sum(i, i)); } } private static class InputReader2 { final private int BUFFER_SIZE = 1 << 16; private final DataInputStream dis; private final byte[] buffer; private int bufferPointer, bytesRead; public InputReader2() { dis = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return ret; } private long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return ret; } private void fillBuffer() throws IOException { bytesRead = dis.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int maxN = 1e6 + 10; ll tree[4 * maxN]; int n, p[maxN]; ll s[maxN]; void update(int x, int l, int r, int k, int w) { if (l == r) tree[x] = w; else { int mid = (l + r) / 2; if (k <= mid) update(2 * x, l, mid, k, w); else update(2 * x + 1, mid + 1, r, k, w); tree[x] = tree[2 * x] + tree[2 * x + 1]; } } void build(int x, int l, int r) { if (l == r) tree[x] = p[l]; else { int mid = (l + r) / 2; build(2 * x, l, mid); build(2 * x + 1, mid + 1, r); tree[x] = tree[2 * x] + tree[2 * x + 1]; } } ll query(int x, int l, int r, int i, int j) { if (l > j || r < i) return 0; else if (l >= i && r <= j) return tree[x]; int mid = (l + r) / 2; return query(2 * x, l, mid, i, j) + query(2 * x + 1, mid + 1, r, i, j); } void input() { cin >> n; for (int i = int(1); i <= int(n); ++i) p[i] = i; for (int i = int(1); i <= int(n); ++i) cin >> s[i]; } void solve() { build(1, 1, n); for (int i = int(n); i >= int(1); --i) { int l = 1, r = n + 1, mid; ll tmp; while (r - l > 1) { mid = (l + r) / 2; if (mid == 0) tmp = 0; else tmp = query(1, 1, n, 1, mid - 1); if (tmp <= s[i]) { l = mid; } else r = mid; } p[i] = l; update(1, 1, n, l, 0); } } void output() { for (int i = int(1); i <= int(n); ++i) cout << p[i] << " "; } int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); input(); solve(); output(); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; pair<long long, long long> t[4 * 200009]; long long a[200009], lazy[4 * 200009], ans[200009]; void build(long long v, long long tl, long long tr) { if (tl == tr) { t[v] = {a[tl], tl}; } else { long long tm = (tl + tr) / 2; build(v * 2, tl, tm); build(v * 2 + 1, tm + 1, tr); if (t[v * 2].first < t[v * 2 + 1].first || (t[v * 2].first == t[v * 2 + 1].first && t[v * 2].second > t[v * 2 + 1].second)) t[v] = t[v * 2]; else t[v] = t[v * 2 + 1]; } } void push(long long v) { t[v * 2].first += lazy[v]; lazy[v * 2] += lazy[v]; t[v * 2 + 1].first += lazy[v]; lazy[v * 2 + 1] += lazy[v]; lazy[v] = 0; } void update(long long v, long long tl, long long tr, long long l, long long r, long long addend) { if (l > r) return; if (l == tl && tr == r) { t[v].first += addend; lazy[v] += addend; } else { push(v); long long tm = (tl + tr) / 2; update(v * 2, tl, tm, l, min(r, tm), addend); update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, addend); if (t[v * 2].first < t[v * 2 + 1].first || (t[v * 2].first == t[v * 2 + 1].first && t[v * 2].second > t[v * 2 + 1].second)) t[v] = t[v * 2]; else t[v] = t[v * 2 + 1]; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; long long i = 1; while (i <= n) cin >> a[i++]; build(1, 1, n); i = 1; long long x; while (i <= n) { x = t[1].second; ans[x] = i; update(1, 1, n, x + 1, n, -i); update(1, 1, n, x, x, 1e13); i++; } i = 1; while (i <= n) { cout << ans[i] << " "; i++; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long good_rand() { return rand() * RAND_MAX + rand(); } template <class A> class myvector : public std::vector<A> { public: A &operator[](int pos) { if (pos < 0) pos = 0; while (this->size() <= pos) { this->push_back({}); } return std::vector<A>::operator[](pos); } void print(bool b = false) { unsigned int si = this->size(); if (b) std::cout << si << std::endl; for (unsigned int i = 0; i < si; ++i) { std::cout << this->operator[](i) << " "; } } void printendl(bool b = false) { print(b); std::cout << std::endl; } void good_random_shuffle() { unsigned int si = this->size(); for (unsigned int i = 0; i < si - 1; ++i) { unsigned int j = good_rand() % (si - i); j += i; swap(this->operator[](i), this->operator[](j)); } } template <class B> void read(B &n, bool b = false) { if (b) std::cin >> n; for (int i = 0; i < n; ++i) { std::cin >> this->operator[](i); } } }; template <class A, class B = std::less<typename std::vector<A>::value_type> > class mypriority_queue : public std::priority_queue<A, std::vector<A>, B> { public: A top() const { if (this->empty()) return {}; return std::priority_queue<A, std::vector<A>, B>::top(); } void pop() { if (this->empty()) return; std::priority_queue<A, std::vector<A>, B>::pop(); } A take() { A a = this->top(); this->pop(); return a; } void print(bool b = false) const { while (!this->empty()) { std::cout << take() << " "; } } void printendl(bool b = false) const { print(b); std::cout << std::endl; } }; class modll { static const long long mod = 1000000007; long long modvalue = 0; public: modll() { this->modvalue = 0; } modll(int x) { this->modvalue = x % mod; } modll(long long x) { this->modvalue = x % mod; } modll(const modll &x) { this->modvalue = x.modvalue; } long long getmodvalue() const { return modvalue; } static long long modpow(long long z, long long u) { if (u == 0) return 1; if (u == 1) return z; if (u % 2 == 0) { long long sa = modpow(z, u / 2); return (sa * sa) % mod; } return (z * modpow(z, u - 1)) % mod; } static modll fastpow(long long z, long long u) { modll a = modpow(z, u); return a; } static modll fact(long long z) { modll a = 1; for (long long i = 2; i <= z; ++i) { a *= i; } return a; } static modll choose(long long n, long long k) { modll a = 1; if (k > n / 2) return choose(n, n - k); for (long long i = n; n - i + 1 <= k; --i) { a *= i; } for (long long i = 2; i <= k; ++i) { a /= i; } return a; } long long rec() const { return modll::modpow(modvalue, mod - 2); } modll operator+(const modll &x) const { modll a; a.modvalue = (this->modvalue + x.modvalue) % mod; return a; } modll operator-(const modll &x) const { modll a; a.modvalue = (mod + this->modvalue - x.modvalue) % mod; return a; } modll operator*(const modll &x) const { modll a; a.modvalue = (this->modvalue * x.modvalue) % mod; return a; } modll operator/(const modll &x) const { modll a; if (x.modvalue == 0) return a; a.modvalue = (this->modvalue * x.rec()) % mod; return a; } template <class A> modll operator+(A x) const { if (x < 0) return this->operator-(-x); modll a; a.modvalue = (this->modvalue + x % mod) % mod; return a; } template <class A> modll operator-(A x) const { if (x < 0) return this->operator+(-x); modll a; a.modvalue = (this->modvalue - (x % mod) + mod) % mod; return a; } template <class A> modll operator*(A x) const { if (x < 0) { long long y = x / mod; y *= mod; y = x - y; x = y; } x %= mod; modll a; a.modvalue = (this->modvalue * x) % mod; return a; } template <class A> modll operator/(A x) const { if (x < 0) { long long y = x / mod; y *= mod; y = x - y; x = y; } x %= mod; modll a; if (x == 0) return a; modll b = x; a = *this / b; return a; } void operator=(modll x) { this->modvalue = x.modvalue; } template <class A> void operator=(A x) { this->modvalue = x % mod; } bool operator==(modll x) const { return (this->modvalue == x.modvalue); } template <class A> bool operator==(A x) const { return (this->modvalue == x); } bool operator<(modll x) const { return (this->modvalue < x.modvalue); } template <class A> bool operator<(A x) const { return (this->modvalue < x); } bool operator>(modll x) const { return (this->modvalue > x.modvalue); } template <class A> bool operator>(A x) { return (this->modvalue > x); } bool operator<=(modll x) const { return (this->modvalue <= x.modvalue); } template <class A> bool operator<=(A x) const { return (this->modvalue <= x); } bool operator>=(modll x) const { return (this->modvalue >= x.modvalue); } template <class A> bool operator>=(A x) const { return (this->modvalue >= x); } void operator+=(const modll &x) { this->modvalue += x.modvalue; this->modvalue %= mod; } template <class A> void operator+=(A x) { if (x < 0) this->operator+=(-1 * x); this->modvalue += x % mod; if (modvalue < 0) { this->modvalue *= -1; if (modvalue % mod == 0) modvalue = 0; else modvalue = mod - (modvalue % mod); } this->modvalue %= mod; } void operator-=(const modll &x) { this->modvalue -= x.modvalue; while (this->modvalue < 0) this->modvalue += mod; this->modvalue %= mod; } template <class A> void operator-=(A x) { if (x < 0) this->operator+=(-1 * x); this->modvalue -= x % mod; if (modvalue < 0) { this->modvalue *= -1; if (modvalue % mod == 0) modvalue = 0; else modvalue = mod - (modvalue % mod); } this->modvalue %= mod; } void operator*=(const modll &x) { this->modvalue *= x.modvalue; this->modvalue %= mod; } template <class A> void operator*=(A x) { bool neg = false; if (x < 0) { neg = true; x *= -1; } this->modvalue *= x % mod; if (modvalue < 0) { this->modvalue *= -1; if (modvalue % mod == 0) modvalue = 0; else modvalue = mod - (modvalue % mod); } this->modvalue %= mod; if (neg) { this->modvalue = mod - modvalue; this->modvalue %= mod; } } void operator/=(const modll &x) { if (x.modvalue == 0) { this->modvalue = 0; return; } this->modvalue = this->modvalue / x.modvalue; this->modvalue %= mod; } template <class A> void operator/=(A x) { bool neg = false; if (x < 0) { neg = true; x *= -1; } modll se = x % mod; this->operator/=(se); if (modvalue < 0) { this->modvalue *= -1; if (modvalue % mod == 0) modvalue = 0; else modvalue = mod - (modvalue % mod); } this->modvalue %= mod; if (neg) { this->modvalue = mod - modvalue; this->modvalue %= mod; } } modll operator++() { modvalue++; if (modvalue == mod) modvalue = 0; } modll operator--() { modvalue--; if (modvalue < 0) modvalue += mod; } void print() const { std::cout << this->modvalue; } void printspace() const { std::cout << this->modvalue << " "; } void printendl() const { std::cout << this->modvalue << std::endl; } }; set<long long> pos; set<long long> t; myvector<long long> v; myvector<long long> w; long long n, m, k, l, r, x, y; mypriority_queue<long long> pq; const long long N = 200005; long long a[2 * N]; long long b[2 * N]; long long c[2 * N]; long long from[2 * N]; long long to[2 * N]; long long childa(long long z) { return (z * 2) + 1; } long long childb(long long z) { return (z * 2) + 2; } long long parent(long long z) { if (z == 0) return -1; return (z - 1) / 2; } void update(long long u) { if (u < 0) return; if (childa(u) < 2 * m - 1) b[u] = (b[childa(u)] + c[childa(u)] + b[childb(u)] + c[childb(u)]); else b[u] = a[m - 1]; if (u != 0) update(parent(u)); } void add(long long l, long long r, long long z, long long u) { if (from[u] == l && to[u] == r) { c[u] += z; update(u); } else { if (to[childa(u)] >= l) { add(l, min(r, to[childa(u)]), z, childa(u)); } if (from[childb(u)] <= r) { add(max(l, from[childb(u)]), r, z, childb(u)); } } } long long getvalue(long long l, long long r, long long u) { long long sa = 0; long long sb = 0; if (from[u] == l && to[u] == r) { return c[u] + b[u]; } if (to[childa(u)] >= l) { sa = getvalue(l, min(r, to[childa(u)]), childa(u)); } if (from[childb(u)] <= r) { sb = getvalue(max(l, from[childb(u)]), r, childb(u)); } return c[u] + sa + sb; } const int SIZE = 400005; long long A[SIZE]; long long sum(long long i) { long long sum = 0; while (i > 0) sum += A[i], i -= ((i) & -(i)); return sum; } void add(long long i, long long k) { while (i < SIZE) A[i] += k, i += ((i) & -(i)); } int main() { std::ios_base::sync_with_stdio(false); v.read(n, true); w[n - 1] = 0; for (int i = 1; i <= n; ++i) add(i, i); for (int i = 1; i <= n; ++i) t.insert(i); for (int i = n - 1; i >= 0; --i) { long long small = 0; long long big = n; while (small != big) { int mid = small + big; mid /= 2; if (sum(mid) < v[i]) small = mid + 1; else big = mid; } x = *t.upper_bound(small); add(x, -x); t.erase(x); w[i] = x; } w.print(false); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.TreeSet; public class Solve3 { public static void main(String[] args) throws IOException { PrintWriter pw = new PrintWriter(System.out); new Solve3().solve(pw); pw.flush(); pw.close(); } public void solve(PrintWriter pw) throws IOException { FastReader sc = new FastReader(); int n = sc.nextInt(); long[] s = new long[n]; TreeSet<Integer> ts = new TreeSet(); for (int i = 0; i < n; i++) { s[i] = sc.nextLong(); ts.add(i + 1); } SegmentTree st = new SegmentTree(n); int[] p = new int[n]; for (int i = n - 1; i >= 0; i--) { int l = 1, h = n; while (l <= h) { int mid = (l + h) >> 1; long sum = st.query(1, n, 1, 1, mid); if (sum > s[i]) { h = mid - 1; } else { l = mid + 1; } } int x = ts.ceiling(l); ts.remove(x); p[i] = x; st.update(1, n, 1, x); } pw.print(p[0]); for (int i = 1; i < n; i++) { pw.print(" " + p[i]); } pw.println(); } class SegmentTree { long[] t; public SegmentTree(int n) { t = new long[n << 2]; build(1, n, 1); } public void build(int l, int r, int node) { if (l == r) { t[node] = l; return; } int mid = (l + r) >> 1; build(l, mid, node * 2); build(mid + 1, r, node * 2 + 1); t[node] = t[node * 2] + t[node * 2 + 1]; } public long query(int left, int right, int node, int l, int r) { if (l <= left && right <= r) { return t[node]; } if (l > right || r < left) { return 0; } int mid = (left + right) >> 1; long x = query(left, mid, node * 2, l, r); long y = query(mid + 1, right, node * 2 + 1, l, r); return x + y; } public void update(int left, int right, int node, int index) { if (left == right && left == index) { t[node] = 0; return; } int mid = (left + right) >> 1; if (index <= mid) { update(left, mid, node * 2, index); } else { update(mid + 1, right, node * 2 + 1, index); } t[node] = t[node * 2] + t[node * 2 + 1]; } } static public class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { if (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 double nextDouble() throws IOException { return Double.parseDouble(next()); } public String nextLine() throws IOException { return br.readLine(); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n; long long s[200005], p[200005]; long long t[200005]; long long lowbit(long long x) { return x & (-x); if (14209 > 9243) { if (31558 > 31075) { bool ZENBDJUSHQ; ZENBDJUSHQ = 28821; if (21853 > 13000) { if (11380 > 20609) { int TNKUVWEIHB; int UJWVDIONEM; } if (5029 > 1837) { long long OZJTULNDAW; short YUTAUSEEWT; short YXRPTHHAJU; } if (2054 > 19771) { bool WLDOHJWIET; int VWGFIBINCA; } } } } } void Add(long long x, long long v) { for (long long i = x; i <= n; i += lowbit(i)) t[i] += v; double ARKMGILIPN; return; bool GDFJDYLEIO; GDFJDYLEIO = 10065; } long long Sum(long long x) { long long sum = 0; double UQDXFBTCAN; for (long long i = x; i >= 1; i -= lowbit(i)) sum += t[i]; if (18949 > 18458) { if (14374 > 21712) { int WCHPQHSLWV; WCHPQHSLWV = 2964; if (WCHPQHSLWV) { short ZXHACSVHHM; if (18954 > 31325) { bool FMPKHFGCRD; long long YJBZSHKCBM; long long ZEDQQFWOJD; } double JLHPFGBHZG; } long long ONYRGSYOVI; ONYRGSYOVI = 31043; double PYNDFOTLPO; } if (9140 > 25442) { if (27549 > 330) { if (11689 > 10410) { bool EIDMTSXEVP; long long VDGUHIGORW; short LVGBKIEMWO; } long long GOCRHCMVKN; GOCRHCMVKN = 18506; } if (3433 > 18363) { if (9590 > 3934) { double ADAWDPHVWQ; int ZZFLJENXXA; int YVKXBGKFAW; int YENRQKDEMC; } if (24567 > 699) { bool JKTZQVQNVI; short KDYQAMAEXV; } } bool FZXPWJHKLT; } } return sum; bool LXXTNIQSIQ; LXXTNIQSIQ = 16503; if (LXXTNIQSIQ) { bool SGABGODWLS; SGABGODWLS = 3660; if (SGABGODWLS) { double EKGJBRZCHE; EKGJBRZCHE = 21905; short GHUHPBLOLN; GHUHPBLOLN = 19589; } if (905 > 23769) { double BZEOTAPENM; short ADIXHUIDFN; } if (6283 > 12100) { long long TLCVCFZOPS; } } } long long find(long long x) { long long l = 1, r = n, best = 0; short NSUMYJFLQG; NSUMYJFLQG = 8388; if (NSUMYJFLQG) { double LAMCGVSZJZ; LAMCGVSZJZ = 8066; if (LAMCGVSZJZ) { long long PHXVHXWSMU; PHXVHXWSMU = 19588; if (PHXVHXWSMU) { int FTXDMLNTIJ; FTXDMLNTIJ = 20885; if (FTXDMLNTIJ) { short ZYKWENLPEM; double MJAKMQZDHK; } short PKIDBRUHHW; PKIDBRUHHW = 2614; if (PKIDBRUHHW) { short PVKKZCKHBO; } if (13597 > 29011) { short ZYYRZRACEQ; } if (30081 > 3983) { short NLQVBZSSLC; short PPWPECFVVD; } } } bool PPFAIENCTH; PPFAIENCTH = 12546; } while (l <= r) { long long mid = (l + r) / 2; if (19539 > 25798) { double WKPBFCJGGF; int JAISCSLFHH; JAISCSLFHH = 7526; if (JAISCSLFHH) { short GXNFUDMIED; GXNFUDMIED = 30340; if (GXNFUDMIED) { bool PHHVBBDNXT; double ZYRWVCKZNG; } if (9243 > 16638) { long long UVAYWQMWJE; } bool UYQTSPIOBQ; UYQTSPIOBQ = 9384; } if (18654 > 8057) { short GJMTBVKSNW; GJMTBVKSNW = 26968; if (GJMTBVKSNW) { int MKFLTHYAHJ; int YTTWCIDRLC; } } if (26324 > 9562) { long long HLHVJACVDE; HLHVJACVDE = 532; } double KEACFCSIPN; KEACFCSIPN = 8567; } if (Sum(mid) <= x) best = mid, l = mid + 1; else r = mid - 1; bool VXIMTSMKZH; VXIMTSMKZH = 4146; } return best; if (27270 > 30192) { double FYQYPYIZBE; FYQYPYIZBE = 13300; if (FYQYPYIZBE) { int BUAQUSLGGX; BUAQUSLGGX = 25559; } long long VUTCCPUHGI; VUTCCPUHGI = 19065; if (VUTCCPUHGI) { if (14907 > 12825) { bool ZVGIJBPWBI; ZVGIJBPWBI = 28220; if (ZVGIJBPWBI) { long long GOUDEVPCUS; } if (2301 > 28031) { bool GBGHNTABIY; bool NYOFNYFHZY; } if (5470 > 25957) { double DDHWUDBHBQ; double XWKBQIMRVB; bool LGDBSVJAUQ; } bool TKHDXSCCZT; TKHDXSCCZT = 629; if (TKHDXSCCZT) { long long DUOXDVKMWC; double DQRMRUGRBX; int KBVEIFAHYN; } if (11770 > 25280) { short CQROWDYDBB; } } if (14459 > 16830) { short YKMCFIIAUC; YKMCFIIAUC = 18746; if (3989 > 25073) { long long EVOJKKFNFD; bool UTFZMYOPJE; long long SXPTTBRECO; double HCACGIYWSI; } bool XBMKBFEECL; if (8235 > 26698) { long long RDFOXJMTVY; double CRFJGTKOGJ; double LHYMZCOWSV; } } } } } int main() { scanf("%lld", &n); if (12780 > 22489) { bool ZCGTBFPGCT; if (31745 > 30661) { if (16855 > 19706) { if (6813 > 27906) { bool BDLPUHCGPZ; } if (17716 > 30112) { long long CPDWYHUXET; double ECYJHIHUYI; } long long YHKYVAVFSC; YHKYVAVFSC = 31968; } if (29265 > 4049) { int CQGKXISAEK; CQGKXISAEK = 24895; if (CQGKXISAEK) { bool HCQEYAGTSQ; long long JANTIWMHWD; long long MGVVEAKYMX; } if (24687 > 18276) { bool HKJEKCZGXJ; short WHMCKELLCQ; bool ZLYREOGAEL; } } if (4956 > 9981) { if (30989 > 19101) { long long HDNQSSLRVV; } short FWZWSVYEHD; FWZWSVYEHD = 18822; short VKBATGOHEU; VKBATGOHEU = 3131; if (VKBATGOHEU) { double YELJWTCJCL; long long RLHTACRWVG; double MEAVPENMTC; long long KMUCQDSYUH; } long long YXPNUTBPJG; } } } for (long long i = 1; i <= n; i++) scanf("%lld", &s[i]); short CQZNJNYJTN; CQZNJNYJTN = 24204; for (long long i = 1; i <= n; i++) Add(i, i); short NBDMGXUMGA; NBDMGXUMGA = 6446; if (NBDMGXUMGA) { int VBAKBENTED; VBAKBENTED = 30605; double JHILCUMRPQ; long long YKRRTOSFOC; YKRRTOSFOC = 4532; } for (long long i = n; i >= 1; i--) { long long now = find(s[i]) + 1; long long ALOXIXBCDP; ALOXIXBCDP = 30259; p[i] = now; if (6231 > 15348) { bool SNQBILZKZV; SNQBILZKZV = 9882; } Add(now, -now); if (20211 > 26895) { double AMXOCTKDVN; short POYHERVAAD; POYHERVAAD = 22794; } } for (long long i = 1; i <= n; i++) printf("%lld ", p[i]); if (11139 > 24245) { if (31075 > 14249) { bool VXYOAAFRPE; } if (30404 > 16859) { if (6719 > 24136) { int JZYOWUJYVO; JZYOWUJYVO = 13997; if (JZYOWUJYVO) { short JGKAZNEOSJ; } double ZIZXCRMYJJ; ZIZXCRMYJJ = 14485; if (ZIZXCRMYJJ) { int IKFRLBQAOD; int IPLSQGFXRO; int URGXSDTNYF; } } if (22449 > 4321) { long long KQOXESIYRM; KQOXESIYRM = 23208; if (23812 > 5143) { bool BREVDBAVPF; int LMDKUOZGRM; } if (12363 > 27150) { double RAVHNLTWBI; short QXWRRIDQZE; } if (10441 > 3829) { short FOPYPNXIEC; long long VDTXLTZIQA; } } int HWVDHWVNRU; } int JOIUZSHBQD; JOIUZSHBQD = 10785; } printf("\n"); double QPMGPDWNPP; QPMGPDWNPP = 8018; return 0; if (2682 > 13987) { long long HKOIUGGXUN; HKOIUGGXUN = 1996; if (HKOIUGGXUN) { short CRQZREGLKN; CRQZREGLKN = 25479; if (CRQZREGLKN) { double NLEIVMKSEN; if (25751 > 5300) { double XFJUHXKDLM; bool XWCVCCHXKF; } int GTJSUVKGBL; GTJSUVKGBL = 16027; if (GTJSUVKGBL) { int XDOIHZMHBS; bool MFNQOMGNZM; int GLGNMAJTNO; double DFGGGITLQL; } } if (31529 > 10708) { if (19626 > 4272) { long long ERRWRVJEKW; bool LTKQTNBHJT; short GOJMATLKUP; } int JDLZPPCWHD; JDLZPPCWHD = 15489; if (JDLZPPCWHD) { long long SYNEQEVSQQ; } } if (20640 > 3925) { long long FCMSKKBEER; FCMSKKBEER = 16970; if (FCMSKKBEER) { int SFFWMRFYCQ; int NZYEEPYPDM; double UAFAKDISGU; } short BQNSJWWJGR; if (19093 > 13597) { double EKFLWHBJMS; long long UITRVPCKMF; bool XRUTCVXATC; short GGJRRDHHBG; } short CIWYDTZODJ; CIWYDTZODJ = 1312; if (CIWYDTZODJ) { long long TPKHLUEEUM; double GECEIJUTLQ; short ASHAQRBZUC; bool GEBCKXQVJT; short AAMYLEWKFX; } } int GTCYNMPZRO; GTCYNMPZRO = 7257; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.util.*; public class test { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); // List<Integer> sr = Arrays.asList(0, 1, 1, 1, 10); // int n = 5; long[] arr = new long[n+1]; long[] fw = new long[n+1]; for(int i=1;i<=n;++i) { arr[i] = s.nextLong(); } for(int i=1;i<=n;++i) fw[i] = 0; for(int i=1;i<=n;++i) { fw[i] += i; if((i + (i & (-i))) <= n) fw[i + (i & (-i))] += fw[i]; } for(int i=n; i>=1; --i) { int l = 0, r = n; while(l < r) { int mid = (l + r )/2; long tp = 0; int j = mid; while(j > 0) { tp += fw[j]; j -= j&(-j); } // System.out.println(l + " " + r + " " + mid + " " + tp); if(tp <= arr[i]) l = mid + 1; else r = mid ; } arr[i] = l; addfw(l, -l, n, fw); } for(int i=1; i<=n; ++i) System.out.print(arr[i] + " "); } private static void addfw(int a, int b, int n, long[] fw) { while((a) <= n) { fw[a] += b; a += a&(-a); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1, c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f ^= 1; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return f ? x : -x; } struct BIT { long long sum[200005]; void upd(int x, int v) { for (; x < 200005; x += x & -x) sum[x] += v; } long long qry(int x) { long long ans = 0; for (; x; x -= x & -x) ans += sum[x]; return ans; } } bit; int n; long long s[200005]; int ans[200005]; int main() { n = read(); for (int i = 1; i <= n; ++i) scanf("%lld", s + i); for (int i = 1; i <= n; ++i) bit.upd(i, i); for (int i = n; i; --i) { int lb = 1, rb = n; while (lb < rb) { int md = (lb + rb + 1) >> 1; if (bit.qry(md - 1) <= s[i]) lb = md; else rb = md - 1; } ans[i] = lb; bit.upd(ans[i], -ans[i]); } for (int i = 1; i <= n; ++i) printf("%d%c", ans[i], i == n ? 10 : 32); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; public class D { // Modify the following 5 methods to implement your custom operations on the tree. // This example implements Add/Max operations. Operations like Add/Sum, Set/Max can also be implemented. long modifyOperation(long x, long y) { return x + y; } // query (or combine) operation long queryOperation(long leftValue, long rightValue) { return Math.min(leftValue, rightValue); } long deltaEffectOnSegment(long delta, int segmentLength) { // Here you must write a fast equivalent of following slow code: // int result = delta; // for (int i = 1; i < segmentLength; i++) result = queryOperation(result, delta); // return result; return delta; } long getNeutralDelta() { return 100000L*100000L*100000L; } long getInitValue() { return getNeutralDelta(); } // generic code int n; public static long[] a; long[] value; long[] delta; // delta[i] affects value[i], delta[2*i+1] and delta[2*i+2] long joinValueWithDelta(long value, long delta) { if (delta == getNeutralDelta()) return value; return modifyOperation(value, delta); } long joinDeltas(long delta1, long delta2) { if (delta1 == getNeutralDelta()) return delta2; if (delta2 == getNeutralDelta()) return delta1; return modifyOperation(delta1, delta2); } void pushDelta(int root, int left, int right) { value[root] = joinValueWithDelta(value[root], deltaEffectOnSegment(delta[root], right - left + 1)); if(left!=right) { delta[2 * root + 1] = joinDeltas(delta[2 * root + 1], delta[root]); delta[2 * root + 2] = joinDeltas(delta[2 * root + 2], delta[root]); } delta[root] = getNeutralDelta(); } public D(int n) { this.n = n; value = new long[4 * n]; delta = new long[4 * n]; init(0, 0, n - 1); } void init(int root, int left, int right) { if (left == right) { value[root] = a[left]; delta[root] = getNeutralDelta(); } else { int mid = (left + right) >> 1; init(2 * root + 1, left, mid); init(2 * root + 2, mid + 1, right); value[root] = queryOperation(value[2 * root + 1], value[2 * root + 2]); delta[root] = getNeutralDelta(); } } public long query(int from, int to) { return query(from, to, 0, 0, n - 1); } public int indexOfMin() { int start = 0; int left = 0; int right = n-1; pushDelta(start, left, right); while(left != right) { pushDelta(2*start+1, left, (left+right)/2); pushDelta(2*start+2, (left+right)/2+1, right); if(value[2*start+1] < value[2*start+2]) { start=2*start+1; right = (left+right)/2; } else { start=2*start+2; left=(left+right)/2+1; } } return left; } long query(int from, int to, int root, int left, int right) { if (from == left && to == right) return joinValueWithDelta(value[root], deltaEffectOnSegment(delta[root], right - left + 1)); pushDelta(root, left, right); int mid = (left + right) >> 1; if (from <= mid && to > mid) return queryOperation( query(from, Math.min(to, mid), root * 2 + 1, left, mid), query(Math.max(from, mid + 1), to, root * 2 + 2, mid + 1, right)); else if (from <= mid) return query(from, Math.min(to, mid), root * 2 + 1, left, mid); else if (to > mid) return query(Math.max(from, mid + 1), to, root * 2 + 2, mid + 1, right); else throw new RuntimeException("Incorrect query from " + from + " to " + to); } public void modify(int from, int to, long delta) { modify(from, to, delta, 0, 0, n - 1); } void modify(int from, int to, long delta, int root, int left, int right) { if (from == left && to == right) { this.delta[root] = joinDeltas(this.delta[root], delta); return; } pushDelta(root, left, right); int mid = (left + right) >> 1; if (from <= mid) modify(from, Math.min(to, mid), delta, 2 * root + 1, left, mid); if (to > mid) modify(Math.max(from, mid + 1), to, delta, 2 * root + 2, mid + 1, right); value[root] = queryOperation( joinValueWithDelta(value[2 * root + 1], deltaEffectOnSegment(this.delta[2 * root + 1], mid - left + 1)), joinValueWithDelta(value[2 * root + 2], deltaEffectOnSegment(this.delta[2 * root + 2], right - mid))); } public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(bf.readLine()); StringTokenizer st = new StringTokenizer(bf.readLine()); a = new long[n]; for(int i=0; i<n; i++) a[i] = Long.parseLong(st.nextToken()); // int n = Integer.parseInt(st.nextToken()); D segtree = new D(n); int[] ans = new int[n]; int cur = 1; for(int i=0; i<n; i++) { int index = segtree.indexOfMin(); // out.println(":"+index+ " " + segtree.query(index, index)); ans[index] = cur; // out.println(index); if(index+1 <= n-1) segtree.modify(index+1, n-1, -1*cur); segtree.modify(index, index, 1L*10000*10000*10000); cur++; } StringBuilder anss = new StringBuilder(); for(int i=0; i<n; i++) anss.append(ans[i]+" "); out.println(anss.toString()); out.close(); System.exit(0); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
/*Author: Satyajeet Singh, Delhi Technological University*/ import java.io.*; import java.util.*; import java.text.*; import java.lang.*; import java.math.*; public class Main{ /*********************************************Constants******************************************/ static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static long mod=(long)1e9+7; static long mod1=998244353; static boolean sieve[]; static ArrayList<Integer> primes; static ArrayList<Long> factorial; static HashSet<Pair> graph[]; static boolean oj = System.getProperty("ONLINE_JUDGE") != null; /****************************************Solutions Begins***************************************/ static class segmentTree{ int n=0; int[] lo,hi; long[] value,delta; int idx[]; segmentTree(int n){ this.n=n; lo=new int[8*n+1]; hi=new int[8*n+1]; value=new long[8*n+1]; delta=new long[8*n+1]; init(1,1,n); idx=new int[8*n+1]; } void init(int i,int left,int right){ lo[i]=left; hi[i]=right; if(left==right){ return; } int mid=(left+right)/2; init(2*i,left,mid); init(2*i+1,mid+1,right); } void update(int left,int right,long val){ update(1,left,right,val); } Pairl query(int left,int right){ return query(1,left,right); } void prop(int i){ delta[2*i]+=delta[i]; delta[2*i+1]+=delta[i]; delta[i]=0; } void update(int i){ long a=value[2*i]+delta[2*i]; long b=value[2*i+1]+delta[2*i+1]; if(a<=b){ value[i]=a; if(a==b){ idx[i]=Math.max(idx[2*i],idx[2*i+1]); } else{ idx[i]=idx[2*i]; } } else{ value[i]=b; idx[i]=idx[2*i+1]; } } void update(int i,int left,int right,long val){ if(left>hi[i]||right<lo[i]){ return; } if(lo[i]>=left&&hi[i]<=right){ delta[i]+=val; value[i]+=delta[i]; if(left==right){ delta[i]=0; idx[i]=left; return; } prop(i); return; } prop(i); update(2*i,left,right,val); update(2*i+1,left,right,val); update(i); } Pairl query(int i,int left,int right){ if(left>hi[i]||right<lo[i]){ return new Pairl(Long.MAX_VALUE,-1); } if(lo[i]>=left&&hi[i]<=right){ value[i]+=delta[i]; prop(i); return new Pairl(value[i],idx[i]); } prop(i); Pairl l=query(2*i,left,right); Pairl r=query(2*i+1,left,right); update(i); if(l.u<=r.u){ if(l.u==r.u){ return new Pairl(l.u,Math.max(r.v,l.v)); } else{ return l; } } else{ return r; } } } public static void main (String[] args) throws Exception { String st[]=nl(); int n=pi(st[0]); long input[]=new long[n]; st=nl(); segmentTree seg=new segmentTree(n); for(int i=0;i<n;i++){ input[i]=pl(st[i]); seg.update(i+1,i+1,input[i]); } // debug(seg.query(1,n)); // debug(seg.lo); // debug(seg.hi); // debug(seg.value); // debug(seg.idx); int output[]=new int[n]; for(int i=1;i<=n;i++){ Pairl p=seg.query(1,n); int idx=(int)p.v; output[idx-1]=i; seg.update(idx,idx,Long.MAX_VALUE/2); if(idx+1<=n) seg.update(idx+1,n,-i); // debug(i); // debug(seg.lo); // debug(seg.hi); // debug(seg.value); // debug(seg.idx); // debug(seg.delta); // out.println(); // for(int i=0;i<n;i++){ // out.print(seg.query(i+1,i+1)+" "); // } } for(int u:output){ out.print(u+" "); } /****************************************Solutions Ends**************************************************/ out.flush(); out.close(); } /****************************************Template Begins************************************************/ static String[] nl() throws Exception{ return br.readLine().split(" "); } static String[] nls() throws Exception{ return br.readLine().split(""); } static int pi(String str) { return Integer.parseInt(str); } static long pl(String str){ return Long.parseLong(str); } static double pd(String str){ return Double.parseDouble(str); } /***************************************Precision Printing**********************************************/ static void printPrecision(double d){ DecimalFormat ft = new DecimalFormat("0.00000000000000000"); out.println(ft.format(d)); } /**************************************Bit Manipulation**************************************************/ static void printMask(long mask){ System.out.println(Long.toBinaryString(mask)); } static int countBit(int mask){ int ans=0; while(mask!=0){ if(mask%2==1){ ans++; } mask/=2; } return ans; } /******************************************Graph*********************************************************/ static void Makegraph(int n){ graph=new HashSet[n]; for(int i=0;i<n;i++){ graph[i]=new HashSet<>(); } } static void addEdge(int a,int b,int c){ graph[a].add(new Pair(b,c)); } /*********************************************PAIR********************************************************/ static class PairComp implements Comparator<Pair>{ public int compare(Pair p1,Pair p2){ return -((p1.u+p1.v)-(p2.u+p2.v)); } } static class Pair implements Comparable<Pair> { int u; int v; int index=-1; public Pair(int u, int v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pair other = (Pair) o; return u == other.u && v == other.v; } public int compareTo(Pair other) { if(index!=other.index) return Long.compare(index, other.index); return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } /******************************************Long Pair*************************************************/ static class PairCompL implements Comparator<Pairl>{ public int compare(Pairl p1,Pairl p2){ long aa=p2.v-p1.v; if(aa<0){ return -1; } else if(aa>0){ return 1; } else{ return 0; } } } static class Pairl implements Comparable<Pairl> { long u; long v; int index=-1; public Pairl(long u, long v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pair other = (Pair) o; return u == other.u && v == other.v; } public int compareTo(Pairl other) { if(index!=other.index) return Long.compare(index, other.index); return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } /*****************************************DEBUG***********************************************************/ public static void debug(Object... o) { if(!oj) System.out.println(Arrays.deepToString(o)); } /************************************MODULAR EXPONENTIATION***********************************************/ static long modulo(long a,long b,long c) { long x=1; long y=a; while(b > 0){ if(b%2 == 1){ x=(x*y)%c; } y = (y*y)%c; // squaring the base b /= 2; } return x%c; } /********************************************GCD**********************************************************/ static long gcd(long x, long y) { if(x==0) return y; if(y==0) return x; long r=0, a, b; a = (x > y) ? x : y; // a is greater number b = (x < y) ? x : y; // b is smaller number r = b; while(a % b != 0) { r = a % b; a = b; b = r; } return r; } /******************************************SIEVE**********************************************************/ static void sieveMake(int n){ sieve=new boolean[n]; Arrays.fill(sieve,true); sieve[0]=false; sieve[1]=false; for(int i=2;i*i<n;i++){ if(sieve[i]){ for(int j=i*i;j<n;j+=i){ sieve[j]=false; } } } primes=new ArrayList<Integer>(); for(int i=0;i<n;i++){ if(sieve[i]){ primes.add(i); } } } /********************************************End***********************************************************/ }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int LIM = 1e5 + 5, MOD = 1e9 + 7; const int maxn = 1000006; long long solve(long long a, long long b) { if ((a == b) || (a == 0)) { return 0; } else { long long res; res = 1 + solve(a % (b / 2), b / 2); return res; } } long long ft[200005]; long long n; void update(long long i, long long diff) { for (; i <= n; i = i + (i & (-i))) { ft[i] += diff; } } long long query(long long i) { long long sum = 0; for (; i > 0; i = i - (i & (-i))) { sum += ft[i]; } return sum; } long long solve(long long z) { long long sum = 0, ret = 0, i; for (i = 20; i >= 0; i--) { if (((ret + (1 << i)) <= n) && ((ft[ret + (1 << i)] + sum) <= z)) { ret = ret + (1 << i); sum = sum + ft[ret]; } } return (ret + 1); } int main() { long long i, j, k, m; cin >> n; long long res[n + 5]; long long s[n + 5]; for (i = 1; i <= n; i++) ft[i] = 0; for (i = 1; i <= n; i++) { update(i, i); cin >> s[i]; } for (i = n; i >= 1; i--) { res[i] = solve(s[i]); update(res[i], -res[i]); } for (i = 1; i <= n; i++) { cout << res[i] << " "; } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f, N = 2e5 + 5; inline int read() { int sum = 0, f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') f = -f; c = getchar(); } while (c >= '0' && c <= '9') sum = sum * 10 + c - 48, c = getchar(); return sum * f; } int n, a[N]; long long pre[N], c[N], s[N], ans[N]; bool vis[N]; void add(int x, int v) { for (int i = x; i <= n; i += (i & -i)) c[i] += v; } long long query(int x) { long long ans = 0; for (int i = x; i > 0; i -= (i & -i)) ans += c[i]; return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> s[i]; for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + i - 1; for (int i = n; i >= 1; i--) { int l = 1, r = n, pos = 0; while (l <= r) { int mid = (l + r) >> 1; long long tmp = query(mid); if (pre[mid] - tmp > s[i]) r = mid - 1; else if (pre[mid] - tmp == s[i] && vis[mid]) l = mid + 1; else if (pre[mid] - tmp == s[i]) { pos = mid; break; } else l = mid + 1; } ans[i] = pos; vis[pos] = 1; add(pos + 1, pos); } for (int i = 1; i <= n; i++) cout << ans[i] << ' '; cout << '\n'; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize "O3" using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const int N = 2e5 + 5; const int M = 1 << 18; pair<long long, int> tree[M * 2 + 5]; long long lazy[M * 2 + 5]; int n, ans[N]; void push(int v) { tree[v * 2].first += lazy[v]; tree[v * 2 + 1].first += lazy[v]; lazy[v * 2] += lazy[v]; lazy[v * 2 + 1] += lazy[v]; lazy[v] = 0; } void add(int l, int r, long long val, int v = 1, int le = 1, int re = M - 1) { if (l > r || l > re || r < le) return; if (le >= l && re <= r) { tree[v].first += val; lazy[v] += val; return; } int mid = (le + re) / 2; push(v); add(l, r, val, v * 2, le, mid); add(l, r, val, v * 2 + 1, mid + 1, re); tree[v] = min(tree[v * 2], tree[v * 2 + 1]); } void solve() { int pom; cin >> n; for (int i = 1; i <= n; i++) { cin >> tree[i + M - 1].first; tree[i + M - 1].second = {-i}; } for (int i = M - 1; i >= 1; i--) tree[i] = min(tree[i * 2], tree[i * 2 + 1]); for (int i = 1; i <= n; i++) { pom = -tree[1].second; ans[pom] = i; add(pom, pom, INF); add(pom + 1, n, -i); } for (int i = 1; i <= n; i++) cout << ans[i] << ' '; cout << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using pii = pair<long long, int>; const int maxn = 2e5 + 5; int n; long long a[maxn], s[maxn], t[maxn << 2]; void build(int l, int r, int rt) { if (l == r) { t[rt] = l; return; } int m = l + r >> 1; build(l, m, rt << 1); build(m + 1, r, rt << 1 | 1); t[rt] = t[rt << 1] + t[rt << 1 | 1]; } void update(int pos, int l, int r, int rt) { if (l > pos || r < pos) return; if (l == r) { t[rt] = 0; return; } int m = l + r >> 1; update(pos, l, m, rt << 1); update(pos, m + 1, r, rt << 1 | 1); t[rt] = t[rt << 1] + t[rt << 1 | 1]; } int query(long long q, int l, int r, int rt) { if (l == r) return l; int m = l + r >> 1; if (t[rt << 1] > q) return query(q, l, m, rt << 1); else return query(q - t[rt << 1], m + 1, r, rt << 1 | 1); } void pri(int l, int r, int rt) { if (l == r) { printf("%lld ", t[rt]); return; } int m = l + r >> 1; pri(l, m, rt << 1); pri(m + 1, r, rt << 1 | 1); } int main() { scanf("%d", &n); build(1, n, 1); for (int i = 1; i <= n; i++) scanf("%lld", s + i); for (int i = n; i > 0; i--) { a[i] = query(s[i], 1, n, 1); update(a[i], 1, n, 1); } for (int i = 1; i <= n; i++) printf("%lld ", a[i]); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int ans[200005], n; long long a[200005], aib[200005]; inline void Update(int p, int val) { while (p <= n) { aib[p] += val; p += p & (-p); } } inline long long Sum(int p) { long long s = 0; while (p >= 1) { s += aib[p]; p -= p & (-p); } return s; } int main() { int left, right, mid, poz; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; Update(i, i); } for (int i = n; i >= 1; i--) { left = 1; right = n; while (left <= right) { mid = (left + right) / 2; if (Sum(mid) > a[i]) { poz = mid; right = mid - 1; } else left = mid + 1; } ans[i] = poz; Update(poz, -poz); } for (int i = 1; i <= n; i++) cout << ans[i] << " "; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:640000000") using namespace std; const double eps = 1e-9; const double pi = acos(-1.0); const int maxn = (int)2e5 + 10; long long a[maxn]; int res[maxn]; long long t[4 * maxn]; void build(int idx, int l, int r) { if (l == r) { t[idx] = l; } else { int m = (l + r) >> 1; build(2 * idx, l, m); build(2 * idx + 1, m + 1, r); t[idx] = t[2 * idx] + t[2 * idx + 1]; } } int get(int idx, int l, int r, long long sum) { if (l == r) return l; int m = (l + r) >> 1; if (t[2 * idx] > sum) { return get(2 * idx, l, m, sum); } return get(2 * idx + 1, m + 1, r, sum - t[2 * idx]); } void upd(int idx, int l, int r, int pos) { if (l == r) { t[idx] = 0; } else { int m = (l + r) >> 1; if (pos <= m) { upd(2 * idx, l, m, pos); } else { upd(2 * idx + 1, m + 1, r, pos); } t[idx] = t[2 * idx] + t[2 * idx + 1]; } } int main() { int n; cin >> n; for (int i = 0; i < n; i++) scanf("%I64d", &a[i]); build(1, 1, n); for (int i = n - 1; i >= 0; i--) { int val = get(1, 1, n, a[i]); res[i] = val; upd(1, 1, n, val); } for (int i = 0; i < n; i++) { printf("%d ", 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 " ] }
CORRECT
java
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=1; while(T-->0) { int n=input.nextInt(); long a[]=new long[n+1]; for(int i=1;i<=n;i++) { a[i]=input.nextLong(); } int ans[]=new int[n+1]; long arr[]=new long[n+1]; for(int i=1;i<=n;i++) { update(i,i,arr,n); } for(int i=n;i>=1;i--) { long s=a[i]; int x=1,y=n+1; while(x<y) { int mid=(x+y)/2; long sum=sum(mid,arr); if(sum<=s) { x=mid+1; } else { y=mid; } } ans[i]=x; update(-x,x,arr,n); } for(int i=1;i<=n;i++) { out.print(ans[i]+" "); } out.println(); } out.close(); } public static void update(int d,int k,long arr[],int n) { while(k<=n) { arr[k]+=d; k+=k&-k; } } public static long sum(int k,long arr[]) { long sum=0; while(k>0) { sum+=arr[k]; k-=k&-k; } return sum; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
/** * @author Finn Lidbetter */ import java.util.*; import java.io.*; import java.awt.geom.*; public class TaskD { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int n = Integer.parseInt(br.readLine()); long[] sums = new long[n]; String[] s = br.readLine().split(" "); for (int i=0; i<n; i++) { sums[i] = Long.parseLong(s[i]); } int[] ans = new int[n]; FT1 ft = new FT1(n); TreeSet<Integer> vis = new TreeSet<>(); for (int i=1; i<=n; i++) { vis.add(i); } for (int i=n-1; i>=0; i--) { int lo = 1; int hi = n; //System.out.println("Index: "+i); while (lo<=hi) { long mid = (lo+hi)/2; if (!vis.contains((int)mid)) { Integer hier = vis.higher((int)mid); if (hier!=null && hier<=hi) { mid = (int)hier; } else { Integer loer = vis.lower((int)mid); mid = (int)loer; } } long sub = ft.sum((int)mid-1); //System.out.printf("Trying: %d, sub is: %d\n", mid, sub); long val = (mid*(mid-1))/2 - sub; if (val==sums[i]) { ans[i] = (int)mid; break; } else if (val<sums[i]) { lo = (int)mid+1; } else { hi = (int)mid-1; } } ft.add(ans[i], ans[i]); vis.remove(ans[i]); } for (int i=0; i<n; i++) { sb.append(ans[i]); sb.append((i==n-1) ? "\n" : " "); } System.out.print(sb); } } class FT1 { long[] arr; FT1(int n) { arr = new long[n + 1]; } // Initialize with 0's FT1(long[] vals) { // Initialize using array, vals[0] is unused arr = (long[]) vals.clone(); for (int i = 1; i < arr.length; i++) { int j = i + (i & -i); if (j < arr.length) arr[j] += arr[i]; } } long sum(int i, int j) { // Get the sum of [i,j] return sum(j) - sum(i - 1); } long sum(int i) { // Get the sum of [1,i] long sum = 0; while (i > 0) { sum += arr[i]; i -= i & -i; } return sum; } void add(int i, int delta) { // Add delta to value at i if (i <= 0) return; while (i < arr.length) { arr[i] += delta; i += i & -i; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<long long> tree; vector<long long> s; vector<long long> p; long long build(long long L, long long R, long long v) { if (R - L == 1) { return tree[v] = L + 1; } else { return tree[v] = build(L, (L + R) / 2, v * 2 + 1) + build((L + R) / 2, R, v * 2 + 2); } } long long get(long long L, long long R, long long v, long long sum) { if (R - L == 1) { return L; } if (tree[v * 2 + 1] <= sum) { return get((L + R) / 2, R, v * 2 + 2, sum - tree[v * 2 + 1]); } else { return get(L, (L + R) / 2, v * 2 + 1, sum); } } long long sset(long long L, long long R, long long v, long long x) { if (R - L == 1) { return tree[v] = 0; } if (x < (L + R) / 2) { return tree[v] = sset(L, (L + R) / 2, v * 2 + 1, x) + tree[v * 2 + 2]; } else { return tree[v] = sset((L + R) / 2, R, v * 2 + 2, x) + tree[v * 2 + 1]; } } int32_t main() { long long N; cin >> N; tree.resize(N * 4); s.resize(N); p.resize(N); for (long long i = 0; i < N; i++) { cin >> s[i]; } build(0, N, 0); for (long long i = N - 1; i >= 0; i--) { p[i] = get(0, N, 0, s[i]); sset(0, N, 0, p[i]); p[i]++; } for (auto i : p) { cout << 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 " ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1000000000") using namespace std; const int maxn = (int)2e5 + 10; int n; long long v[maxn]; long long t[4 * maxn]; void change(int it, int l, int r, int pos, int x) { if (l == r) { t[it] = x; } else { int m = (l + r) >> 1; if (pos <= m) { change(2 * it, l, m, pos, x); } else { change(2 * it + 1, m + 1, r, pos, x); } t[it] = t[2 * it] + t[2 * it + 1]; } } int go(int it, int l, int r, long long s) { if (l == r) { return l; } int m = (l + r) >> 1; if (t[2 * it] > s) { return go(2 * it, l, m, s); } return go(2 * it + 1, m + 1, r, s - t[2 * it]); } int ans[maxn]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { scanf("%lld", &v[i]); } for (int i = 1; i <= n; i++) { change(1, 1, n, i, i); } for (int i = n - 1; i >= 0; i--) { int x = go(1, 1, n, v[i]); change(1, 1, n, x, 0); ans[i] = x; } for (int i = 0; i < n; i++) { printf("%d ", ans[i]); } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
//package educational.codefest19; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class D { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(); long[] a = new long[n]; for(int i = 0;i < n;i++){ a[i] = nl(); } long[] ft = new long[n+5]; for(int i = 0;i < n;i++){ addFenwick(ft, i+1, i+1); } int[] ret = new int[n]; for(int i = n-1;i >= 0;i--){ int pos = firstGEFenwick(ft, a[i] + 1); addFenwick(ft, pos, -pos); ret[i] = pos; } for(int i = 0;i < n;i++){ out.print(ret[i] + " " ); } out.println(); } public static long sumFenwick(long[] ft, int i) { long sum = 0; for(i++;i > 0;i -= i&-i)sum += ft[i]; return sum; } public static void addFenwick(long[] ft, int i, long v) { if(v == 0)return; int n = ft.length; for(i++;i < n;i += i&-i)ft[i] += v; } public static int firstGEFenwick(long[] ft, long v) { int i = 0, n = ft.length; for(int b = Integer.highestOneBit(n);b != 0;b >>= 1){ if((i|b) < n && ft[i|b] < v){ i |= b; v -= ft[i]; } } return i; } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new D().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
/** * BaZ :D */ import java.util.*; import java.io.*; import static java.lang.Math.*; import java.math.BigInteger; public class Main { static MyScanner scan; static PrintWriter pw; static long MOD = 998244353 ; static long INF = 1_000_000_000_000_000_000L; static long inf = 2_000_000_000; public static void main(String[] args) { new Thread(null,null,"BaZ",1<<25) { public void run() { try { solve(); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } static long BIT[]; static int n; static void solve() throws IOException { //initIo(true); initIo(false); StringBuilder sb = new StringBuilder(); n = ni(); long arr[] = new long[n]; BIT = new long[n+1]; for(int i=0;i<n;++i) { arr[i] = nl(); update(i+1, i+1); } //pl("scanned"); long ans[] = new long[n]; for(int i=n-1;i>=0;--i) { ans[i] = query_last(arr[i])+1; int val = (int)ans[i]; update(val, -val); } for(long e : ans) { sb.append(e); sb.append(" "); } pl(sb); pw.flush(); pw.close(); } static int query_last(long req) { int low = 0, high = n, mid,ans = 0; while(low<=high) { //pl("stuck"); mid = (low+high)>>1; if(query(mid)<=req) { ans = mid; low = mid+1; } else { high = mid-1; } } return ans; } static void update(int index,long val) { for(int i=index;i<=n;i+=i&-i) BIT[i]+=val; } static long query(int index) { long sum = 0; for(int i=index;i>0;i-=i&-i) sum+=BIT[i]; return sum; } static void initIo(boolean isFileIO) throws IOException { scan = new MyScanner(isFileIO); if(isFileIO) { pw = new PrintWriter("/Users/amandeep/Desktop/output.txt"); } else { pw = new PrintWriter(System.out, true); } } static int ni() throws IOException { return scan.nextInt(); } static long nl() throws IOException { return scan.nextLong(); } static double nd() throws IOException { return scan.nextDouble(); } static String ne() throws IOException { return scan.next(); } static String nel() throws IOException { return scan.nextLine(); } static void pl() { pw.println(); } static void p(Object o) { pw.print(o+" "); } static void pl(Object o) { pw.println(o); } static void psb(StringBuilder sb) { pw.print(sb); } static void pa(String arrayName, Object arr[]) { pl(arrayName+" : "); for(Object o : arr) p(o); pl(); } static void pa(String arrayName, int arr[]) { pl(arrayName+" : "); for(int o : arr) p(o); pl(); } static void pa(String arrayName, long arr[]) { pl(arrayName+" : "); for(long o : arr) p(o); pl(); } static void pa(String arrayName, double arr[]) { pl(arrayName+" : "); for(double o : arr) p(o); pl(); } static void pa(String arrayName, char arr[]) { pl(arrayName+" : "); for(char o : arr) p(o); pl(); } static void pa(String listName, List list) { pl(listName+" : "); for(Object o : list) p(o); pl(); } static void pa(String arrayName, Object[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(Object o : arr[i]) p(o); pl(); } } static void pa(String arrayName, int[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(int o : arr[i]) p(o); pl(); } } static void pa(String arrayName, long[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(long o : arr[i]) p(o); pl(); } } static void pa(String arrayName, char[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(char o : arr[i]) p(o); pl(); } } static void pa(String arrayName, double[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(double o : arr[i]) p(o); pl(); } } static class MyScanner { BufferedReader br; StringTokenizer st; MyScanner(boolean readingFromFile) throws IOException { if(readingFromFile) { br = new BufferedReader(new FileReader("/Users/amandeep/Desktop/input.txt")); } else { br = new BufferedReader(new InputStreamReader(System.in)); } } String nextLine()throws IOException { return br.readLine(); } String next() throws IOException { if(st==null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /* the last 0 in the sequence must be the 1 in the permutation cuz there cannot be any 0's after it, cuz they would be at least 1 so take the 1 and then subtract 1 from every element after then 2 must be the last 0 and so on find last 0 with segtree and binary search log^2(n) and do the range updates in segtree log(n) */ public class manthan19d { public static void main(String[] args) { FastScanner scan=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int n=scan.nextInt(); node root=new node(1,n); for(int i=1;i<=n;i++) { root.add(i,i,scan.nextLong()); } int[] res=new int[n]; for(int ct=1;ct<=n;ct++) { int lo=1, hi=n; while(hi>=lo) { int mid=(hi+lo)/2; if(root.min(mid,n)==0) { lo=mid+1; } else { hi=mid-1; } } if(hi+1<=n) root.add(hi+1,n,-ct); root.add(hi,hi,Long.MAX_VALUE/2); res[hi-1]=ct; } for(int i:res) { out.print(i); out.print(" "); } out.close(); } static class node { node l=null,r=null; int lo,hi; long delta,min; node(int lo, int hi) { this.lo=lo; this.hi=hi; delta=0L; min=0L; if(lo!=hi) { int m=(lo+hi)/2; l=new node(lo,m); r=new node(m+1,hi); } } void add(int nlo, int nhi, long add) { if(nlo<=lo&&hi<=nhi) { //covers entire interval delta+=add; return; } if(lo>nhi||hi<nlo) { //covers nothing return; } push(); l.add(nlo,nhi,add); r.add(nlo,nhi,add); update(); } long min(int nlo, int nhi) { if(nlo<=lo&&hi<=nhi) { //covers entire interval return min+delta; } if(lo>nhi||hi<nlo) { //covers nothing return Long.MAX_VALUE; } push(); long left=l.min(nlo,nhi); long right=r.min(nlo,nhi); update(); return Math.min(left,right); } void update() { min=Math.min(l.min+l.delta,r.min+r.delta); } void push() { l.delta+=delta; r.delta+=delta; delta=0; } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); } catch (Exception e){e.printStackTrace();} } public String next() { if (st.hasMoreTokens()) return st.nextToken(); try {st = new StringTokenizer(br.readLine());} catch (Exception e) {e.printStackTrace();} return st.nextToken(); } public int nextInt() {return Integer.parseInt(next());} public long nextLong() {return Long.parseLong(next());} public double nextDouble() {return Double.parseDouble(next());} public String nextLine() { String line = ""; if(st.hasMoreTokens()) line = st.nextToken(); else try {return br.readLine();}catch(IOException e){e.printStackTrace();} while(st.hasMoreTokens()) line += " "+st.nextToken(); return line; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int T; long long n; long long a[333333]; long long twosum[333333]; long long tree[(1LL << 18) * 3]; long long ans[333333]; set<long long> s; void update(long long x, long long y) { x += (1LL << 18); while (x > 0LL) { tree[x] += y; x /= 2LL; } } long long sum(int s, int e, int ns = 0, int ne = (1LL << 18) - 1, int idx = 1) { if (s > ne || ns > e) return 0LL; if (s <= ns && ne <= e) return tree[idx]; int mid = (ns + ne) / 2; return sum(s, e, ns, mid, idx * 2) + sum(s, e, mid + 1, ne, idx * 2 + 1); } int main() { scanf("%lld", &n); for (long long i = 1LL; i <= n; i++) { scanf("%lld", &a[i]); twosum[i] = twosum[i - 1] + i; s.insert(i); update(i, i); } for (long long i = n; i > 0; i--) { long long st = 1LL; long long ed = n; long long val; while (st <= ed) { long long mid = (st + ed) / 2LL; if (sum(1, (int)mid - 1) <= a[i]) { st = mid + 1LL; val = mid; } else ed = mid - 1LL; } val = *s.lower_bound(val); update(val, -val); s.erase(val); ans[i] = val; } for (int i = 1; i <= n; i++) printf("%d ", ans[i]); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, ans[200100]; long long s[200100]; pair<long long, int> seg[800100]; long long lazy[800100]; void build(int l, int r, int ind) { if (l == r) { seg[ind] = {s[l], l}; return; } int mid = (l + r) / 2; build(l, mid, ind * 2); build(mid + 1, r, ind * 2 + 1); if (seg[ind * 2].first < seg[ind * 2 + 1].first) seg[ind] = seg[ind * 2]; else if (seg[ind * 2].first > seg[ind * 2 + 1].first) seg[ind] = seg[ind * 2 + 1]; else { if (seg[ind * 2].second > seg[ind * 2 + 1].second) seg[ind] = seg[ind * 2]; else seg[ind] = seg[ind * 2 + 1]; } } void push(int ind) { lazy[ind * 2] += lazy[ind]; lazy[ind * 2 + 1] += lazy[ind]; lazy[ind] = 0; } void update(int l, int r, int x, int y, int ind, long long add) { if (x > y or l > y or r < x) return; if (l >= x and r <= y) { lazy[ind] += add; seg[ind].first += lazy[ind]; if (l != r) { lazy[ind * 2] += lazy[ind]; lazy[ind * 2 + 1] += lazy[ind]; } lazy[ind] = 0; return; } seg[ind].first += lazy[ind]; push(ind); int mid = (l + r) / 2; update(l, mid, x, y, ind * 2, add); update(mid + 1, r, x, y, ind * 2 + 1, add); if (seg[ind * 2].first + lazy[ind * 2] < seg[ind * 2 + 1].first + lazy[ind * 2 + 1]) seg[ind] = {seg[ind * 2].first + lazy[ind * 2], seg[ind * 2].second}; else if (seg[ind * 2].first + lazy[ind * 2] > seg[ind * 2 + 1].first + lazy[ind * 2 + 1]) seg[ind] = {seg[ind * 2 + 1].first + lazy[ind * 2 + 1], seg[ind * 2 + 1].second}; else { if (seg[ind * 2].second > seg[ind * 2 + 1].second) seg[ind] = {seg[ind * 2].first + lazy[ind * 2], seg[ind * 2].second}; else seg[ind] = {seg[ind * 2 + 1].first + lazy[ind * 2 + 1], seg[ind * 2 + 1].second}; } } pair<long long, int> calc(int l, int r, int ind) { seg[ind].first += lazy[ind]; if (l == r) { lazy[ind] = 0; return seg[ind]; } else push(ind); if (seg[ind].first > 0) return seg[ind]; int mid = (l + r) / 2; pair<long long, int> b = calc(mid + 1, r, ind * 2 + 1); if (b.first <= 0) return b; else return calc(l, mid, ind * 2); } int main() { memset(lazy, 0, sizeof(lazy)); cin >> n; for (int i = 1; i <= n; i++) cin >> s[i]; build(1, n, 1); for (int i = 1; i <= n; i++) { pair<long long, int> a = calc(1, n, 1); ans[a.second] = i; update(1, n, a.second + 1, n, 1, -i); update(1, n, a.second, a.second, 1, 1e12); } for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.util.*; import java.io.*; import java.text.*; public class Gymaya { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int N = 1; while(N < n) N <<= 1; //padding long[] in = new long[N + 1]; long inf= (long) 1e17; Arrays.fill(in,inf); for(int i = 1; i <= n; i++) in[i] = sc.nextLong(); SegmentTree sg = new SegmentTree(in); int[]ans= new int[n+1]; for (int i =1;i<=n;i++){ int id= sg.query(1,n); ans[id]=i; sg.update_range(id+1,n,-i); sg.update_range(id,id,inf); } for (int i =1;i<=n;i++){ pw.print(ans[i]+" "); } pw.println(); pw.flush(); } static class SegmentTree { // 1-based DS, OOP int N; //the number of elements in the array as a power of 2 (i.e. after padding) int[] sTree; long[] array,lazy; SegmentTree(long[] in) { array = in; N = in.length - 1; sTree = new int[N << 1]; //no. of nodes = 2*N - 1, we add one to cross out index zero lazy = new long[N << 1]; build(1, 1, N); } void build(int node, int b, int e) // O(n) { if (b == e) sTree[node] = b; else { int mid = b + e >> 1; build(node << 1, b, mid); build(node << 1 | 1, mid + 1, e); if (array[sTree[node<<1]]<array[sTree[node << 1 | 1]]){ sTree[node]=sTree[node<<1]; } else sTree[node]=sTree[node<<1 | 1]; } } void update_point(int index, long val) // O(log n) { array[index]=val; index += N - 1; while (index > 1) { index >>= 1; propagate(index,0,0,0); if (array[sTree[index<<1]]<array[sTree[index << 1 | 1]]){ sTree[index]=sTree[index<<1]; } else sTree[index]=sTree[index << 1|1]; } } void update_range(int i, int j, long val) // O(log n) { update_range(1, 1, N, i, j, val); } void update_range(int node, int b, int e, int i, int j, long val) { if (i > e || j < b) return; if (b >= i && e <= j) { array[sTree[node]]+=val; lazy[node] += val; } else { int mid = b + e >> 1; propagate(node, b, mid, e); update_range(node << 1, b, mid, i, j, val); update_range(node << 1 | 1, mid + 1, e, i, j, val); if (array[sTree[node<<1]]<array[sTree[node << 1 | 1]]){ sTree[node]=sTree[node<<1]; } else sTree[node]=sTree[node<<1 | 1]; } } void propagate(int node, int b, int mid, int e) { lazy[node << 1] += lazy[node]; lazy[node << 1 | 1] += lazy[node]; if (sTree[node<<1]!=sTree[node]) array[sTree[node << 1]] += lazy[node]; if (sTree[node<<1 | 1 ]!=sTree[node]) array[sTree[node << 1 | 1]] += lazy[node]; lazy[node] = 0; } int query(int i, int j) { return query(1, 1, N, i, j); } int query(int node, int b, int e, int i, int j) // O(log n) { if (i > e || j < b) return -1; if (b >= i && e <= j) return sTree[node]; int mid = b + e >> 1; propagate(node, b, mid, e); int q1 = query(node << 1, b, mid, i, j); int q2 = query(node << 1 | 1, mid + 1, e, i, j); if (q1==-1)return q2; if (q2==-1)return q1; if (array[q1]<array[q2])return q1; return q2; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const long long mod = 1e9 + 7; long long tree[200050 << 2], lazy[200050 << 2], a[200050]; void pushup(long long rt) { tree[rt] = min(tree[rt << 1], tree[rt << 1 | 1]); } void pushdown(long long rt) { if (lazy[rt]) { lazy[rt << 1] += lazy[rt]; lazy[rt << 1 | 1] += lazy[rt]; tree[rt << 1] -= lazy[rt]; tree[rt << 1 | 1] -= lazy[rt]; lazy[rt] = 0; } } void build(long long l, long long r, long long rt) { if (l == r) { tree[rt] = a[l]; return; } long long mid = (l + r) / 2; build(l, mid, rt * 2); build(mid + 1, r, rt * 2 + 1); pushup(rt); } void update_id(long long pos, long long l, long long r, long long rt) { if (l == r) { tree[rt] = INF; return; } pushdown(rt); long long mid = (l + r) / 2; if (pos <= mid) update_id(pos, l, mid, rt * 2); else update_id(pos, mid + 1, r, rt * 2 + 1); pushup(rt); } void update_range(long long L, long long R, long long w, long long l, long long r, long long rt) { if (L <= l && r <= R) { tree[rt] -= w; lazy[rt] += w; return; } pushdown(rt); long long mid = (l + r) / 2; if (L <= mid) update_range(L, R, w, l, mid, rt * 2); if (R > mid) update_range(L, R, w, mid + 1, r, rt * 2 + 1); pushup(rt); } long long query(long long l, long long r, long long rt) { if (l == r) { return l; } pushdown(rt); long long mid = (l + r) / 2; if (tree[rt << 1 | 1] == 0) return query(mid + 1, r, rt * 2 + 1); else return query(l, mid, rt * 2); } long long ans[200050]; int main() { long long n; scanf("%lld", &n); for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]); build(1, n, 1); for (long long i = 1; i <= n; i++) { int pos = query(1, n, 1); ans[pos] = i; update_id(pos, 1, n, 1); update_range(pos, n, i, 1, n, 1); } for (long long 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 " ] }
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 (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 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct BIT { int size; vector<long long> bit; BIT() { size = 0; } BIT(int s) { size = s; bit.resize(size + 1); init(); } void init() { for (int i = 1; i <= size; i++) bit[i] = 0; } long long query(long long i) { long long ret = 0; while (i > 0) { ret += bit[i]; i -= i & (-i); } return ret; } void add(long long i, long long x) { while (i <= size) { bit[i] += x; i += i & (-i); } } }; long long n; long long s[200005]; long long ans[200005]; BIT bit(200005); int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> s[i]; for (int i = 1; i <= n; i++) bit.add(i, i); for (int i = n; i >= 1; i--) { long long ub = n, lb = 0, mid; while (ub - lb > 1) { mid = (ub + lb) / 2; if (bit.query(mid) <= s[i]) lb = mid; else ub = mid; } ans[i] = ub; bit.add(ub, -ub); } 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 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; const long long maxn = 2e5 + 10; long long n, a[maxn], ans[maxn]; long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } void print(long long x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + '0'); } void write(long long x) { print(x); puts(""); } struct Segment_Tree { struct Seg { long long mn, f; } tree[maxn << 2]; void update(long long k) { tree[k].mn = min(tree[k << 1].mn, tree[k << 1 | 1].mn); } void down(long long k) { if (tree[k].f) { tree[k << 1].f += tree[k].f; tree[k << 1 | 1].f += tree[k].f; tree[k << 1].mn += tree[k].f; tree[k << 1 | 1].mn += tree[k].f; tree[k].f = 0; } } void build(long long k, long long l, long long r) { tree[k].f = 0; if (l == r) { tree[k].mn = a[l]; return; } long long mid = (l + r) >> 1; build(k << 1, l, mid); build(k << 1 | 1, mid + 1, r); update(k); } void change(long long k, long long l, long long r, long long x, long long y, long long v) { if (x > y) return; if (x <= l && r <= y) { tree[k].mn += v; tree[k].f += v; return; } down(k); long long mid = (l + r) >> 1; if (x <= mid) change(k << 1, l, mid, x, y, v); if (mid < y) change(k << 1 | 1, mid + 1, r, x, y, v); update(k); } void erase(long long k, long long l, long long r, long long x) { if (l == r) { tree[k].mn = inf; tree[k].f = 0; return; } down(k); long long mid = (l + r) >> 1; if (x <= mid) erase(k << 1, l, mid, x); else erase(k << 1 | 1, mid + 1, r, x); update(k); } long long query(long long k, long long l, long long r) { if (l == r) return l; down(k); long long mid = (l + r) >> 1; if (!tree[k << 1 | 1].mn) return query(k << 1 | 1, mid + 1, r); else return query(k << 1, l, mid); } } T; signed main() { n = read(); for (long long i = 1; i <= n; i++) a[i] = read(); T.build(1, 1, n); for (long long i = 1; i <= n; i++) { long long pos = T.query(1, 1, n); ans[pos] = i; T.change(1, 1, n, pos + 1, n, -i); T.erase(1, 1, n, pos); } for (long long i = 1; i <= n; i++) printf("%lld ", ans[i]); puts(""); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<long long> bit(200001); void update(long long x, long long y, long long n) { for (long long i = x; i <= n; i = i + (i & (-i))) bit[i] += y; } int search(long long x, int n) { long long ans = 0, sum = 0; for (int i = 21; i >= 0; i--) { if (ans + (1LL << i) <= n && sum + bit[ans + (1LL << i)] <= x) { ans = ans + (1LL << i); sum = sum + bit[ans]; } } return ans + 1; } int main() { int n; cin >> n; vector<long long> arr(n + 1); for (int i = 1; i <= n; i++) { cin >> arr[i]; update(i, i, n); } vector<int> ans(n + 1); for (int i = n; i > 0; i--) { ans[i] = search(arr[i], n); update(ans[i], -ans[i], n); } for (int i = 1; i <= n; i++) cout << ans[i] << " "; }