美人鱼 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 1≤k<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
(1≤n≤100000)(1≤n≤100000)
-- the length of the sequence. The second line contains nn
integers a1,a2,...,ana1,a2,...,an
(1≤ai≤109)(1≤ai≤109)
.OutputFor each test case, output nn
integers b1,b2,...,bnb1,b2,...,bn
(1≤bi≤109)(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\n",lower_bound(dp,dp+n,a[n-1])-dp+1); } return 0; }