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 ...
随机推荐
- nodejs是单线程
你不妨先思考一个问题:在单核时代,PHP之类多线程或者多进程的,是怎么处理并发的?是排队吗? 答案是:的确就是排队.但是并不是一定要处理完请求1才能去处理请求2:实际上请求的处理过程中,有很多的时间是 ...
- iOS学习笔记之typedef
typedef unsigned long long weiboId; typedef 定义一个使用方便的类型,谓之为“宏定义“. unsigned long long 是一种无符号的长长整型.本应该 ...
- jquery定义表格宽度
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Error: Cannot find module 'express'
安装Express命令如下: npm install -g express 安装成功之后会在C:\Users\[YOUR_USER_NAME]\AppData\Roaming\npm\node_mod ...
- 自定义PopupWindow 怎么设置PopupWindow的宽度充满全屏宽度
自定义了一个MyPopMenu类,用于上图中的下拉筛选效果的. 但是按照网上有说需要: new PopupWindow(view,getWindowManager().getDefaultDispla ...
- 解决在ubuntu下requests 无法找到模块packages
我明明用pip install requests安装成功了,但是依然报下面的错 错误1 requests.packages.urllib3.disable_warnings()AttributeErr ...
- android禁止ScrollView自动滚动
当Scrollview嵌套listview,或者子View的内容过多时,当内容加载完成后,ScrollView中内容的长度会发生改变,ScrollView会自动往下滚动,解决办法:在ScollView ...
- mysql数据库连接池 手动编写
源码来源于http://www.toutiao.com/a6350448676050174209/,留存以供以后参考学习 先上一张项目托普图 然后分别列出各个文件的源码: MyPool.java(就是 ...
- Cent Os 常用操作
开放端口 编辑iptables文件(/etc/sysconfig/iptables) -A INPUT -m state --state NEW -m tcp -p tcp --dport xx端口号 ...
- HDU4276 The Ghost Blows Light SPFA&&树dp
题目的介绍以及思路完全参考了下面的博客:http://blog.csdn.net/acm_cxlove/article/details/7964739 做这道题主要是为了加强自己对SPFA的代码的训练 ...