hdu 5748 Bellovin [the longest ascending subsequence]

Question link: https://vjudge.net/contest/148584#problem/A

 

Topic meaning:

 

Problem- solving idea:
The title requirement is: output the sequence with the smallest lexicographical order that is the same as f(i) (the definition of f(i) is the same as the title) of each element of the known sequence. After a little thinking, we can actually find f(i) of the original sequence. This is easy to do. Just make some changes to the template problem of the longest ascending subsequence, and record the f of each element of the original sequence. (i) is sufficient. See the code for specific implementation.

#include <cstdio>
#include <algorithm>  
using namespace std;

int b[100005], c[100005];
int n;

intmain ()
{
    int t, data;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d", &n);
        int num = 0;
        for (int i = 1; i <= n; i++)
        {
            scanf( " %d " , & data);
             if (i == 1 ) b[++num] = data, c[ 1 ] = 1 ;             // the number of b[] arrays is the last one at the end of i The length of the long subsequence, the elements in the b[] array are the elements that form the longest ascending subsequence at this time 
            else  if (data>b[num]) b[++num] = data, c[i] = num;
             else
            {
                int k = lower_bound(b + 1 , b + 1 + num, data) - b;            // binary search function, find the position of the first element greater than data 
                b[k] = data;
                c[i] = k;                 // c[i] records the length of the longest subsequence at the end of i 
            }
        }
        for (int i = 1; i<n; i++)
            printf("%d ", c[i]);
        printf("%d\n", c[n]);
    }
    return 0;
}

 

 

2018-04-29

 

Guess you like

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