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 ...
随机推荐
- Java ActiveMQ 示例
所需引入Jar包: jms-1.1.jar activemq-all-5.15.0.jar 生产者 package com.mousewheel.demo; import javax.jms.Conn ...
- EF5 通用数据层 增删改查操作,泛型类
using System; using System.Collections.Generic; using System.Data.Entity.Infrastructure; using Syste ...
- Random类、ThreadLocalRandom类
Random和ThreadLocalRandom类均用于生成伪随机数. Random的构造函数: Random() 默认以系统当前时间为种子,相当于Random(System.currentT ...
- 关于React的require添加动态变化的路径
关于React的require添加动态变化的路径 直接这样写显然是不会有错误的 let path = require('../images/girl.png'); 但是如果你尝试着 var gg = ...
- 微信开发 config:invalid url domain
当遇到config:invalid url domain 有2种可能 1.没有配置url. 2.url配置错误.配置url如http://write.blog.csdn.NET/,就要这样配置writ ...
- javascript对象的学习
一.对象的定义: 对象是JavaScript的一个基本数据类型,是一种复合值,它将很多值(原始值或者其他对象)聚合在一起,可通过名字访问这些值.即属性的无序集合. JavaScript 提供多个内建对 ...
- apple-touch-icon-precomposed
<link rel="apple-touch-icon-precomposed" href=""> apple-touch-icon-precomp ...
- 《spss统计分析与行业应用案例详解》:实例九 单一样本t检验
单一样本t检验的功能与意义 spss的单一样本t检验过程是瑕设检验中最基本也是最常用的方法之一,跟所有的假没检验一样,其依剧的基木原理也是统计学中的‘小概率反证法”原理.通过单一样本t检验.我们可以实 ...
- SqlServer自定义排序
在实际项目中,有时会碰到数据库SQL的特殊排序需求,举几个例子,作为参考. 1.自定义优先级 一种常见的排序需求是指定某个字段取值的优先级,根据指定的优先级展示排序结果.比如如下表: Create T ...
- 发现知乎的一个Bug,并且我绕过了此Bug,沾沾自喜中...
发现问题 在知乎点击修改头像,上传图片时发现一片空白.凭着程序员的直觉,第一反应时看下控制台是否有报错.果然发现如下: Refused to load the image 'data:image/jp ...