hdu 3450 Counting Sequences 树状数组+DP 求相邻两个数字的绝对值小于等于H并且序列长度的序列个数
2011-10-10 22:02
330 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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
[align=left]Output[/align]
The number of Perfect Sub-sequences mod 9901
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
//
显然是动态规划。dp[i]表示前i个数有多少个有效的子序列。那么 dp[i]=dp[i-1]+A。 A是前面i-1个数中,与i的差值不超过d的以该数结尾的有效的子序列的个数 的和。我们可以用另外一个数组sub[i]表示以i结尾的有效的子序列的个数。 dp与sub的不同之处是dp中的子序列不一定是以第i个数结尾的。
sub[i]= sigma sub[k] ,( abs(numk],num[i])<=d )。 由于求sub的时间复杂度为O(n^2),而n太大,因此需要离散化后用树状数组。
树状数组求和是一段连续的,而sub要求和的是位于区间[num[i]-d,num[i]+d],所以要对num排序,这样就能把
[num[i]-d,num[i]+d]放到连续的区间中。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=9901;
const int maxn=110000;
int n,d;
int dp[maxn];
int num[maxn],a[maxn];
int pos(int x)//求最大的<=x的数的位置
{
a[n+1]=1<<30;
int l=1,r=n+1;
while(l<r)
{
int mid=(l+r)>>1;
if(a[mid]>x) r=mid;
else l=mid+1;
}
return l-1;
}
int c[maxn];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int val)
{
for(int i=x;i<=n;i+=lowbit(i))
{
c[i]+=val;if(c[i]>=mod) c[i]-=mod;
}
}
int getsum(int x)
{
int cnt=0;
for(int i=x;i>=1;i-=lowbit(i))
{
cnt+=c[i];if(cnt>=mod) cnt-=mod;
}
return cnt;
}
int main()
{
while(scanf("%d%d",&n,&d)==2)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
a[i]=num[i];
}
sort(a+1,a+1+n);
memset(c,0,sizeof(c));
dp[0]=0;
for(int i=1;i<=n;i++)
{
int ps=pos(num[i]);
int k1=pos(num[i]-d-1);//important -1
int k2=pos(num[i]+d);
int cnt=getsum(k2)-getsum(k1);
cnt=(cnt%mod+mod)%mod;
update(ps,cnt+1);//important cnt+1
dp[i]=(dp[i-1]+cnt)%mod;
}
printf("%d\n",dp
);
}
return 0;
}
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.
[align=left]Input[/align]
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
[align=left]Output[/align]
The number of Perfect Sub-sequences mod 9901
[align=left]Sample Input[/align]
4 2 1 3 7 5
[align=left]Sample Output[/align]
4
//
显然是动态规划。dp[i]表示前i个数有多少个有效的子序列。那么 dp[i]=dp[i-1]+A。 A是前面i-1个数中,与i的差值不超过d的以该数结尾的有效的子序列的个数 的和。我们可以用另外一个数组sub[i]表示以i结尾的有效的子序列的个数。 dp与sub的不同之处是dp中的子序列不一定是以第i个数结尾的。
sub[i]= sigma sub[k] ,( abs(numk],num[i])<=d )。 由于求sub的时间复杂度为O(n^2),而n太大,因此需要离散化后用树状数组。
树状数组求和是一段连续的,而sub要求和的是位于区间[num[i]-d,num[i]+d],所以要对num排序,这样就能把
[num[i]-d,num[i]+d]放到连续的区间中。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=9901;
const int maxn=110000;
int n,d;
int dp[maxn];
int num[maxn],a[maxn];
int pos(int x)//求最大的<=x的数的位置
{
a[n+1]=1<<30;
int l=1,r=n+1;
while(l<r)
{
int mid=(l+r)>>1;
if(a[mid]>x) r=mid;
else l=mid+1;
}
return l-1;
}
int c[maxn];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int val)
{
for(int i=x;i<=n;i+=lowbit(i))
{
c[i]+=val;if(c[i]>=mod) c[i]-=mod;
}
}
int getsum(int x)
{
int cnt=0;
for(int i=x;i>=1;i-=lowbit(i))
{
cnt+=c[i];if(cnt>=mod) cnt-=mod;
}
return cnt;
}
int main()
{
while(scanf("%d%d",&n,&d)==2)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
a[i]=num[i];
}
sort(a+1,a+1+n);
memset(c,0,sizeof(c));
dp[0]=0;
for(int i=1;i<=n;i++)
{
int ps=pos(num[i]);
int k1=pos(num[i]-d-1);//important -1
int k2=pos(num[i]+d);
int cnt=getsum(k2)-getsum(k1);
cnt=(cnt%mod+mod)%mod;
update(ps,cnt+1);//important cnt+1
dp[i]=(dp[i-1]+cnt)%mod;
}
printf("%d\n",dp
);
}
return 0;
}
相关文章推荐
- 2014 GCJ Round 1B New Lottery Game(数位dp,x小于等于A,y小于等于B,并且x&y值小于等于K的数字个数)
- 空格替换 请编写一个方法,将字符串中的空格全部替换为“%20”。假定该字符串有足够的空间存放新增的字符,并且知道字符串的真实长度(小于等于1000),同时保证字符串由大小写的英文字母组成。 给定一
- 题意:给两个序列(长度达10^5,序列内的数都不同且都小于10^5),求这两个序列的最长公共序列的长度
- (hdu step 9.1.1)A == B ?(在这个数字有可能是大数并且存在无效0的情况下,判断这两个数字是否相等)
- 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
- 请编写一个方法,将字符串中的空格全部替换为“%20”。假定该字符串有足够的空间存放新增的字符,并且知道字符串的真实长度(小于等于1000),同时保证字符串由大小写的英文字母组成。 给定一个string
- hdu 4352 统计数字数位上最长上升子序列长度为k的个数
- 请编写一个方法,将字符串中的空格全部替换为“%20”。假定该字符串有足够的空间存放新增的字符,并且知道字符串的真实长度(小于等于1000),同时保证字符串由大小写的英文字母组成
- ZOJ1733 | | HDU1159简单的DP求两个字符串最大子序列的长度,没啥好说的,照着书上敲得。
- 求一串数字中——和最大的连续子序列; 求一串数字差值的绝对值最小的两个数字
- 匹配一个字符串,它由0、1、2组成,并且中间没有两个相邻的数字是相同的(比如,匹配1232101210321020,而不匹配1100123)
- HDU 2243 考研路茫茫——单词情结 求长度小于等于L的通路总数的方法
- 关于求两个数字序列最打子序列长度的一道程序题
- HDU 5569 长度为n的上升子序列个数d (大数模板+DP)
- 请编写一个方法,将字符串中的空格全部替换为“ ”。假定该字符串有足够的空间存放新增的字符,并且知道字符串的真实长度(小于等于1000),同时保证字符串由大小写的英文字母组成。
- hdu ztr loves lucky numbers (最小的但不小于n的幸运数字是多少,幸运数字只含4和7,且4的个数等于7的个数)
- 和为S的两个数字 & 和为S的连续正数序列
- HDU 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列的长度)
- 和为S的两个数字VS和为s的连续正数序列