HDU

来源:互联网 发布:oracle查找重复数据 编辑:程序博客网 时间:2024/05/16 15:04

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

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#define max_n 100010using namespace std;int a[max_n], dp[max_n], b[max_n];int main() {    int t, n;    scanf("%d", &t);    while(t--) {        memset(b, 0, sizeof(b));        scanf("%d", &n);        for(int i = 0; i < n; i++)            scanf("%d", &a[i]);        int p = 1, u = 1;        dp[1] = a[0];        b[1] = 1;        for(int i = 1; i < n; i++) {            if(dp[p] < a[i]) {                dp[++p] = a[i];                b[++u] = p;            }               else {                int cnt = lower_bound(dp + 1, dp + p + 1, a[i]) - dp;                dp[cnt] = a[i];                b[++u] = cnt;            }        }        for(int i = 1; i <= n; i++) {            if(i > 1) printf(" ");            printf("%d", b[i]);        }        printf("\n");    }    return 0;}