Counting Sequences
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)Total Submission(s): 2495 Accepted Submission(s): 877
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
题意:给出一个数列,统计当前数列中相邻元素差值不大于d的完美子序列个数
Dp[i]表示以i为结尾的完美子序列个数:Dp[i] = Σ(Dp[j]) abs(Dp[i] - Dp[j]) <= d
需要将A[i]离散化
按从1到N的顺序找区间为[A[i] - d, A[i] + d]的数结尾的完美子序列个数,并求和,
因为是从1到n,并且是求和后再放入树中,所以不会重复计算。
Status | Accepted |
---|---|
Time | 249ms |
Memory | 2324kB |
Length | 1251 |
Lang | G++ |
Submitted | 2017-05-22 14:56:40 |
Shared |
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int Max = 100005;
const int MOD = 9901;
int N, D, fro, back;
int A[Max], B[Max], Tr[Max];
int getint(){
char c; int flag = 1, num = 0;
while((c = getchar()) < '0' || c > '9') if(c == '-') flag = -1;
while(c >= '0'&& c <= '9'){ num = num * 10 + c - 48; c = getchar();}
num *= flag;
return num;
}
int lowbit(int x){return -x&x;}
void updata(int x, int val){ for( ; x <= Max; Tr[x] += val, x += lowbit(x));}
int get_sum(int x){
int sum = 0;
for(int i = x; i; sum = (Tr[i] + sum) % MOD, i -= lowbit(i));
return sum;
}
int main(){
while(~scanf("%d%d", &N, &D)){
memset(Tr, 0, sizeof(Tr));
for(int i = 1; i <= N; ++ i)
A[i] = getint(), B[i] = A[i];
sort(B + 1, B + 1 + N);
int len = unique(B + 1, B + 1+ N) - B - 1;
int Ans = 0;
for(int i = 1; i <= N; ++ i){
int r = upper_bound(B + 1, B + 1 + len, A[i] + D) - (B + 1);
int l = lower_bound(B + 1, B + 1 + len, A[i] - D) - (B + 1);
int pos = lower_bound(B + 1, B + 1 + len, A[i]) - B;
int sum = ((get_sum(r) - get_sum(l))%MOD + MOD)%MOD;
Ans = (Ans + sum) % MOD;
updata(pos, sum + 1);
}
printf("%d\n", Ans);
}
return 0;
}