HDU3450-Counting Sequences
来源:互联网 发布:杠杆炒股盈利算法 编辑:程序博客网 时间:2024/06/06 09:44
Counting Sequences
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)Total Submission(s): 2362 Accepted Submission(s): 826
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 21 3 7 5
Sample Output
4
Source
2010 ACM-ICPC Multi-University Training Contest(2)——Host by BUPT
题意:给出n个数,统计满足相邻两个数之差不超过d的子序列个数。
解题思路:树状数组
对于每项a[i],则以[a[i]-d,a[i]+d]范围内的数结尾的子串,加上a[i],就可以成为符合要求的另一个子串,用树状数组维护。
树状数组sum(x)记录的是以1~x结尾的符合要求的子串个数,则sum(y)-sum(z-1)+1,就是[z,y]范围子串加上a[i]后新增子串,还要加上a[i]本身成串。
#include<cmath>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int mod = 9901;const int N = 4e5 + 10;int n,d,a[N],b[N],f[N];int low(int x){ return x&-x;}int sum(int x){ int sum=0; while(x>0) { sum+=f[x]; sum%=mod; x-=low(x); } return sum;}void add(int x,int v){ while(x<=n) { f[x]+=v; f[x]%=mod; x+=low(x); }}int main(){ while(~scanf("%d %d",&n,&d)) { memset(f,0,sizeof f); for(int i=1; i<=n; i++) scanf("%d",&a[i]),b[i]=a[i]; sort(b+1,b+1+n); int ans=0; for(int i=1; i<=n; i++) { int k=lower_bound(b+1,b+1+n,a[i])-b; int l=lower_bound(b+1,b+1+n,a[i]-d)-b; int r=upper_bound(b+1,b+1+n,a[i]+d)-b-1; int x=(sum(r)-sum(l-1)); x=(x+mod)%mod; ans=(ans+x)%mod; add(k,(x+1)%mod); } printf("%d\n",ans); } return 0;}
0 0
- HDU3450 Counting Sequences
- HDU3450-Counting Sequences
- hdu3450——Counting Sequences
- HDU3450-Counting Sequences【树状数组
- [树状数组+DP]hdu3450 Counting Sequences
- HDU3450 Counting Sequences(树状数组+dp)
- #HDU3450#Counting Sequences(Dp+树状数组优化)
- hdu3450 Counting Sequences(dp+离散化+树状数组优化)
- Counting Sequences
- hdu3450
- HDU 3450 Counting Sequences
- hdu 3450 Counting Sequences
- hdu 3450 Counting Sequences
- HDU - 3450 Counting Sequences
- HDU 3450 Counting Sequences
- [TOJ 3541] Counting Sequences
- hdu 3450 Counting Sequences(树状数组)
- HDU 3450 Counting Sequences 树状数组
- 如果有人问你 SQL 注入的资料,请叫他看这份速查表
- 关于在g++编译C++过程中调用移动构造函数
- nodejs的第二个小程序
- 图像是怎么转换为JPEG图像的
- 传统产业、传统企业为什么要进行互联网转型?
- HDU3450-Counting Sequences
- 排序算法总结(C++)
- poi导出excel例子
- ListView列表
- 打造CommonAdapter
- Java面向对象-Integer与int的区别
- List的contains()和remove()方法探讨
- Spring Boot 启动加载数据 CommandLineRunner
- Mybatis中传参包There is no getter for property named 'XXX' in 'class java.lang.String'