您的位置:首页 > 大数据 > 人工智能

HDU 5141 LIS again(dp+线段树)

2014-12-12 00:45 381 查看
题意:给一串数,[i,j]这个区间的lis长度等于这串数的lis长度,问你这样的区间有几个。

思路不难想,dp[i][0]代表以i为结尾的lis长度,dp[i][1]代表能达到这个lis长度最右边的起点是哪个。那么这个可以用线段树去优化完成,按值建立线段树,找小于当前数区间的lis长度最大值,最大值相同就找最右边起点的。然后这个算完就可以开始递推答案了。

假设对于以j结尾的区间[i,j]是最短的了,那么包含这个区间的所有区间都可以,那么就是i到左边的长度乘以j到右边的长度,到下一个等于最长lis的结尾点k时,如果k的起点小于等于i那么就不用加了,因为这个区间之前已经被算过了,如果大于i,那么就应该是i到k起点的长度乘以k到结尾的长度,所以需要维护一样左边界,起点是-1。

PS:线段树真慢。要900多ms

AC代码:

//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll __int64
#define ull unsigned long long
#define eps 1e-8
#define NMAX 1000000005
#define MOD 1000000007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template<class T>
inline void scan_d(T &ret)
{
char c;
int flag = 0;
ret=0;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c == '-')
{
flag = 1;
c = getchar();
}
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
if(flag) ret = -ret;
}
struct SegTree
{
int len,s;
};
SegTree T[100005*4];
int mm[100005][2];//离散化后的最大及最右起点
int a[100005],b[100005],dp[100005][2];
map<int,int>mp;
void build(int l, int r, int rt)
{
T[rt].len = 0;
if(l == r) return;
int mid = (l+r)>>1;
build(lson);
build(rson);
}

void pushup(int rt)
{
if(T[rt<<1].len > T[rt<<1|1].len)
{
T[rt].len = T[rt<<1].len;
T[rt].s = T[rt<<1].s;
}
else if(T[rt<<1].len < T[rt<<1|1].len)
{
T[rt].len = T[rt<<1|1].len;
T[rt].s = T[rt<<1|1].s;
}
else
{
T[rt].len = T[rt<<1].len;
if(T[rt<<1].s > T[rt<<1|1].s) T[rt].s = T[rt<<1].s;
else T[rt].s = T[rt<<1|1].s;
}
}

void update(int L, int t1, int t2, int l, int r, int rt)
{
if(l == r)
{
T[rt].len = t1;
T[rt].s = t2;
return;
}
int mid = (l+r)>>1;
if(L <= mid) update(L,t1,t2,lson);
else update(L,t1,t2,rson);
pushup(rt);
}
int ha[2];
void query(int L, int R, int l, int r, int rt)
{
if(L <= l && R >= r)
{
if(T[rt].len == 0) return;
if(ha[0] < T[rt].len || (ha[0] == T[rt].len && ha[1] < T[rt].s))
{
ha[0] = T[rt].len;
ha[1] = T[rt].s;
}
return;
}
int mid = (l+r)>>1;
if(L <= mid) query(L,R,lson);
if(R > mid) query(L,R,rson);
}

int main()
{
#ifdef GLQ
freopen("input.txt","r",stdin);
// freopen("o1.txt","w",stdout);
#endif // GLQ
int n;
while(~scanf("%d",&n))
{
mp.clear();
for(int i = 0; i < n; i++)
{
scan_d(a[i]);
b[i] = a[i];
}
sort(b,b+n);
int nct = unique(b,b+n)-b;
for(int i = 0; i < nct; i++)
{
mp[b[i]] = i+1;
mm[i+1][0] = 0;
}
for(int i = 0; i < n; i++)
a[i] = mp[a[i]];
build(1,nct,1);
int qu=1;
for(int i = 0; i < n; i++)
{
ha[0] = ha[1] = 0;
if(a[i] != 1) query(1,a[i]-1,1,nct,1);
// if(i == 1) cout<<ha[0]<<" "<<ha[1]<<" "<<a[i]<<endl;
if(ha[0] == 0)
{
update(a[i],1,i,1,nct,1);
// if(i == 0) cout<<"kao"<<" "<<T[4].len<<endl;
mm[a[i]][0] = 1;
mm[a[i]][1] = i;
dp[i][0] = 1;
dp[i][1] = i;
}
else
{
qu = max(qu,ha[0]+1);
dp[i][0] = ha[0]+1;
dp[i][1] = ha[1];
if(ha[0]+1 == mm[a[i]][0] && ha[1] > mm[a[i]][1])
{
mm[a[i]][1] = ha[1];
update(a[i],ha[0]+1,ha[1],1,nct,1);
}
else if(ha[0]+1 > mm[a[i]][0])
{
mm[a[i]][0] = ha[0]+1;
mm[a[i]][1] = ha[1];
update(a[i],ha[0]+1,ha[1],1,nct,1);
}
dp[i][0] = mm[a[i]][0];
}
}
ll ans = 0;
int l = -1;
for(int i = 0; i < n; i++) if(dp[i][0] == qu)
{
if(dp[i][1] > l)
{
ans += (ll)(dp[i][1]-l)*(ll)(n-i);
l = dp[i][1];
}
}
printf("%I64d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: