标签:des style blog http io ar color os sp
二分求LIS
对每一个位置为终点的LIS记录开头的最靠右边的值....
3 1 2 3 2 2 1
1 3
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long int LL;
const int maxn=100100;
int a[maxn],c[maxn],w[maxn],p[maxn],l[maxn];
int len,n;
LL ans;
void init()
{
ans=0; len=0;
memset(w,-1,sizeof(w));
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
for(int i=0;i<n;i++) scanf("%d",a+i);
len=1; c[0]=a[0]; l[0]=1; p[0]=0; w[0]=0;
for(int i=1;i<n;i++)
{
int j=lower_bound(c,c+len,a[i])-c;
c[j]=a[i];
if(j==0) w[j]=i;
else w[j]=max(w[j],w[j-1]);
p[i]=w[j];
l[i]=j+1;
if(j>=len) len++;
}
bool flag=false;
int l1=-1;
for(int i=0;i<n;i++)
{
if(l[i]==len)
{
flag=true;
l1=max(l1,p[i]);
}
if(flag)
{
ans+=l1+1;
}
}
cout<<ans<<endl;
}
return 0;
}
标签:des style blog http io ar color os sp
原文地址:http://blog.csdn.net/ck_boss/article/details/41868113