您的位置:首页 > 理论基础 > 计算机网络

hdu 5489 Removed Interval 2015合肥网络赛 树状数组 dp 离散化/dp

2015-09-28 22:58 477 查看

题目

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5489

题目来源:2015合肥网络赛第三题。题目简洁处理起来挺巧妙的。

简要题意:求长度为NN的序列移掉长度为LL的一段后LIS的最大长度。

数据范围:T⩽100;ΣN⩽5000001⩽N⩽105;0⩽L⩽N;Ai⩽109;T\leqslant 100;\quad \Sigma N\leqslant 500000\\1\leqslant N\leqslant 10^5;\quad 0\leqslant L\leqslant N;\quad A_i\leqslant 10^9;\quad

题解

维护一个树状数组保存
query(x)
求出结尾不超过xx含有LL空隙的最长LIS的长度。

对于当前位置来说以当前值为结尾的LIS共有两种来源:

第一种是前面含有LL空隙,这种就是
query(a[i]-1)+1
,取出结尾严格小于a[i]a[i]的最大的LIS接上。

第二种是在最后插入一个LL空隙,这个需要用到[1,i−L][1,i-L]的LIS信息,这个直接顺便做就行了。

UPD:由于GDUT某人给了我一份精彩的代码,我看完之后理解了下补在了后面。

实现

首先要会Θ(nlogn)\Theta(n\log n)求LIS的方法也就是
lower_bound()
乱搞。

然后AiA_i比较大所以要离散化一下。

经过这场比赛也算是会用点树状数组了。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 1e5+5;
const int INF = 0x3f3f3f3f;

struct BIT {
int tree
;

int lowbit(int x) {
return x&(-x);
}
void add(int x, int add, int n) {
for (; x <= n; x += lowbit(x)) {
tree[x] = max(add, tree[x]);
}
}
int sum(int x) {
int s = 0;
for (; x > 0; x -= lowbit(x)) {
s = max(s, tree[x]);
}
return s;
}
void clear() {
memset(tree, 0, sizeof tree);
}
};
BIT tree;
int lis
;
int a
;
int dis
;

int main()
{
int t, n, l, cas = 1;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &l);
memset(lis, INF, sizeof lis);
for (int i = 1; i <= n; i++) {
scanf("%d", a+i);
dis[i] = a[i];
}
sort(dis+1, dis+n+1);
int tot = unique(dis+1, dis+n+1)-dis-1;
for (int i = 1; i <= n; i++) {
a[i] = lower_bound(dis+1, dis+1+tot, a[i])-dis;
}

for (int i = l+1; i <= n; i++) {
int pos = lower_bound(lis+1, lis+tot+1, a[i-l])-lis;
lis[pos] = a[i-l];
tree.add(a[i], tree.sum(a[i]-1)+1, tot);
tree.add(a[i-l], pos, tot);
}

printf("Case #%d: %d\n", cas++, tree.sum(tot));
tree.clear();
}
return 0;
}


脑洞题解

GDUT某人很神奇地几行代码就A掉了这题,我就学习了下。先称题目中的lis为glis吧。

我们先假设之前的glis被正确更新。

首先如果之前已经有了间隙,glis前面的部分一定是个曾经更新过的glis,保存在里面。

然后如果当前插入一个空隙,它是一个glis,那去掉末尾那个,之前的也一定是个glis,也被保存了。

如果我们进行更新,保证之前的glis都被更新了,那么只需要对glis像对普通lis那样先更新一次。

然后再根据a1⋯ai−la_1\cdots a_{i-l}以ai−la_{i-l}结尾的lis去更新glis,由于之前的glis已经完全更新,我们只需要去看当前lis的位置和当前glis对应的位置进行更新即可。

于是我们就得到了新的正确的glis。

整个思考是一个数归的思考方式,代码很短,思路很脑洞也很神奇。

脑洞代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 1E5+5;
const int INF = 0x3f3f3f3f;

int lis
;
int glis
;
int a
;

int main()
{
int t, cas = 1, n, l;
scanf("%d", &t);
while (t--) {
memset(lis, INF, sizeof lis);
memset(glis, INF, sizeof glis);
scanf("%d%d", &n, &l);
for (int i = 0; i < n; i++) {
scanf("%d", a+i);
}
for (int i = l; i < n; i++) {
*lower_bound(glis, glis+n, a[i]) = a[i];
int pos = lower_bound(lis, lis+n, a[i-l])-lis;
lis[pos] = a[i-l];
glis[pos] = min(lis[pos], glis[pos]);
}
printf("Case #%d: %d\n", cas++, lower_bound(glis, glis+n, INF)-glis);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: