题意:给你一个序列,问相邻两数高度差绝对值小于等于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 + 树状数组的更多相关文章

  1. HDU 5489 Removed Interval DP 树状数组

    题意: 给一个长度为\(N\)的序列,要删除一段长为\(L\)的连续子序列,问所能得到的最长的\(LIS\)的长度. 分析: 设\(f(i)\)表示以\(a_i\)结尾的\(LIS\)的长度,设\(g ...

  2. 树形DP+树状数组 HDU 5877 Weak Pair

    //树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...

  3. bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)

    1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 793  Solved: 503[Submit][S ...

  4. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  5. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  6. 【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 ...

  7. 奶牛抗议 DP 树状数组

    奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...

  8. 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 ...

  9. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

随机推荐

  1. linux下的汇编环境搭建(nasm)

    第一步:先判断系统是否已经安装了nasm--------------->打开终端,执行whereis nasm :如果显示nasm: /usr/bin/nasm ,则已经安装:如果只显示nasm ...

  2. 【js】undefined

    alert(a); function name(parameters) { alert(parameters); } var a; name(a); ---输出结果--- underfind unde ...

  3. c语言编程之二叉树

    利用链表建立二叉树,完成前序遍历.中序遍历.后序遍历. 建立二叉树用的是前序遍历建立二叉树: #include<stdio.h> #include<stdlib.h> #inc ...

  4. Struts2结合sitemesh3制作网站母版页面

    上一篇文章介绍了sitemesh3的使用,这篇文章来介绍如何结合struts2来配置和使用sitemesh,具体的如何使用sitemesh3我就不讲解了,这个你们可以看看我的上一篇博客. 首先你要添加 ...

  5. tomcat配置及使用 环境变量设置

    Tomcat的配置及测试: 第一步:下载tomcat,然后解压到任意盘符 第二步:配置系统环境变量 我这里是tomcat5.5,解压到的D盘 (路径为: D:\Program Files\tomcat ...

  6. Careercup - Google面试题 - 5732809947742208

    2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...

  7. MySQL数据库双机热备份

    MySQL数据库双机热备份 1.mysql 数据库没有增量备份的机制 当数据量太大的时候备份是一个很大的问题.还好 mysql 数据库提供了一种主从备份的机制,其实就是把主数据库的所有的数据同时写到备 ...

  8. Codeforces Round #347 (Div. 2) C. International Olympiad 找规律

    题目链接: http://codeforces.com/contest/664/problem/C 题解: 这题最关键的规律在于一位的有1989-1998(9-8),两位的有1999-2098(99- ...

  9. The service ‘xxx’ configured for WCF is not registered with the Autofac container

    最近在使用autofac.wcf时,报如下异常: Exception Details: System.InvalidOperationException: The service 'xxx' conf ...

  10. 【Asp.net MVC ---杂七杂八】

    @RenderSection 母模板:_mainLayout.cshtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio ...