Another LIS
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2100 Accepted Submission(s): 763
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
Recommend
zhouzeyong | We have carefully selected several similar problems for you: 2389 3293 1255 1540 1542
- 线段树部分 :采取从后到前面的考虑方式 :可以想象后面的数占有posx位置的优先于前面的数占有posx的位置 也就是从后往前不断地占有第posx的空位
- LIS
#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int c[N<<2],a[N],h[N],dp[N],len;
void build(int rt,int l,int r)
{
c[rt]=r-l+1;//当前的空位数
if(l==r)
{
return ;
}
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
}
void insert(int rt,int l,int r,int wz,int v)
{
if(l==r)
{
h[v]=l;c[rt]--; //x放在h[x]这个位置上
return ;
}
int mid=(l+r)>>1;
c[rt]--;
if(c[rt<<1]>=wz) insert(rt<<1,l,mid,wz,v);
else insert(rt<<1|1,mid+1,r,wz-c[rt<<1],v);//左儿子放不下就放到右儿子里面 注意wz的处理
}
int main()
{
int tt;scanf("%d",&tt);int cas=0;
while(tt--)
{
int n;scanf("%d",&n);build(1,1,n);
for(int i=1;i<=n;i++) dp[i]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=n;i>=1;i--)
insert(1,1,n,a[i]+1,i);
printf("Case #%d:\n",++cas);len=1;
for(int i=1;i<=n;i++)
{
if(i==1)
{
dp[1]=h[1];puts("1");
}else
{ // dp[k]表示LIS为k至少需要的结束点的位置在dp[k]之后
int k=lower_bound(dp+1,dp+1+len,h[i])-dp;
if(len<k) dp[k]=h[i],len=k;
else if(h[i]<dp[k]) dp[k]=h[i];//更新
printf("%d\n",len);
}
}
puts("");
}
}
二分《想法同上》
#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int c[N<<2],h[N],a[N],n,m,dp[N],len;
void build(int rt,int l,int r)
{
c[rt]=r-l+1;
if(l==r)
{
return;
}
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
}
void insert(int rt,int l,int r,int wz,int v)
{
if(l==r)
{
c[rt]--;h[v]=l;
return ;
}
int mid=(l+r)>>1;
if(c[rt<<1]>=wz) insert(rt<<1,l,mid,wz,v);
else insert(rt<<1|1,mid+1,r,wz-c[rt<<1],v);
c[rt]--;
}
int find(int x)
{
int l=1,r=len,mid;
while(l<=r)
{
mid=(l+r)>>1;
if(dp[mid]<x)
{
l=mid+1;
}else
{
r=mid-1;
}
}return l;
}
int main()
{
int tt,cas=0;scanf("%d",&tt);
while(tt--)
{
scanf("%d",&n);build(1,1,n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);dp[i]=0;
}
for(int i=n;i>=1;i--)
{
insert(1,1,n,a[i],i);
}
len=0;
printf("Case #%d:\n",++cas);
for(int i=1;i<=n;i++)
{
int k=find(h[i]);
if(k>len||dp[k]>h[i]) dp[k]=h[i];
len=max(len,k);
printf("%d\n",len);
}
puts("");
}
}