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. noi 666 放苹果

    题目链接:http://noi.openjudge.cn/ch0206/666/ 和ural 1114题意类似,但是有顺序,5,1,1和1,5,1是同一种序列.不能直接枚举 d(i,j) 前 i 个盘 ...

  2. [转](一)unity4.6Ugui中文教程文档-------概要

    转载请注明出处:http://blog.csdn.net/u010019717更全的内容请看我的游戏蛮牛地址:http://www.unitymanual.com/forum.php?mod=guid ...

  3. 《BI项目笔记》创建标准维度、维度自定义层次结构

  4. HDU-4531 吉哥系列故事——乾坤大挪移 模拟

    题意:给定一个九宫格,然后能够选择某行或者是某列滚动,每个小方格分为上下左右四个块,每个块可以涂上4种不同的颜色.问最少使用多少步能够使得所有相同颜色相互联通. 分析:由于九宫格的所有的状态只有9!( ...

  5. jQuery之元素操作及事件绑定

    1.操作元素之属性: ①attr读:("selector").attr("属性名"):=>getAttribute("属性名"):改: ...

  6. 【转】Linux系统启动过程分析

    [转]Linux系统启动过程分析 转自:http://blog.chinaunix.net/uid-23069658-id-3142047.html 经过对Linux系统有了一定了解和熟悉后,想对其更 ...

  7. springMVC的两种下载方式

    1:通过httpServletResponse对象实现下载,觉得LOW的自行跳过 2:有人觉得既然使用的是MVC就要使用spring的方式

  8. @Scheduled 注解

    Spring配置文件xmlns加入 <!---加入:xmlns:task="http://www.springframework.org/schema/task"--> ...

  9. Google Analytics与百度统计原理

    Google Analytics与百度统计的统计数据都是有差异的,而且差异非常大,其根本原因在于数据统计的原理和机制是不同的.下面分享一下两位博友李鑫和赵高欣对Google Analytics与百度统 ...

  10. 编程之美----NIM游戏

    : 博弈游戏·Nim游戏 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 今天我们要认识一对新朋友,Alice与Bob.Alice与Bob总是在进行各种各样的比试,今天他 ...