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 ...
随机推荐
- parameter和argument的区别
根据网上一些资料,对parameter和argument的区别,做如下的简单说明.1. parameter是指函数定义中参数,而argument指的是函数调用时的实际参数.2. 简略描述为:param ...
- nginx demo
server_names_hash_bucket_size 512;upstream node_app { server 127.0.0.1:3000; } server { listen 80; s ...
- 计划:怎样理解水平集方法 ITK Level set V4 框架介绍
简易解释:在曲面中插入一个平面所形成的轮廓,即是该轮廓的水平集表示,可见,该轮廓的水平集表示有多个.对于图像分割,在图像力的驱动下曲面进行更新. 轮廓的数学表达有隐式和显式两种表达.用曲面演化代替Fr ...
- 树形dp求树的重心
Balancing Act http://poj.org/problem?id=1655 #include<cstdio> #include<cstring> #include ...
- 编译为 Release 与 Debug 的区别
class Program { static void Main(string[] args) { DoWork(); } static void DoWork() { new Person().Ru ...
- GhostDoc:生成.NET API文档的工具 (帮忙文档)
在 Sandcastle:生成.NET API文档的工具 (帮忙文档) 后提供另一个生成API文档的工具. 1) 准备工作 安装GhostDoc Proc. 收费的哦.... 这个工具的优势是不像 ...
- cookie中转注入实战
随着网络安全技术的发展,SQL注入作为一种很流行的攻击方式被越来越多的人所知晓.很多网站也都对SQL注入做了防护,许多网站管理员的做法就是添加一个防注入程序.这时我们用常规的手段去探测网站的SQL注入 ...
- uva 10304
最优二叉查找数 看了这位大牛 的博客 http://www.cnblogs.com/lpshou/archive/2012/04/26/2470914.html /****************** ...
- 一个很吊的swing循环生成窗口。
import javax.swing.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; publi ...
- MongoDB 性能优化五个简单步骤
MongoDB 一直是最流行的 NoSQL,而根据 DB-Engines Ranking 最新的排行,时下 MongoDB 已经击败 PostgreSQL 跃居数据库总排行的第四位,仅次于 Oracl ...