hdu3450 Counting Sequences(dp+离散化+树状数组优化)
来源:互联网 发布:阳江市问政网络平台 编辑:程序博客网 时间:2024/06/04 23:22
Problem Description
For a set of sequences of integers{a1,a2,a3,…an}, we define a sequence{ai1,ai2,ai3…aik}in which 1<= i1 < i2< i3<…< ik<=n, as the sub-sequence of {a1,a2,a3,…an}. It is quite obvious that a sequence with the length n has 2^n sub-sequences. And for a sub-sequence{ai1,ai2,ai3…aik},if it matches the following qualities: k >= 2, and the neighboring 2 elements have the difference not larger than d, it will be defined as a Perfect Sub-sequence. Now given an integer sequence, calculate the number of its perfect sub-sequence.
Input
Multiple test cases The first line will contain 2 integers n, d(2<=n<=100000,1<=d=<=10000000) The second line n integers, representing the suquence
Output
The number of Perfect Sub-sequences mod 9901
Sample Input
4 2
1 3 7 5
Sample Output
4
大致题意:给你n个数,让你找出找出长度大于2,且相邻两个数的差值不大于d的子序列的个数
思路:和hdu2227那题差不多,也是先离散化,然后用树状数组来优化dp,最后取模的时候要注意下。
代码如下
#include<cstring> #include<cstdio> #include<iostream> #include <algorithm> #define ll long long int using namespace std;const int N=1e5+5; const int mod=9901;int dp[N];int n,d;int a[N],b[N],HASH[N];int tot;int lowbit(int x){ return x&-x;}int sum(int x){ int s=0; while(x>0) { s=(s+dp[x])%mod; x=x-lowbit(x); } return s;}void add(int x,int date){ while(x<=n) { dp[x]=(dp[x]+date)%mod; x=x+lowbit(x); }}int findR(int v){ int i=1,j=tot+1,mid; while(i<j) { mid=(i+j)/2; if(HASH[mid]<=v) i=mid+1; else j=mid; } return i-1;}int findL(int v){ int i=1,j=tot+1,mid; while(i<j) { mid=(i+j)/2; if(HASH[mid]<v) i=mid+1; else j=mid; } return i-1;}int main() { while(~scanf("%d%d",&n,&d)) { memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); b[i]=a[i]; } sort(b+1,b+n+1); tot=1; HASH[1]=b[1]; for(int i=2;i<=n;i++) if( b[i]!=b[i-1] ) HASH[++tot]=b[i]; for(int i=1;i<=n;i++) { int L=findL(a[i]-d),id=findL(a[i])+1,R=findR(a[i]+d); int tmp=sum(R)-sum(L); add(id,tmp+1); } printf("%d\n",((sum(tot)-n)%mod+mod)%mod); } return 0; }
- hdu3450 Counting Sequences(dp+离散化+树状数组优化)
- #HDU3450#Counting Sequences(Dp+树状数组优化)
- HDU3450 Counting Sequences(树状数组+dp)
- [树状数组+DP]hdu3450 Counting Sequences
- hdoj 3450 Counting Sequences 【离散化 + 树状数组优化dp】
- HDU3450-Counting Sequences【树状数组
- HDU 3450 Counting Sequences(树状数组+DP+离散化)
- hdu 3450 Counting Sequences(树状数组+排序离散化)
- HDU 3450 Counting Sequences(DP + 树状数组)
- HDU3450 Counting Sequences
- HDU3450-Counting Sequences
- hdu3450之离散化+二分查找+树状数组
- hdu3450——Counting Sequences
- HDU 5862 Counting Intersections(离散化+树状数组)
- HDU 5542 ccpc 树状数组优化dp +离散化
- 树状数组专题(十三)之hdu3450
- 2015 南阳 CCPC hdu 5542 The Battle of Chibi(DP+树状数组优化+离散化)
- hdu 3450 Counting Sequences(树状数组)
- Picker mk
- C/C++语言调用lua函数和编译问题
- 键盘虚拟码
- 107. Binary Tree Level Order Traversal II(BFS)
- 排序算法Day1
- hdu3450 Counting Sequences(dp+离散化+树状数组优化)
- UVA
- Maven面试宝典
- Hadoop1.x HDFS系统架构
- Android制作lib--可依赖三方库
- OpenCV实现Matlab的fft2、ifft2函数
- 网络层次与主要设备的对应关系
- Ctex中插入Visio图的步骤
- 洛谷P2878 保护花朵