题目描述
As you know, escape rooms became very popular since they allow you to play the role of a video game hero. One such room has the following quiz. You know that the locker password is a permutation of N numbers. A permutation of length N is a sequence of distinct positive integers, whose values are at most N. You got the following hint regarding the password - the length of the longest increasing subsequence starting at position i equals Ai. Therefore you want to find the password using these values. As there can be several possible permutations you want to find the lexicographically smallest one. Permutation P is lexicographically smaller than permutation Q if there is an index i such that Pi < Qi and Pj = Qj for all j < i. It is guaranteed that there is at least one possible permutation satisfying the above constraints.
Can you open the door?
输入
The first line of the input contains one integer N (1≤N≤105).
The next line contains N space-separated integer Ai (1≤Ai≤N).
It’s guaranteed that at least one possible permutation exists.
输出
Print in one line the lexicographically smallest permutation that satisfies all the conditions.
样例输入
复制样例数据 4
1 2 2 1
样例输出
4 2 1 3
题意 根据所给的序列(每个数代表从这个位置开始的最长递增子序列长度)找出满足条件的字典序最小序列
找最优解 将所给序列按照从从小到大排序将,要求递增序列尽可能小就让数字尽可能大,所以将n-1依次赋给排完序后的结构体,然后将结构体按编号从小到大排即可
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct no {
int num;
int name;
}ss[100010];
int cmp1(no a, no b)
{
return a.num < b.num;
}
int cmp2(no a, no b)
{
return a.name < b.name;
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> ss[i].num;
ss[i].name = i;
}
sort(ss+1, ss + n + 1, cmp1);
for (int i = 1; i <= n; i++)
{
ss[i].num = n - i + 1;
}
sort(ss + 1, ss + 1 + n, cmp2);
for (int i = 1; i <= n; i++)
{
if(i!=n) cout << ss[i].num<<" ";
else cout << ss[i].num;
}
}