Permutation Recovery
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 456 Accepted Submission(s): 316
Problem Description
Professor Permula gave a number of permutations of the n integers 1, 2, ..., n to her students. For each integer i (1 <= i <= n), she asks the students to write down the number of integers greater than i that appears before i in the given permutation. This number is denoted ai. For example, if n = 8 and the permutation is 2,7,3,5,4,1,8,6, then a1 = 5 because there are 5 numbers (2, 7, 3, 5, 4) greater than 1 appearing before it. Similarly, a4 = 2 because there are 2 numbers (7, 5) greater than 4 appearing before it.
John, one of the students in the class, is studying for the final exams now. He found out that he has lost the assignment questions. He only has the answers (the ai‘s) but not the original permutation. Can you help him determine the original permutation, so he can review how to obtain the answers?
Input
The input consists of a number of test cases. Each test case starts with a line containing the integer n (n <= 500). The next n lines give the values of a1, ..., an. The input ends with n = 0.
Output
For each test case, print a line specifying the original permutation. Adjacent elements of a permutation should be separated by a comma. Note that some cases may require you to print lines containing more than 80 characters.
Sample Input
8
5
0
1
2
1
2
0
0
10
9
8
7
6
5
4
3
2
1
0
0
Sample Output
2,7,3,5,4,1,8,6
10,9,8,7,6,5,4,3,2,1
题意:给你一个序列 如2,7,3,5,4,1,8,6 a1=5代表1前边比1大的数的个数为5 (2 7 3 5 4)a4=2代表4前边比4大的数的个数为2个(7 5)现在给你每个数前边比这个数大的数的个数让你求出这个序列
题解:因为所给的数都是按照1~n的顺序给的,所以我们可以看做有n个空格 可以根据这个顺序来向空格中放数,首先,当输入0时代表前边没有比它自己大的数则证明前边的空格都已放满,此时找到数组a的
第一个0的位置就是这个数的位置,如果输入的数k不为0则证明这个数前边还有k个比自己大的数,那么找到第k+1个0的位置,就是这个数的位置
#include<stdio.h> #include<string.h> #define MAX 510 int a[MAX];//输入数据 int main() { int n,m,j,i,k,t; while(scanf("%d",&n),n) { memset(a,0,sizeof(a)); for(i=1;i<=n;i++) { scanf("%d",&k); int ok=0; for(j=1;j<=n;j++) { bool flag=false; if(k==0) { for(t=1;t<=n;t++) { if(a[t]==0) { a[t]=i; flag=true; break; } } if(flag) break; } else { if(a[j]==0) ok++; if(ok==k+1) { a[j]=i; break; } } } } for(i=1;i<n;i++) printf("%d,",a[i]); printf("%d",a[n]); printf("\n"); } return 0; }