Another LIS
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 876 Accepted Submission(s): 307
Problem Description
There is a sequence firstly empty. We begin to add number from 1 to N to the sequence, and every time we just add a single number to the sequence at a specific position. Now, we want to know length of the LIS (Longest Increasing Subsequence) after every time's add.
Input
An integer T (T <= 10), indicating there are T test cases.
For every test case, an integer N (1 <= N <= 100000) comes first, then there are N numbers, the k-th number Xk means that we add number k at position Xk (0 <= Xk <= k-1).See hint for more details.
Output
For the k-th test case, first output "Case #k:" in a separate line, then followed N lines indicating the answer. Output a blank line after every test case.
Sample Input
1
3
0 0 2
Sample Output
Case #1:
1
1
2
Hint
In the sample, we add three numbers to the sequence, and form three sequences.
a. 1
b. 2 1
c. 2 1 3
Author
standy
Source
2010 ACM-ICPC Multi-University Training Contest(13)——Host by UESTC
解析:
由于当前每次插入会影响前面的位置,即前面数的位置由后面的操作决定。。。
所以我们考虑倒推。。。建一颗线段树,kong记录当前节点对应区间剩余的位置个数。。。
倒起将每次操作在线段树中维护出他的位置,再用nlogn的方法求lis
代码:
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100000+10;
int n,cha[N],pos[N],kong[4*N],c[N],len;
void read()
{
freopen("lis.in","r",stdin);
freopen("lis.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&cha[i]);
}
void up(int p)
{
kong[p]=kong[p<<1]+kong[(p<<1)+1];
}
void build(int p,int l,int r)
{
if(l==r)
{
kong[p]=1;
return ;
}
int m=(l+r)>>1;
build(p<<1,l,m);
build((p<<1)+1,m+1,r);
up(p);
}
void update(int p,int l,int r,int k,int t)
{
if(l==r)
{
kong[p]--;pos[t]=l;
return ;
}
kong[p]--;
int m=(l+r)>>1;
if(kong[p<<1]>=k)update(p<<1,l,m,k,t);
else update((p<<1)+1,m+1,r,k-kong[p<<1],t);
up(p);
}
int find(int x)
{
int l=1,r=len,m;
while(l<=r)
{
m=(l+r)>>1;
if(x>c[m])l=m+1;
else r=m-1;
}
return l;
}
void work()
{
build(1,1,n);
for(int i=n;i>=1;i--)
update(1,1,n,cha[i]+1,i);//把i插到cha[i]+1位置
for(int i=1;i<=n;i++)
{
int k=find(pos[i]);
len=max(len,k);
c[k]=pos[i];
printf("%d\n",len);
}
}
int main()
{
read();
work();
return 0;
}