Counting Sequences

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)

Total Submission(s): 2335    Accepted Submission(s): 820

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

思路:题目求满足要求的子串有多少个,那么我们应该固定序列的结尾点,dp[i],表示以i点为结尾的满足条件的序列有多少个,对于每一个点作为序列的最后一个点,都去找它前面所有满足条件的点k,然后加上dp[k],类似于动态规划,那么如何高效的求满足条件的点,即这个点前面的所有点,哪些是满足在a[i]-d,和a[i]+d.我们可以用线段树

另外,需要离散化一下,这里时间只有1秒钟,如果用map离散会超时,所以我们可以用二分法离散
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <map> using namespace std;
const int maxn=1e5;
typedef long long int LL;
LL sum[maxn*8+5];
int n,d;
void pushup(int node)
{
sum[node]=sum[node<<1]+sum[node<<1|1];
sum[node]%=9901;
}
void update(int node,int l,int r,int val,LL num)
{
if(l==r)
{
sum[node]+=num;
sum[node]%=9901;
return;
}
int mid=(l+r)>>1;
if(val<=mid)
update(node<<1,l,mid,val,num);
else
update(node<<1|1,mid+1,r,val,num);
pushup(node);
}
LL query(int node,int l,int r,int L,int R)
{
if(L<=l&&r<=R)
return sum[node]%9901;
int mid=(l+r)>>1;
LL ret=0;
if(L<=mid) ret+=query(node<<1,l,mid,L,R);
if(R>mid) ret+=query(node<<1|1,mid+1,r,L,R);
return ret%9901;
}
int a[maxn+5];
int c[maxn+5];
int b[maxn+5];
int cot;
int ans; int fun1(int k,int tag)
{
int l=1,r=cot;
while(l<=r)
{
int mid=(l+r)/2;
if(k<a[mid])
r=mid-1;
else
l=mid+1;
}
if(tag) return l;
return r;
}
int fun2(int k)
{
int l=1,r=cot;
while(l<=r)
{
int mid=(l+r)/2;
if(k==a[mid])
return mid;
else if(k<a[mid])
r=mid-1;
else
l=mid+1;
}
return l;
}
int main()
{
while(scanf("%d%d",&n,&d)!=EOF)
{ memset(sum,0,sizeof(sum)); for( int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
c[i]=b[i];
}
sort(b+1,b+n+1);
cot=1;a[1]=b[1];
for(int i=2;i<=n;i++)
{
if(b[i]!=b[i-1])
a[++cot]=b[i];
}
for(int i=1;i<=n;i++)
{
int r=fun1(c[i],0)-1;
int l=fun1(c[i],1)+1;
int k=fun2(c[i]); LL num=query(1,1,n,l,r);
ans+=num;
ans%=9901;
update(1,1,n,k,(num+1)%9901);
}
printf("%d\n",ans);
} return 0;
}

HDU 3450 Counting Sequences(线段树)的更多相关文章

  1. hdu 3450 Counting Sequences

    /* n*n暴力 这个很好想 */ #include<cstdio> #define maxn 100010 #define mod 9901 using namespace std; i ...

  2. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  3. hdu 5700区间交(线段树)

    区间交 Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...

  4. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  5. HDU 5862 Counting Intersections (树状数组)

    Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...

  6. HDU 6047 Maximum Sequence(线段树)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...

  7. Hdu 3564 Another LIS 线段树+LIS

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  8. HDU 5091---Beam Cannon(线段树+扫描线)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5091 Problem Description Recently, the γ galaxies bro ...

  9. HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. 简洁经常使用权限系统的设计与实现(一):构造权限菜单树的N(N&gt;=4)种方法

    权限系统.Web开发常见标准子系统之中的一个.结合自己的一些思考和实践,从本篇開始权限系统的设计与实现之路. 近期,重构了项目的权限菜单构造过程,向前端返回json格式的权限树. 这一篇.仅仅是大致介 ...

  2. UIWindow的一点儿思考

    转自:http://www.cnblogs.com/smileEvday/archive/2012/11/16/UIWindow.html 每一个IOS程序都有一个UIWindow,在我们通过模板简历 ...

  3. Machine Learning—The k-means clustering algorithm

    印象笔记同步分享:Machine Learning-The k-means clustering algorithm

  4. JVM基础学习之类的加载、链接和初始化

    本文我们一起讨论Java类的加载.链接和初始化. Java字节代码的表现形式是字节数组(byte[]),而Java类在JVM中的表现形式是 java.lang.Class类 的对象.一个Java类从字 ...

  5. 这不是bug,而是语言特性

    分析编程语言缺陷的一种方法是把所有的缺陷归于3类:不该做的做了,该做的没做,该做但做得不合适. 在使用switch case时,如果使用缺省的 fall through,请一定在旁边注释,因为97%的 ...

  6. 一款基于jQuery外观优雅带遮罩弹出层对话框

    今天我们要来分享一款基于jQuery的弹出层对话框插件,该插件包含多种对话框类型,比如提示框.确认框等.更为实用的是,这款jQuery对话框中的按钮事件也可以被我们所捕获,从而相应对话框按钮的各种事件 ...

  7. 考试星陈沧:借助Testin云測加速实现”考试电子化”目标

    考试星陈沧:借助Testin云測加速实现"考试电子化"目标 2014/10/11 · Testin · 开发人员訪谈 考试星国内首款在线考试云平台,可用于企业内部考核,经销商考核, ...

  8. /sys/kernel/debug/gpio

    在使用GPIO的时候,有时候不知道GPIO的状态,也不知道在内核中GPIO是否申请成功. 可以通过/sys/kernel/debug/gpio这个文件来查看.这个文件显示了申请成功的GPIO的输入输出 ...

  9. hdu 1115:Lifting the Stone(计算几何,求多边形重心。 过年好!)

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  10. CDialog与CDialogEx的区别联系

    CDialogEx是VS2003之后出现的,VC++6.0没有.CDialogEx = CDialog ExtendExtend的意思是扩展,即扩展的CDialog! 这个类是CDialog的扩展类, ...