hdu 5489 Removed Interval 2015合肥网络赛 树状数组 dp 离散化/dp
来源:互联网 发布:python高级编程最新版 编辑:程序博客网 时间:2024/05/16 16:14
题目
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5489
题目来源:2015合肥网络赛第三题。题目简洁处理起来挺巧妙的。
简要题意:求长度为
N 的序列移掉长度为L 的一段后LIS的最大长度。数据范围:
T⩽100;ΣN⩽5000001⩽N⩽105;0⩽L⩽N;Ai⩽109;
题解
维护一个树状数组保存
query(x)
求出结尾不超过x 含有L 空隙的最长LIS的长度。对于当前位置来说以当前值为结尾的LIS共有两种来源:
第一种是前面含有
L 空隙,这种就是query(a[i]-1)+1
,取出结尾严格小于a[i] 的最大的LIS接上。第二种是在最后插入一个
L 空隙,这个需要用到[1,i−L] 的LIS信息,这个直接顺便做就行了。UPD:由于GDUT某人给了我一份精彩的代码,我看完之后理解了下补在了后面。
实现
首先要会
Θ(nlogn) 求LIS的方法也就是lower_bound()
乱搞。然后
Ai 比较大所以要离散化一下。经过这场比赛也算是会用点树状数组了。
代码
#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 secondusing 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;}// headconst int N = 1e5+5;const int INF = 0x3f3f3f3f;struct BIT { int tree[N] ; 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[N];int a[N];int dis[N];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−l 以ai−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 secondusing 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;}// headconst int N = 1E5+5;const int INF = 0x3f3f3f3f;int lis[N];int glis[N];int a[N];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;}
- hdu 5489 Removed Interval 2015合肥网络赛 树状数组 dp 离散化/dp
- HDU 5489 Removed Interval(2015合肥站网赛1006,DP+树状数组)
- 2015合肥网络赛 HDU 5489 Removed Interval LIS+线段树(树状数组)
- HDU 5489 Removed Interval (dp+线段树) 2015合肥网络赛
- hdu5489 ||2015合肥网络赛1006 dp+离散化树状数组优化
- HDU 5489 Removed Interval (合肥网络赛 1006 )
- hdu 5493 Queue 2015合肥网络赛 树状数组 二分 离散化 贪心
- HDU 5489 Removed Interval(DP)
- 【树状数组 + 离散化 + DP】 HDU 5542
- HDU 3450 Counting Sequences(树状数组+DP+离散化)
- HDU 5542 ccpc 树状数组优化dp +离散化
- hdoj4991Ordered Subsequence【dp+离散化+树状数组】
- LightOJ-1085-树状数组,离散化,dp
- 1002(dp+树状数组+离散化)
- 2015 南阳 CCPC hdu 5542 The Battle of Chibi(DP+树状数组优化+离散化)
- hdu 5489 Removed Interval
- http://acm.hdu.edu.cn/showproblem.php?pid=2227&&树状数组+离散化+dp
- hdu 2227 Find the nondecreasing subsequences【离散化+树状数组+DP思路】
- HDU 3177 Crixalis's Equipment 贪心
- 5. 封装和继承
- 代码大全读书笔记(1)
- NYOJ-263解题报告
- java中wait和sleep的区别
- hdu 5489 Removed Interval 2015合肥网络赛 树状数组 dp 离散化/dp
- 在proteus软件80C51芯片隐藏电源接口VCC
- Block传值和传值之间的注意事项
- memory prefetch浅析
- 【UIKit-110-6】#import <UIKit/UITableView.h>外观
- SQL改写, 聚合语句, left join 改写标量子查询
- 学生选课系统(1)-功能模块
- xUtils基本介绍
- 字符编码(一)