hdu 2404 Permutation Recovery (known reverse seeking original sequence number)

Permutation Recovery

hdu2404

Problem Description

Professor Permula given n integers 1,2, ..., n to her students. For each integer i (1 <= i <= n), she was given the number of students write permutation occurs before i is greater than i is an integer. This number represents ai. For example, if n = 8, but the order is 2,7,3,5,4,1,8,6, then a1 = 5, because there are five digits (2,7,3,5,4) to 1 Big. Similarly, a4 = 2, because there are two numbers (7,5) is greater than 4.
John is one of the students in the class, and now are preparing for the final exam. He found his lost job problem. He only answers (artificial intelligence), but without the original arrangement. Can you help him determine if the original arrangement of it, so he can review how to get the answer?

Entry

Enter composed by a number of test cases. Each test case to the row contains an integer n (n <= 500) starts. It gives the following n rows a1, ..., an. N = 0 to the input end.

Export

For each test case, the print line specifies the original arrangement. It should be separated by commas between adjacent elements replacement. Note that in some cases may need to print a line of more than 80 characters.

analysis:

n data
on a reverse data showing the number 1, the second 2 represents the number of pairs of reverse. .

Because 1 is the smallest of all the other big number than 1, so the location is 1 (in reverse order of the number 1 + 1)
and 2 of it, only one younger than him, and all the other big number than 2, through the array, If the array is empty, the original sequence described here is larger than the number of his (non-empty for 2 hours (at this time the position of non-empty one is placed) than) the number of counter ++ represents a number greater than 2, when the counter is equal to the size of the reverse number 2, indicating the position 2 on the next slot.
3,4,5 and so on until n

code:

#include<iostream>
#include<cstdio>
typedef long long ll;
const int inf=0x3f3f3f3f;
const int inn=0x80808080;
using namespace std;
const int maxm=505;
int a[maxm];
int main(){
    int n;
    while(cin>>n&&n){
        memset(a,0,sizeof a);
        for(int i=1;i<=n;i++){
            int t;
            cin>>t;
            int cnt=0;
            for(int j=1;j<=n;j++){//找符合的位置
                if(a[j]==0){//如果位置是空的
                    if(cnt==t){//如果该位置符合条件就填入
                        a[j]=i;
                        break;
                    }else{
                        cnt++;
                    }
                }
            }
        }
        for(int i=1;i<=n;i++){
            if(i-1)cout<<',';
            cout<<a[i];
        }
        cout<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44178736/article/details/92072388