Question F: Escape Room

Question F: Escape Room

Time Limit: 1 Sec   Memory Limit: 128 MB
Commits: 63   Resolved: 27
[ Commit ][ Status ][ Discussion Board ][Assert By: admin ]

Topic description

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?

enter

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.

output

Print in one line the lexicographically smallest permutation that satisfies all the conditions.

sample input

4
1 2 2 1

Sample output 4 2 1 3


**************************************************** ************************
is a thinking question about the longest ascending subsequence, so it belongs to dp. The
meaning of the question:
give you n numbers are The maximum length of the longest ascending subsequence of each number from this position to the last digit
Find a matching sequence (if multiple outputs are the smallest)
//
Sample output: 4 2 1 3
(the longest ascending length of 4 is 1 ; the longest ascending length of 2 is 2; the longest ascending length of 1
is

2; The smallest fills in 4 3 2 1 sequentially
but it has to be traversed every time, I tried to traverse the largest and smallest at the same time, but still tle
my tle error code:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
 
using namespace std;
 
intmain ()
{
    int n;
    int a [ 100005 ] = { 0 };
    int ans [ 100005 ] = { 0 };
    scanf("%d",&n);
    int maxx = 0;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]>maxx)
            maxx = a[i];
    }
    int p=1,q=maxx; //min max order
    int cnt1=1,cnt2=n; // min max num
    while(1)
    {
        if(p>q)
            break;
        for(int i=0;i<n;i++)
        {
            if(a[i]==p)
            {
                ans[i] = cnt2;
                cnt2 - ;
            }
            if(a[n-1-i]==q)
            {
                ans[n-1-i]=cnt1;
                cnt1++;
            }
        }
//        for(int i=0;i<n;i++)
//        {
//            printf("%d ",ans[i]);
//        }
        //printf("\n");
        p++;
        q--;
    }
    for(int i=0;i<n-1;i++)
    {
        printf("%d ",ans[i]);
    }
    printf("%d",ans[n-1]);
}

Then the teammates thought of an algorithm: use a three-dimensional structure

To make it easier to understand I wrote the group example: 1 1 2 3 3 2 1 1

Step 1: Mark the subscript of the maximum ascending sequence of the first dimension in the second dimension (for recording the original position):

First dimension x: 1 1 2 3 3 2 1 1

Second dimension num: 1 2 3 4 5 6 7 8

Sort by first dimension:

x:        1  1  1  1  2  2  3  3

num:1 2 7 8 3 6 4 5

The third dimension: 8 7 6 5 4 3 2 1 (fill in the corresponding x in reverse order)

In order to change x back to its original position

reorder the entire structure by num

return to the original order

x:     1  1  2  3  3  2  1  1

num:1 2 3 4 5 6 7 8

r:      8  7  4  2  1  3  6  5

The third dimension of the output is the answer

Correct code:

#include <sstream>
#include <iostream>
#include <string>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct po    //结构体
{
    int x,num,r;
};
bool cmp1 (after a, after b)  
{
    if(a.x!=b.x)
        return a.x<b.x;
    return a.num<b.num;

}
bool cmp2(po a,po b)
{
    return a.num<b.num;
}
intmain ()
{
    struct po s[100008]= {0};
    int n,i,x,t;
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        scanf("%d",&t);
        s[i].num = i; //fill the original order into num
        s[i].x=t;    
    }
    sort(s + 1 ,s+ 1 + n,cmp1); //sort by x
     int k= n;
     for (i= 1 ; i<=n; i++ ) //fill in reverse order
    {
        s[i].r=k;
        k--;
    }
    sort(s + 1 ,s+ 1 + n,cmp2); //sort by num to restore the original order of the structure
     for (i= 1 ;i<n;i++)printf( " %d " ,s[i].r ); //output r
    printf("%d\n",s[n].r);
    return 0;
}

 

 

 


  











      
   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324975335&siteId=291194637