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. double函数和int函数

    可以看到,当tensor全是double型时,int函数会把所有元素取整,从1.5可以看出,不是四舍五入,而是取整.double函数又把整数型元素变成double型. th> a 0.0000 ...

  2. Oracle软件开发分析

    软件开发的步骤可大致分为: 1.需求分析 2.系统设计 3.编码实现 4.系统测试 5.运行维护 student class     多对一 sno name age gender cid id  c ...

  3. GZAPI框架初识

    新建一个MVC项目(GZAPIFramework.Demo): mvc:用于API接口文档查看,Log日志查看 webapi:api调用 新建一个Biz类库并添加nuget引用: 搜索GZAPI.Co ...

  4. createjs 的 bitmapdata类

    今天测试一个功能,在效率上出现了问题.2D舞台绘制了大量的元素,联想到AS3的 bitmapdata.darw() 功能,遗憾是createjs官方类 中没有bitmapdata类. 好在已经有大神替 ...

  5. nginx+tomcat 配置负载均衡

    nginx 从Nginx官网下载页面(http://nginx.org/en/download.html)下载Nginx最新版本(我用的是nginx-1.8.1版本) 安装就直接把压缩包解压到一个路径 ...

  6. 关于JSPatch热修复

    今天和同事聊到JSPatch热修复,我们来看下JSPatch,确实解决了APP升级的痛点. 刚好,已经有这么一个第三方平台实现了后台管理,全套服务(网址是:http://jspatch.com/),先 ...

  7. i.BIO方式的SSL通道流程

    前面已经讲解了BIO通道的整体流程,对于SSL的流程是插在通道中的,在BIO通道的初始化的时候,根据Connector配置的SSLEnabled属性进行SSL的逻辑. 主要集中的位置在JIOEndpo ...

  8. vue学习笔记之属性和方法

    每个Vue都会代理其data对象里所有的属性:只有这些被代理的属性是响应的.如果在实例创建之后添加新的属性到实例上,它不会触发视图更新.例子: <script type="text/j ...

  9. 20150207读书笔记<深入理解计算机系统2-1>

    第二章 信息存储 (1)  多数计算机以一个字节作为最小可寻址的存储器单元. 机器级程序将存储器看成一个非常大的字节数组,称为虚拟存储器. 存储器的每个字节都由唯一的数字标识,称为它的地址. 所有可能 ...

  10. String类和StringBuffer类的方法

    一.String类的方法 public char charAt(int index)          返回字符串index个字符 public int length()         返回字符串长 ...