Escape Room(思维)

问题 F: Escape Room

时间限制: 1 Sec   内存限制: 128 MB
提交: 110   解决: 49
[ 提交][ 状态][ 讨论版][命题人: 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?

输入

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个数ai,分别代表以i开始到第n个数的最长递增子序列为ai,要求你构造出这么一个字典序最小的序列。

题解:水题,但是比赛的时候百思不得其解(感慨自己的菜),最终找到的规律是对于每一个ai为结尾求一个最长递增,则构造的该位为当前第几大,而且要倒着构造,因为复杂度原因不可行。最后在学长的教导下才知道正解,由ai的大小倒着构造,ai最大的数赋值最大,相同的时候按位置顺序赋值。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<time.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))
const double PI = acos(-1.0);
const double eps = 1e-6;
typedef long long ll;
using namespace std;
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}

struct node
{
    int x,indx,y;
}p[100005];

int cmp1(node a,node b)
{
    if(a.x!=b.x)
        return a.x<b.x;
    return a.indx<b.indx;
}
int cmp2(node a,node b)
{
    return a.indx<b.indx;
}

int main()
{
    int n;
    cin>>n;

    for(int i=1;i<=n;i++)
    {
        scanf("%d",&p[i].x);
        p[i].indx=i;
    }
    sort(p+1,p+1+n,cmp1);

    for(int i=1;i<=n;i++)
        p[i].y=(n-i+1);

    sort(p+1,p+1+n,cmp2);

    printf("%d",p[1].y);
    for(int i=2;i<=n;i++)
        printf(" %d",p[i].y);

    printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sudu6666/article/details/80110237