第15届浙江省赛 E LIS

本文介绍了一个关于最长递增子序列(LIS)的问题,重点在于如何根据已知的辅助序列和范围序列来恢复原始序列。文章通过具体实例展示了问题的输入输出样例,并提供了一段C++代码实现解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



LIS

Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge

DreamGrid is learning the LIS (Longest Increasing Subsequence) problem and he needs to find the longest increasing subsequence of a given sequence  of length .

Recall that

  • A subsequence  of length  is a sequence satisfying  and .

  • An increasing subsequence  is a subsequence satisfying .

DreamGrid defines the helper sequence  where  indicates the maximum length of the increasing subsequence which ends with . 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.

procedure lis_helper(: original sequence)
{Let  be the length of the original sequence,
 be the -th element in sequence , and 
be the -th element in sequence }
for  := 1 to 
     := 1
    for  := 1 to ( - 1)
        if  and 
             :=  + 1
return  { is the helper sequence}

DreamGrid has derived the helper sequence using the program, but the original sequence  is stolen by BaoBao and is lost! All DreamGrid has in hand now is the helper sequence and two range sequences  and  indicating that  for all .

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 , indicating the number of test cases. For each test case:

The first line contains an integer  (), indicating the length of the original sequence.

The second line contains  integers  () seperated by a space, indicating the helper sequence.

For the following  lines, the -th line contains two integers  and  (), indicating the range sequences.

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

Output

For each test case output one line containing  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
题解:
若i位置有fi,ai,则之前若有j<i且fi==fj,则ai<=aj
若有fi==fj+1,则ai>aj
只需从后往前遍历一遍ai<=aj确定一个下限,然后
从前到后跑一边ai<aj更新下限即可
代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+7;
int a[maxn],d[maxn],L[maxn],pre[maxn];
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        memset(pre,-1,sizeof(pre));
        int n;scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
        {
            int x;scanf("%d%d",&L[i],&x);
        }
        for(int i=n;i>=1;i--)
        {
            d[i]=L[i];
            if(pre[a[i]]!=-1)d[i]=max(d[pre[a[i]]],L[i]);
            pre[a[i]]=i;
        }
        memset(pre,-1,sizeof(pre));
        for(int i=1;i<=n;i++)
        {
            int x=pre[a[i]-1];
            if(x!=-1)d[i]=max(d[i],d[x]+1);
            pre[a[i]]=i;
        }
        for(int i=1;i<=n;i++)
        {
            printf("%d",d[i]);
            if(i!=n)printf(" ");
            else printf("\n");
        }
    }
    return 0;
}

LIS

Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge

DreamGrid is learning the LIS (Longest Increasing Subsequence) problem and he needs to find the longest increasing subsequence of a given sequence  of length .

Recall that

  • A subsequence  of length  is a sequence satisfying  and .

  • An increasing subsequence  is a subsequence satisfying .

DreamGrid defines the helper sequence  where  indicates the maximum length of the increasing subsequence which ends with . 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.

procedure lis_helper(: original sequence)
{Let  be the length of the original sequence,
 be the -th element in sequence , and 
be the -th element in sequence }
for  := 1 to 
     := 1
    for  := 1 to ( - 1)
        if  and 
             :=  + 1
return  { is the helper sequence}

DreamGrid has derived the helper sequence using the program, but the original sequence  is stolen by BaoBao and is lost! All DreamGrid has in hand now is the helper sequence and two range sequences  and  indicating that  for all .

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 , indicating the number of test cases. For each test case:

The first line contains an integer  (), indicating the length of the original sequence.

The second line contains  integers  () seperated by a space, indicating the helper sequence.

For the following  lines, the -th line contains two integers  and  (), indicating the range sequences.

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

Output

For each test case output one line containing  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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值