zoukankan      html  css  js  c++  java
  • 美人鱼 hdu 5784

    Peter has a sequence a1,a2,...,ana1,a2,...,an and he define a function on the sequence -- F(a1,a2,...,an)=(f1,f2,...,fn)F(a1,a2,...,an)=(f1,f2,...,fn) , where fifi is the length of the longest increasing subsequence ending with aiai .

    Peter would like to find another sequence b1,b2,...,bnb1,b2,...,bn in such a manner that F(a1,a2,...,an)F(a1,a2,...,an) equals to F(b1,b2,...,bn)F(b1,b2,...,bn) . Among all the possible sequences consisting of only positive integers, Peter wants the lexicographically smallest one.

    The sequence a1,a2,...,ana1,a2,...,an is lexicographically smaller than sequence b1,b2,...,bnb1,b2,...,bn , if there is such number ii from 11 to nn , that ak=bkak=bk for 1k<i1≤k<i and ai<biai<bi .InputThere are multiple test cases. The first line of input contains an integer TT , indicating the number of test cases. For each test case:

    The first contains an integer nn (1n100000)(1≤n≤100000) -- the length of the sequence. The second line contains nn integers a1,a2,...,ana1,a2,...,an (1ai109)(1≤ai≤109) .
    OutputFor each test case, output nn integers b1,b2,...,bnb1,b2,...,bn (1bi109)(1≤bi≤109) denoting the lexicographically smallest sequence.
    Sample Input

    3
    1
    10
    5
    5 4 3 2 1
    3
    1 3 5

    Sample Output

    1
    1 1 1 1 1
    1 2 3

    题意:

    就是求fi,即求以ai为最后一位的最长子序列

    解法:

    扫描这一个数组a,每一个数进入dp数组,在里面找到<=a[i]的下标最小的指针,在这里面存储a[i]。

    lower_bound(dp,dp+n,a[i])-dp+1   这个是找到>=a[i]的最小指针减去dp数组的首指针再加1

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<iostream>
    #define inf 0x3f3f3f
    using namespace std;
    typedef long long ll;
    ll a[110000],dp[110000];
    
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            int n;
            scanf("%d",&n);
            for(int i=0;i<n;i++) scanf("%I64d",&a[i]);
            memset(dp,inf,sizeof(dp));
            for(int i=0;i<n-1;i++)
            {
                *lower_bound(dp,dp+n,a[i])=a[i];
                printf("%d ",lower_bound(dp,dp+n,a[i])-dp+1);
            }
            *lower_bound(dp,dp+n,a[n-1])=a[n-1];
            printf("%d
    ",lower_bound(dp,dp+n,a[n-1])-dp+1);
        }
        return 0;
    }
    

      

  • 相关阅读:
    第33周二
    第33周一
    第32周日
    第32周六
    RichTextBox 右键显示 ContextMenuTrip
    关于 Head First SQL 中文版
    linux进程通信之共享内存
    chroot 与 jail
    SQL基础--&gt; 约束(CONSTRAINT)
    MessageDigest简单介绍
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10209335.html
Copyright © 2011-2022 走看看