5140: Escape Room
时间限制: 1 Sec 内存限制: 128 MB提交: 116 解决: 53
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
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?
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.
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和n个数ai,ai表示从i开始到第n个数的最长上升子序列的个数,输出满足条件的一组且字典序最小的一组数
思路:按ai从小到大排序,ai相同的按输入顺序排序,从n开始按顺序赋值,变回原来的顺序,输出
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
typedef struct node
{
int x,index,y;
}NODE;
NODE p[100010];
int cmp1(NODE a,NODE b)
{
if(a.x!=b.x)
return a.x<b.x;
return a.index<b.index;
}
int cmp2(NODE a,NODE b)
{
return a.index<b.index;
}
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&p[i].x);
p[i].index=i;
}
sort(p+1,p+n+1,cmp1);
for(i=1;i<=n;i++)
p[i].y=(n-i+1);
sort(p+1,p+n+1,cmp2);
printf("%d",p[1].y);
for(i=2;i<=n;i++)
printf(" %d",p[i].y);
putchar('\n');
return 0;
}