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+,则ai>aj
只需从后往前遍历一遍ai<=aj确定一个下限,然后
从前到后跑一边ai<aj更新下限即可
代码: #include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+;
int a[maxn],d[maxn],L[maxn],pre[maxn];
int main()
{
int T;scanf("%d",&T);
while(T--)
{
memset(pre,-,sizeof(pre));
int n;scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
int x;scanf("%d%d",&L[i],&x);
}
for(int i=n;i>=;i--)
{
d[i]=L[i];
if(pre[a[i]]!=-)d[i]=max(d[pre[a[i]]],L[i]);
pre[a[i]]=i;
}
memset(pre,-,sizeof(pre));
for(int i=;i<=n;i++)
{
int x=pre[a[i]-];
if(x!=-)d[i]=max(d[i],d[x]+);
pre[a[i]]=i;
}
for(int i=;i<=n;i++)
{
printf("%d",d[i]);
if(i!=n)printf(" ");
else printf("\n");
}
}
return ;
}

第15届浙江省赛 E LIS的更多相关文章

  1. 第15届浙江省赛 D Sequence Swapping(dp)

    Sequence Swapping Time Limit: 1 Second      Memory Limit: 65536 KB BaoBao has just found a strange s ...

  2. ZOJ 3879 Capture the Flag 15年浙江省赛K题

    每年省赛必有的一道模拟题,描述都是非常的长,题目都是蛮好写的... sigh... 比赛的时候没有写出这道题目 :( 题意:首先输入4个数,n,q,p,c代表有n个队伍,q个服务器,每支队伍的初始分数 ...

  3. CSUST 第15届 校赛总结

    一直想记录一下自己的比赛,却感觉空间说说有点不适,思考了一番还是打算放到自己的博客园 这次比赛总体来说还是不错,签到还是稳的一批,基本前四小时都在rk1 开局切了几道签到题,然后开了一道思维gcd,正 ...

  4. ZOJ 3872 Beauty of Array DP 15年浙江省赛D题

    也是一道比赛时候没有写出来的题目,队友想到了解法不过最后匆匆忙忙没有 A 掉 What a pity... 题意:定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和 ...

  5. ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds      Me ...

  6. ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Time Limit: 2 Seconds      Me ...

  7. ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds      Me ...

  8. 第十五届浙江省赛 F Now Loading!!!

    Now Loading!!! Time Limit: 1 Second      Memory Limit: 131072 KB DreamGrid has  integers . DreamGrid ...

  9. 2016第十三届浙江省赛 D - The Lucky Week

    D - The Lucky Week Edward, the headmaster of the Marjar University, is very busy every day and alway ...

随机推荐

  1. Spring scope解惑

    在2.0之前只有两种singleton和prototype(网上说的,没去验证),后面增加了session.request.global session三种专门用于web应用程序上下文的Bean Si ...

  2. 10 个深恶痛绝的 Java 异常

    异常是 Java 程序中经常遇到的问题,我想每一个 Java 程序员都讨厌异常,一 个异常就是一个 BUG,就要花很多时间来定位异常问题. 今天,来列一下 Java 中经常遇到的前 10 个异常,排名 ...

  3. note——《Tableau商业分析一点通》

    为了更好地发掘各领域的数据价值,且能对数据进行精确分析及可视化,掌握资料的脉动,做出正确的决策 人们需要一种工具:能够快速灵活地连接和整合数据,提供简单的方式实现从不同的角度去观察研究数据,计算和展示 ...

  4. 拖拽窗口的实现-JQuery实现;

    主要是距离的掌握 如图,原始位置和当前位置. 对于当前位置:想要求得left值b',需要b'=a'-c; 其中,a’= ev.pageX;就是指针当前距离文档左边的距离: 同时,可以发现c在拖拽过程中 ...

  5. django中Model表的反向查询

    很多时候需要在多张表之间进行跨表查询,这其中外键是必须存在的,而通过外键所处的表的对象进行跨表查询, 称为正向查询.反之,则是反向查询. 正向查询很简单,这里不谈. 主要谈下反向查询. class U ...

  6. C# 语言版本

    (摘自:维基百科)https://en.wikipedia.org/wiki/C_Sharp_(programming_language) Versions Version Language spec ...

  7. Mybatis输入和输出映射(#{}和${}的区别)

    #{}占位符                                                                                              ...

  8. C/C++开发android应用

    (转自:http://blog.csdn.net/srplab1/article/details/7617963) 在某些情况下,比如原来与很多c/c++的代码, 可能希望采用c/c++编写andro ...

  9. 【python】利用h5py存储数据

    两类容器:group & dataset group类似文件夹,字典. dataset是数据集,类似数组 支持更多的对外透明的存储特征,数据压缩,误差检测,分块传输 group下面可以是gro ...

  10. Intellij IDEA基于Springboot的远程调试

    简介 本篇博客介绍一下在Intellij IDEA下对Springboot类型的项目的远程调试功能.所谓的远程调试就是服务端程序运行在一台远程服务器上,我们可以在本地服务端的代码(前提是本地的代码必须 ...