HDOJ 题目2836 Traversal(线段树,离散化,DP)
来源:互联网 发布:域名ns记录查询 编辑:程序博客网 时间:2024/06/01 07:26
Traversal
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 812 Accepted Submission(s): 301
Problem Description
I arrive at a big lake and I have to cross it. Luckily, I’m a very good jumper, but the lake is too big to be crossed in one jump. On the shore, I find N boxes of different heights, set in a certain order. If I throw a box into the lake, it will float and it will have the same height as on the shore. This is good, because I intend to throw some boxes into the lake and get from one shore to the other by jumping from box to box. The only things to consider are:
The lake is big, so I must throw at least 2 boxes, which means that in order to cross the lake I have to make at least 3 jumps.
Not all the boxes have to be thrown; some of them may be ignored.
The boxes can be thrown into the lake only in the order they are found on the shore and I have to jump on them in this order.
The height difference between two consecutive boxes I use must be at most H meters, because I can jump a lot in length, but I have some problems with jumping in height.The height of a box doesn’t change when I jump on it.
I’m always able to jump from the shore to a box and from a box to the shore, no matter what the height of the box is.
Facing so many possibilities that respect the above conditions, I begin counting the number of possibilities that I have, instead of actually crossing the lake. I quickly find the answer and I wonder whether you can also find it as fast as I did.
Task
Write a program that determines the number of possibilities to cross the lake in the above conditions. Since the number can be quite big, you only have to output the remainder of this number, when divided by 9901.
The lake is big, so I must throw at least 2 boxes, which means that in order to cross the lake I have to make at least 3 jumps.
Not all the boxes have to be thrown; some of them may be ignored.
The boxes can be thrown into the lake only in the order they are found on the shore and I have to jump on them in this order.
The height difference between two consecutive boxes I use must be at most H meters, because I can jump a lot in length, but I have some problems with jumping in height.The height of a box doesn’t change when I jump on it.
I’m always able to jump from the shore to a box and from a box to the shore, no matter what the height of the box is.
Facing so many possibilities that respect the above conditions, I begin counting the number of possibilities that I have, instead of actually crossing the lake. I quickly find the answer and I wonder whether you can also find it as fast as I did.
Task
Write a program that determines the number of possibilities to cross the lake in the above conditions. Since the number can be quite big, you only have to output the remainder of this number, when divided by 9901.
Input
There are multiple test cases. Each test case contains two integers N and H, separated by a space, representing the number of boxes and the maximum height difference between two consecutive boxes thrown into the lake. The following N lines contain the heights of the boxes, in the order the boxes are set on the shore. The (i+1)th line contains the height of the ith box.
Output
For each test case you should output a single line, containing the number of possibilities modulo 9901.
Constraints
1 < N < 100 001
0 < H < 100 000 001
The height of any box is a strictly positive integer and does not exceed 100 000 000
Constraints
1 < N < 100 001
0 < H < 100 000 001
The height of any box is a strictly positive integer and does not exceed 100 000 000
Sample Input
4 21 3 7 5
Sample Output
4HintExplanationThere are 4 possibilities:1 31 3 53 57 5
Source
2009 Multi-University Training Contest 3 - Host by WHU
Recommend
gaojie | We have carefully selected several similar problems for you: 2835 2843 2841 2839 2837
题目大意:给个n,m,就是从一个序列中找一个序列,使相邻的两个数的差不大于m
ac代码
153641052015-11-03 16:13:48Accepted2836249MS4948K2761 BG++XY_
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>//#define mod 9901#define LL long longusing namespace std;LL a[100010],b[100010];LL node[100010<<2],n;LL dp[100010];void pushup(int tr){ node[tr]=(node[tr<<1]+node[tr<<1|1])%9901;}void build(int l,int r,int tr){ node[tr]=0; if(l==r) { return; } int mid=(l+r)>>1; build(l,mid,tr<<1); build(mid+1,r,tr<<1|1); //pushup(tr);}void update(int pos,LL val,int l,int r,int tr){ if(l==r) { node[tr]=(node[tr]+val); return; } int mid=(l+r)>>1; if(pos<=mid) update(pos,val,l,mid,tr<<1); else update(pos,val,mid+1,r,tr<<1|1); pushup(tr);}LL query(int L,int R,int l,int r,int tr){ if(L<=l&&r<=R) { return node[tr]; } int mid=(l+r)>>1; if(R<=mid) return query(L,R,l,mid,tr<<1); else if(L>mid) return query(L,R,mid+1,r,tr<<1|1); else { int a,b; a=query(L,mid,l,mid,tr<<1); b=query(mid+1,R,mid+1,r,tr<<1|1); return (a+b)%9901; }}int bseach(LL val){ int l=1; int r=n; while(l<=r) { int mid=(l+r)>>1; if(b[mid]==val) return mid; if(b[mid]<val) l=mid+1; else r=mid-1; } return l;}int bseach_r(LL val){ int l=1; int r=n; int ans; while(l<=r) { int mid=(l+r)>>1; if(b[mid]<=val) { ans=mid; l=mid+1; } else r=mid-1; } return ans;}int bseach_l(LL val){ int l=1,r=n; int ans; while(l<=r) { int mid=(l+r)>>1; if(b[mid]>=val) { ans=mid; r=mid-1; } else l=mid+1; } return ans;}int main(){ int N,m; while(scanf("%d%d",&N,&m)!=EOF) { int i; memset(dp,0,sizeof(dp)); for(i=1;i<=N;i++) { scanf("%lld",&a[i]); b[i]=a[i]; // node[i]=0; } sort(b+1,b+1+N); n=unique(b+1,b+1+N)-b-1; build(1,n,1); dp[1]=0; update(bseach(a[1]),dp[1]+1,1,n,1); LL ans=0; for(i=2;i<=N;i++) { int l=bseach_l(a[i]-m); int r=bseach_r(a[i]+m); // printf("%d %d\n",l,r); dp[i]=query(l,r,1,n,1); int x=bseach(a[i]); ans=(ans+dp[i])%9901; update(x,dp[i]+1,1,n,1); } printf("%lld\n",ans); }}
0 0
- HDOJ 题目2836 Traversal(线段树,离散化,DP)
- HDU 2836 Traversal(线段树+离散化+DP)
- HDOJ 题目4325 Flowers(线段树+离散化)
- POJ题目 1151|| HDOJ 题目1542Atlantis(线段树+离散化+扫描线)
- HDOJ 5124 lines 【线段树 & 离散化】
- hdoj 5124 lines 【离散化 + 线段树】
- 【HDOJ】 Picture (离散化+线段树)
- HDOJ 题目2227 Find the nondecreasing subsequences(树状数组,离散化,DP)
- HDU 3016 Man Down(线段树+离散化+dp)
- HDU 3607 线段树+离散化+DP
- 【dp+离散化+线段树优化】Paint
- hdoj 4028(离散DP)
- HDOJ 题目4107 Gangster(线段树)
- [线段树+离散化+单点查询] HDOJ - 4325 Flowers
- HDOJ 2492 Ping pong 线段树+离散化
- HDOJ 1542 (POJ 1151) Atlantis 【线段树 离散化 扫描线 面积并】
- poj 2528 离散化+线段树 hdu 1698 线段树 线段树题目类型一:染色计数 外加离散化
- POJ 题目2528 Mayor's posters(线段树+离散化)
- NSData
- DOXYGEN简明实用教程
- charles中如何对https抓包
- Android中shape的使用
- C++ 输入函数 cin>>、cin.getline()和cin.get()区别
- HDOJ 题目2836 Traversal(线段树,离散化,DP)
- CocoaPods—CocoaPods安装和使用
- Masonry介绍与使用实践:快速上手Autolayout
- 分享下我的IO流学习笔记,很详细的
- [MySQL FAQ]系列 — 为什么InnoDB表要建议用自增列做主键
- service详解
- JDBC实现与解析
- Servlet生命周期
- phpexcel 导出数据