HDU 2836 Traversal 简单DP + 树状数组
题意:给你一个序列,问相邻两数高度差绝对值小于等于H的子序列有多少个。
dp[i]表示以i为结尾的子序列有多少,易知状态转移方程为:dp[i] = sum( dp[j] ) + 1;( abs( height[i] - height[j] ) <= H )
由abs( height[i] - height[j] ) <= H 可得 height[i] - H <= height[j] <= height[i] + H
将序列中的数离散化,每个height对应一个id, 用树状数组求区间[ height[i] - H的id, height[i] + H的id ]内dp[j]的和,并且每次把新得到的dp[i]更新到树状数组中height[i]的id对应的位置。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; const int MAXN = ;
const int MOD = ; int dp[MAXN];
int height[MAXN];
int num[MAXN];
int C[MAXN];
int n, d; int lowbit( int x )
{
return x & ( -x );
} int Query( int x )
{
int res = ;
while ( x > )
{
res += C[x];
res %= MOD;
x -= lowbit(x);
}
return res;
} void Add( int x, int v )
{
while ( x <= n )
{
C[x] += v;
C[x] %= MOD;
x += lowbit(x);
}
return;
} int main()
{
while ( ~scanf( "%d%d", &n, &d ) )
{
for ( int i = ; i <= n; ++i )
{
scanf( "%d", &height[i] );
num[i] = height[i];
} sort( num + , num + n + );
int cnt = unique( num + , num + n + ) - num - ; memset( C, , sizeof(C) );
int ans = ;
dp[] = ;
for ( int i = ; i <= n; ++i )
{
int id = lower_bound( num + , num + cnt + , height[i] ) - num;
int left = lower_bound( num + , num + cnt + , height[i] - d ) - num;
int right = upper_bound( num + , num + cnt + , height[i] + d ) - num - ;
dp[i] = ( Query( right ) - Query( left - ) + ) % MOD;
ans += dp[i];
Add( id, dp[i] );
} if ( ans >= n ) ans -= n;
else ans = ;
printf("%d\n", ans % MOD );
}
return ;
}
HDU 2836 Traversal 简单DP + 树状数组的更多相关文章
- HDU 5489 Removed Interval DP 树状数组
题意: 给一个长度为\(N\)的序列,要删除一段长为\(L\)的连续子序列,问所能得到的最长的\(LIS\)的长度. 分析: 设\(f(i)\)表示以\(a_i\)结尾的\(LIS\)的长度,设\(g ...
- 树形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 6447 - YJJ's Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 Problem DescriptionYJJ is a salesman who has tra ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
随机推荐
- AJAX异步同步
为了更好的用户体验,AJAX的异步同步技术给了我们一个很好的用户体验下面是我做的一个例子. 1.客户端处理 UserId.HTML <!DOCTYPE html PUBLIC "-// ...
- yabeblog.me 关于Tomcat7部署 一台机器部署两个项目,一个用域名访问,一个用IP访问
该内容来自 http://www.yabeBlog.me,转载请说明出处. 1.使用IP访问的项目放在Tomcat7 的webapps目录下面:比如:AAA 2.使用域名访问的项目放在Tomcat7的 ...
- 详解C/C++预处理器
C/C++编译系统编译程序的过程为预处理.编译.链接.预处理器是在程序源文件被编译之前根据预处理指令对程序源文件进行处理的程序.预处理器指令以#号开头标识,末尾不包含分号.预处理命令不是C/C++语 ...
- Java 图形编程 二:布局管理器之边界布局
package second; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.Window ...
- 从零开始学ios开发(五):IOS控件(2),Slider
下面继续学习ios的其他控件,这次会使用到的控件有Slider,当然还有一些之前已经使用过的控件Label. 这次我们不新建一个project了,当然如果你愿意重新创建一个新的项目也完全可以,我们还是 ...
- MVC初学 - The type or namespace name 'DbContext' could not be found
问题: The type or namespace name 'DbContext' could not be found (are you missing a using directive or ...
- 第三章 DispatcherServlet详解
3.1.DispatcherServlet作用 DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring Io ...
- c++类中的静态成员
静态成员和非静态成员的区别: 类静态成员用static修饰,类的静态成员属于类本身,而不属于类的某个具体对象,静态成员被类的所有对象共享,因此某个对象对静态成员(数据成员)的修改对其对象是可见的.而类 ...
- js遍历集合
首先,描述一下问题.由于需要后台取出数据,遍历展示在table中,大体如下: 后台取出的是 Map<String , List<Object>>,下面是我写得比较朴素一点的写法 ...
- php 安全处理方案
Safe::mysqlSafe(); sql注入,升级5.3.6以上版本php 方案一:将所有请求中所有数据(get/post/cookie)实现mysql_escape_string进行安全处理. ...