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 ...
随机推荐
- P3930 SAC E#1 - 一道大水题 Knight
TLE,额 ,有空再写吧. #include<queue> #include<cstdio> #include<vector> #include<algori ...
- 2017年2月28日-----------乱码新手自学.net 之特性与验证
现在看asp.net MVC5自学已经到了第六章:数据注解与验证. 话得从以前看MVC music store(音乐商店项目)的源码说起, 最初看music store源码完全就是一脸懵逼,整个程序, ...
- Kendo MVVM 数据绑定(四) Disabled/Enabled
Kendo MVVM 数据绑定(四) Disabled/Enabled Disabled 和 Enabled 绑定可以根据 ViewModel 的某个属性值的 true,false 来设置 DOM 元 ...
- PADS 9.5封装向导 多一个管脚
使用PADS 9.5封装向导(Decal Wizard)建立封装(Decals) 时遇到封装的中间多了一个管脚,如图红圈位置,通过一番搜寻,才知道这是热焊盘,不需要就在右边的红圈处去掉勾选热焊盘即可.
- SQL Server一个特殊的阻塞案例分析2
最近发现一个非常奇怪的阻塞问题,如下截图所示(来自监控工具DPA),会话583被会话1036阻塞,而且阻塞发生在tempdb,被阻塞的SQL如下截图所示,会话等待类型为LCK_M_S 因为DPA工具不 ...
- Python3+Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )
!/usr/bin/env python -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )'''from selenium im ...
- SQL 事物回滚
转自https://www.cnblogs.com/delphinet/archive/2010/08/17/1801424.html 第一种: declare @iErrorCount ...
- 给VS2008 打补丁
vs2003到2008各版本如下: vs.net2003 Visual Studio .NET 2003 Enterprise Architect Visual Studio .NET 2003 En ...
- flash jquery 调用摄像头 vue chrome49浏览器
flash jquery 调用摄像头 vue chrome49浏览器 这个摄像头,不能一个页面加载多个,只能一个页面显示一次,所以 调用的时候,记得加v-if 把组件销毁,然后从新加载新的 <! ...
- 2018.4.15 Mac系统下如何使用StartUml画好需求分析的类图 (同样适用于windows)
Mac如何使用StartUml (同样适用于windows) 左侧边栏的英文含义及其用法 关联(Association) [关联关系]:是一种拥有的关系,它使一个类知道另一个类的属性和方法:如:老师与 ...