Restoration of the Permutation
64-bit integer IO format: %I64d Java class name: (Any)
Let A = {a1, a2, ..., an} be any permutation of the first n natural numbers {1, 2, ..., n}. You are given a positive integer k and another sequence B = {b1, b2, ..., bn}, where bi is the number of elements aj in A to the left of the element at = i such that aj ≥ (i + k).
For example, if n = 5, a possible A is {5, 1, 4, 2, 3}. For k = 2, B is given by {1, 2, 1, 0, 0}. But if k = 3, then B = {1, 1, 0, 0, 0}.
For two sequences X = {x1, x2, ..., xn} and Y = {y1, y2, ..., yn}, let i-th elements be the first elements such that xi ≠ yi. If xi < yi, then X is lexicographically smaller than Y, while if xi > yi, then X is lexicographically greater than Y.
Given n, k and B, you need to determine the lexicographically smallest A.
Input
The first line contains two space separated integers n and k (1 ≤ n ≤ 1000, 1 ≤ k ≤ n). On the second line are n integers specifying the values of B = {b1, b2, ..., bn}.
Output
Print on a single line n integers of A = {a1, a2, ..., an} such that A is lexicographically minimal. It is guaranteed that the solution exists.
Sample Input
5 2 1 2 1 0 0
4 1 5 2 3
4 2 1 0 0 0
2 3 1 4#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> using namespace std; int main() { int n,k; int i,j,t; int a[1010],b[1010]; while(cin>>n>>k) { for(i=1;i<=n;i++) { cin>>a[i]; } for(i=1;i<=n;i++) { j=1; while(a[j]!=0) { j++; } b[i]=j; a[j]--; for(t=1;t+k<=j;t++) { a[t]--; } } for(i=1;i<n;i++) { cout<<b[i]<<' '; } cout<<b[n]<<endl; } return 0; }