Random Generator
Time limit1sMemory limit1024 MB
Simulate repeatedly picking the p-th remaining copy from a multiset where value i appears w_i times, and output the order in which values are exhausted.
- Level
Medium7 of 10
- Topics
- Segment tree, Binary search, Prefix sum, Simulation
- Solved
- No attempts yet
Problem
Gukryeol will randomly build a permutation of the positive integers from 1 to N using the given positive integers w1 ... , wN. The following is the method for randomly building the permutation.
- Place each positive integer i from 1 to N (1 ≤ i ≤ N) consecutively, wi copies at a time.
- Let W be the total number of positive integers currently placed. Choose one number pi uniformly at random from the positive integers 1 through W.
- Add the pi-th number to the permutation.
- Erase all the numbers added to the permutation, and repeat steps 2 through 4 until no numbers remain.
Given the numbers w1 through wN and p1 through pN, find the permutation that results.
Input
The first line gives N (1 ≤ N ≤ 200,000).
The second line gives the positive integers w1 through wN, each at most 1,000.
The third line gives the positive integers p1 through pN. Only cases in which a sequence can be built from p1 through pN are given.
Output
Find the sequence of positive integers from 1 to N that results at the end.