HDU 5489 Removed Interval DP 树状数组
题意:
给一个长度为\(N\)的序列,要删除一段长为\(L\)的连续子序列,问所能得到的最长的\(LIS\)的长度。
分析:
设\(f(i)\)表示以\(a_i\)结尾的\(LIS\)的长度,设\(g(i)\)表示以\(a_i\)结尾而且被删去一段长为\(L\)的\(LIS\)的长度。
则有状态转移方程:
\(g(i)=max( g(j) , f(j)_{j<i-L} )\)
用树状数组维护一下。
我用二分也写了一遍,WA掉了,不知道怎么改。 = =||
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100000 + 10;
int n, L;
int a[maxn], b[maxn];
int f[maxn], g[maxn];
int C1[maxn], C2[maxn];
inline int lowbit(int x) { return x & (-x); }
void Change(int* C, int x, int v) {
while(x <= n) {
C[x] = max(C[x], v);
x += lowbit(x);
}
}
int Query(int* C, int x) {
int ans = 0;
while(x) {
ans = max(ans, C[x]);
x -= lowbit(x);
}
return ans;
}
int main() {
//freopen("in.txt", "r", stdin);
int T; scanf("%d", &T);
for(int kase = 1; kase <= T; kase++) {
scanf("%d%d", &n, &L);
for(int i = 1; i <= n; i++) {
scanf("%d", a + i);
b[i] = a[i];
}
sort(b + 1, b + 1 + n);
for(int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + 1 + n, a[i]) - b;
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
memset(C1, 0, sizeof(C1));
memset(C2, 0, sizeof(C2));
int ans = 0;
for(int i = L + 1; i <= n; i++) {
g[i] = max(Query(C1, a[i] - 1), Query(C2, a[i] - 1)) + 1;
ans = max(ans, g[i]);
Change(C2, a[i], g[i]);
f[i - L] = Query(C1, a[i - L] - 1) + 1;
Change(C1, a[i - L], f[i - L]);
}
ans = max(ans, f[n - L]); //可能是去掉最后L个
printf("Case #%d: %d\n", kase, ans);
}
return 0;
}
HDU 5489 Removed Interval DP 树状数组的更多相关文章
- HDU 2836 Traversal 简单DP + 树状数组
题意:给你一个序列,问相邻两数高度差绝对值小于等于H的子序列有多少个. dp[i]表示以i为结尾的子序列有多少,易知状态转移方程为:dp[i] = sum( dp[j] ) + 1;( abs( he ...
- 2015合肥网络赛 HDU 5489 Removed Interval LIS+线段树(树状数组)
HDU 5489 Removed Interval 题意: 求序列中切掉连续的L长度后的最长上升序列 思路: 从前到后求一遍LIS,从后往前求一遍LDS,然后枚举切开的位置i,用线段树维护区间最大值, ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)
1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 793 Solved: 503[Submit][S ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组
题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row andnumbered 1..N. The cows ...
- 奶牛抗议 DP 树状数组
奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
随机推荐
- CSS grid layout demo 网格布局实例
直接 上代码,里面我加注释,相当的简单, 也可以去我的github上直接下载代码,顺手给个星:https://github.com/yurizhang/micro-finance-admin-syst ...
- 将SpringBoot默认使用的tomcat替换为undertow
随着微服务的兴起,越来越多的互联网应用在选择web容器时使用更加轻量的undertow或者jetty.SpringBoot默认使用的容器是tomcat,如果想换成undertow容器,只需修改pom. ...
- JsonModel&AFNetWorking
// HttpManager.h // JsonModel&AFNetWorking // // Created by qianfeng on 15/7/21. // Copyright (c ...
- 集合(List、Set)
第19天 集合 第1章 List接口 我们掌握了Collection接口的使用后,再来看看Collection接口中的子类,他们都具备那些特性呢? 接下来,我们一起学习Collection中的常用几个 ...
- Android端WebRTC点对点互连
项目准备 信令服务器代码:https://github.com/matthewYang92/WebRtcServer(代码改自ProjectRTC) 安装Node.js 进入项目根目录,命令行:npm ...
- iOS-浅谈runtime运行时机制02-runtime简单使用
http://blog.csdn.net/jiajiayouba/article/details/44201079 由于OC是运行时语言,只有在程序运行时,才会去确定对象的类型,并调用类与对象相应的方 ...
- Python+selenium之获取验证信息
通常获取验证信息用得最多的几种验证信息分别是title,URL和text.text方法用于获取标签对之间的文本信息. 代码如下: from selenium import webdriverimpor ...
- HDU Rabbit and Grass 兔子和草 (Nim博弈)
思路:简单Nim博弈,只需要将所给的数字全部进行异或,结果为0,则先手必败.否则必胜. #include <iostream> using namespace std; int main( ...
- 2017年团体程序设计天梯赛 - 大区赛 L3-3
题意:有向图找哈密顿回路 比赛的时候剪枝只剪了vis 状压没剪对 反而只拿17分... 比赛结束后还去看了一发这个NP问题的QB(快速回溯法...但是对于本题好像大材小用...) 上网看了一个神犇的写 ...
- 删除Chrome地址栏记录中自动补全的网址
为了删除某个自动补全的网站,多年的历史纪录没了,还浪费我十多分钟,蠢哭_(:з」∠)_ 不是历史记录.不是清除浏览器数据.不是myactivity(谷歌账号)中的历史纪录,直接在书签中搜索,删除,OK ...