码迷,mamicode.com
首页 > 其他好文 > 详细

第15届浙江省赛 E LIS

时间:2018-05-08 14:40:35      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:ini   ase   i++   cond   printf   while   for   rect   ++   

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;  
}  

 

第15届浙江省赛 E LIS

标签:ini   ase   i++   cond   printf   while   for   rect   ++   

原文地址:https://www.cnblogs.com/caiyishuai/p/9007651.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!