Problem Description
Peter has a sequence a1,a2,...,an and he define a function on the sequence -- F(a1,a2,...,an)=(f1,f2,...,fn), where fi is the length of the longest increasing subsequence ending with ai.

Peter would like to find another sequence b1,b2,...,bn in such a manner that F(a1,a2,...,an) equals to F(b1,b2,...,bn). Among all the possible sequences consisting of only positive integers, Peter wants the lexicographically smallest one.

The sequence a1,a2,...,an is lexicographically smaller than sequence b1,b2,...,bn, if there is such number i from 1 to n, that ak=bk for 1≤k<i and ai<bi.

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

The first contains an integer n (1≤n≤100000) -- the length of the sequence. The second line contains n integers a1,a2,...,an (1≤ai≤109).

 
Output
For each test case, output n integers b1,b2,...,bn (1≤bi≤109) denoting the lexicographically smallest sequence.
 
Sample Input
3
1
10
5
5 4 3 2 1
3
1 3 5
 
Sample Output
1
1 1 1 1 1
1 2 3

【题意】给出n个数的序列求每个数的最长上升子序列

【思路】可以树状数组求解

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=+;
int a[N],b[N],c[N],ans[N];
int n,cnt;
int lowbit(int x)
{
return x&(-x);
} int query(int x)
{
int res=;
while(x)
{
res=max(c[x],res);//找出前面比他小的数中拥有最长上升子序列的
x-=lowbit(x);
}
return res;
}
void update(int p,int x)
{
while(p<=cnt)
{
c[p]=max(x,c[p]);//把后面的数最长上升子序列进行更新
p+=lowbit(p);
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
cnt=unique(b+,b++n)-b-;
for(int i=;i<=n;i++)
{
a[i]=lower_bound(b+,b++cnt,a[i])-b;
}
memset(c,,sizeof(c));
for(int i=;i<=n;i++)
{
ans[i]=query(a[i]-)+;//前面存在的最长上升子序列长度加上1,就是当前数拥有的最长上升子序列长度
update(a[i],ans[i]);//更新后面的数的长度
printf("%d%c",ans[i],i==n?'\n':' ');
}
} return ;
}

Bellovin_树状数组的更多相关文章

  1. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  2. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

  3. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  5. BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]

    3529: [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1399  Solved: 694[Submit][Status] ...

  6. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  7. 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组

    E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  8. 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序

    3881: [Coci2015]Divljak Time Limit: 20 Sec  Memory Limit: 768 MBSubmit: 508  Solved: 158[Submit][Sta ...

  9. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

随机推荐

  1. C#获取文件时间

    在NTFS下,文件的创建及修改时间可以精确到毫秒,以下是测试过程. DirectoryInfo diSource = new DirectoryInfo(@"C:\Users\不告诉你\De ...

  2. C语言面试题(三)

    这篇主要聚焦在排序算法,包括常见的选择排序,插入排序,冒泡排序,快速排序.会对这四种排序的时间复杂度和空间复杂度进行探究. a.选择排序 int main(int argc,char **argv){ ...

  3. mysql 日期格式化

    SELECT plc.id, plc.policy_no, plc.out_date, og.organ_name, ir.insurer_name, pd.product_name, plc.pol ...

  4. Linux用户与“最小权限”原则

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 作为一个Linux用户来说,我们并不需要特别关心下面的机制.但是,当我们去编写一个 ...

  5. PHP-----循环结构

    for循环语句 打印金字塔 完整的金字塔 //打印金字塔 $n=25; for($i=1;$i<=$n;$i++){ //空格循环 for($k=1;$k<=$n-$i;$k++){ ec ...

  6. 安装openJDK 8

    1.JDK 8 示例 (1.1)Debian, Ubuntu等使用下述安装命令: $ -jre ps : openjdk-8-jre 仅包含JRE,如果需要开发java程序,需要下载openjdk-8 ...

  7. Java面向对象三大特点之继承

    概念: 继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为. 生活中的继承: 兔子和羊属于食草动物类,狮子和豹属于食肉动物类 ...

  8. windows下的Nodejs及npm的安装、常用命令,Nodejs开发环境配置

    http://www.cnblogs.com/webstorm/p/5744942.html ***************************************** 第一步:下载Nodej ...

  9. CSS 3 颜色表示法

    1,Color Name:red    优点:方便记忆  缺点:数量少不支持透明度 2,HEX方式:#FFFFFF    优点:颜色种类多   缺点:换算复杂需要借助工具 3,RGB方式:rgb(25 ...

  10. havok之内存管理

    [现象记录] 1.往world和rb里都各自加入一个entityListener,当这个rb被remove掉之后, 会首先调用world里的listener的removecallback, 再调用rb ...