第15屆浙江省賽E題 - LIS(ZOJ 4028)

歡迎訪問https://blog.csdn.net/lxt_Lucia~~

宇宙第一小仙女\(^o^)/~萌量爆表求帶飛=≡Σ((( つ^o^)つ~ dalao們點個關注唄~

 

--------------------------------我只是一條可愛噠分界線-------------------------------

 

一、問題:

Description

DreamGrid is learning the LIS (Longest Increasing Subsequence) problem and he needs to find the longest increasing subsequence of a given sequence a1,a2,…,ana1,a2,…,an of length nn.

Recall that

  • A subsequence b1,b2,…,bmb1,b2,…,bm of length mm is a sequence satisfying b1 = ak1 , b2 = ak2 ,…, bm = akm b1 = ak1, b2 = ak2 ,…, bm = akm and 1≤ k1 < k2<⋯<km ≤ n1 ≤ k1 < k2 <⋯< km ≤n.

  • An increasing subsequence b1,b2,…,bm b1,b2,…,bm is a subsequence satisfying b1 < b2 < ⋯ < bm b1 < b2 <⋯< bm.

DreamGrid defines the helper sequence f1,f2,…,fn f1,f2,…,fn where fifi indicates the maximum length of the increasing subsequence which ends with aiai. In case you don't know how to derive the helper sequence, he provides you with the following pseudo-code which calculates the helper sequence.

DreamGrid has derived the helper sequence using the program, but the original sequence a1,a2,…,an a1,a2,…,an is stolen by BaoBao and is lost! All DreamGrid has in hand now is the helper sequence and two range sequences l1,l2,…,lnl1,l2,…,ln and r1,r2,…,rn r1,r2,…,rn indicating that li ≤ ai ≤ ri li ≤ ai ≤ ri for all 1 ≤ i ≤ n1 ≤ i ≤ n.

Please help DreamGrid restore the original sequence which is compatible with the helper sequence and the two range sequences.

 

Input

There are multiple test cases. The first line of the input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains an integer nn (1 ≤ n ≤ 1051 ≤ n ≤ 105), indicating the length of the original sequence.

The second line contains nn integers f1,f2,…,fn f1,f2,…,fn (1 ≤ fi ≤ n1 ≤ fi ≤ n) seperated by a space, indicating the helper sequence.

For the following nn lines, the ii-th line contains two integers lili and riri (0≤ li ≤ ri ≤ 2×1090 ≤ li ≤ ri ≤ 2×109), indicating the range sequences.

It's guaranteed that the original sequence exists, and the sum of nn of all test cases will not exceed 5×1055×105.

 

Output

For each test case output one line containing nn integers separated by a space, indicating the original sequence. If there are multiple valid answers, print any of them.

Please, DO NOT print extra spaces at the end of each line, or your solution may be considered incorrect!

 

Sample Input

4
6
1 2 3 2 4 3
0 5
2 4
3 3
1 2
3 5
1 5
5
1 2 1 3 1
100 200
200 300
200 400
400 500
100 500
7
1 2 3 1 1 4 2
0 3
0 3
0 3
0 3
0 3
0 3
0 3
2
1 1
1 2
2 3

 

Sample Output

1 2 3 2 5 3
200 300 200 500 200
0 1 2 0 0 3 1
2 2

 

二、題意:

給出一列由 n 個數組成的幫助序列 f [ i ]( 即從a [ 0 ] 到 a [ i ] 的子序列的 LIS 爲 f [ i ] ),再分別給出原序列 a [ i ] 中每個數的取值範圍,求滿足該條件的原序列,若有多組,輸出任意一組即可( 特判 ),保證有解。

 

三、思路:

若 i > j 時,有f [ i ] == f [ j ],則a [ i ] <= a [ j ] 。

若f [ i ] > f [ j ],則 a [ i ] > a [ j ] 。

從後往前遍歷一遍a [ i ] <= a [ j ] 確定一個下限,然後從前到後遍歷一遍a [ i ] < a [ j ] 更新下限。

pre [ i ] 用來記錄前面是否有相同的 f [ i ],即是否可以構成多個LIS串,有的話就把下標記錄下來。

 

四、代碼:

#include <cstdio>
#include <cstring>
#include <algorithm>
#define read(x) scanf("%d",&x)
#define mem(a,b) memset(a,b,sizeof(a))
#define fori(a,b) for(int i=a; i<=b; ++i)
#define ifor(a,b) for(int i=a; i>=b; --i)

using namespace std;
const int maxn = 1e5 + 7;
int f[maxn], a[maxn], L[maxn], pre[maxn];

int main()
{
    int T, n, x;
    read(T);
    while(T--)
    {
        read(n);
        mem(pre, -1);
        fori(1, n)
            read(f[i]);
        fori(1, n)
            read(L[i]), read(x);
        ifor(n, 1)
        {
            a[i] = L[i];
            if(pre[f[i]] != -1)
                a[i] = max(a[pre[f[i]]], L[i]);
            pre[f[i]] = i;
        }
        mem(pre, -1);
        fori(1, n)
        {
            int x = pre[f[i]-1];
            if(x != -1)
                a[i] = max(a[i],a[x]+1);
            pre[f[i]] = i;
        }
        fori(1, n)
        {
            if(i != n)
                printf("%d ",a[i]);
            else
                printf("%d\n",a[i]);
        }
    }
    return 0;
}

 

--------------------------------我也是有底線的---------------------------------

宇宙第一小仙女\(^o^)/~萌量爆表求帶飛=≡Σ((( つ^o^)つ~ dalao們點個關注唄~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章