hdu 2836 Traversal

Traversal

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 722    Accepted Submission(s):
268

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.

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

Sample Input

4 2
1
3
7
5

Sample Output

4

还是太弱,弄一题就得找题解。。。。

 1 #include<iostream>
 2 #include<string>
 3 #include<cstdio>
 4 #include<vector>
 5 #include<queue>
 6 #include<stack>
 7 #include<algorithm>
 8 #include<cstring>
 9 #include<stdlib.h>
10 #include<string>
11 #include<cmath>
12 #include<map>
13 using namespace std;
14 #define mod 9901
15 #define pb push_back
16 #define LL __int64
17 #define mmax 100000+10
18 LL p[mmax],love[mmax],fuck[mmax],cnt,n,m;
19 int search1(LL x){     //找到>=x的第一个数
20     int l=1,r=cnt,mid,tmp=cnt+1;
21     while(l<=r){
22         mid=(l+r)>>1;
23         if(fuck[mid]>=x) r=mid-1,tmp=min(tmp,mid);
24         else l=mid+1;
25     }
26     return tmp;
27 }
28 int search2(LL x){     //找到<=x的最后一个数
29     int l=1,r=cnt,mid,tmp=0;
30   //  cout<<"x="<<x<<endl;
31     while(l<=r){
32         mid=(l+r)>>1;
33         if(fuck[mid]<=x){
34               //  cout<<"tmp="<<tmp<<endl;
35                 l=mid+1,tmp=max(tmp,mid);
36               //  cout<<"tmp="<<tmp<<endl;
37         }
38         else r=mid-1;
39     }
40     return tmp;
41 }
42 void update(int pos,int x){
43     while(pos<=cnt){
44         p[pos]+=x;
45         p[pos]%=mod;
46         pos+=pos&(-pos);
47     }
48 }
49 int getnum(int pos){
50     LL sum=0;
51     while(pos>=1){
52         sum+=p[pos];
53         pos-=pos&(-pos);
54     }
55     return sum%mod;
56 }
57 int main(){
58  #ifndef ONLINE_JUDGE
59         freopen("input.txt","r" ,stdin);
60     #endif // ONLINE_JUDGE
61     while(cin>>n>>m){
62         cnt=0;
63         for(int i=1;i<=n;i++) scanf("%I64d",&love[i]);
64         fuck[++cnt]=love[1];
65         for(int i=2;i<=n;i++) if(love[i]!=love[i-1]) fuck[++cnt]=love[i];
66         sort(fuck+1,fuck+1+cnt);
67         LL sum=0;memset(p,0,sizeof(p));
68         for(int i=1;i<=n;i++){
69             int a=search1(love[i]-m),b=search2(love[i]+m),c=search1(love[i]);
70             int tmp=getnum(b)-getnum(a-1);
71             //tmp%=mod; 不着为啥下面的改成这样就错了
72             tmp=(tmp%mod+mod)%mod;
73             sum+=tmp;
74             sum%=mod;
75             update(c,tmp+1);
76         }
77         cout<<sum<<endl;
78     }
79 }

hdu 2836 Traversal,布布扣,bubuko.com

时间: 2024-10-14 02:21:04

hdu 2836 Traversal的相关文章

HDU 2836 (离散化DP+区间优化)

Reference:http://www.cnblogs.com/wuyiqi/archive/2012/03/28/2420916.html 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2836 题目大意:计算序列有多少种组合,每个组合至少两个数,使得组合中相邻两个数之差不超过H,序列有重复的数.MOD 9901. 解题思路: 朴素O(n^2) 以样例1 3 7 5为例,如果没有重复的数. 设dp[i]前n个数的方案数,初始化为1. 那么fo

hdu 1710 Binary Tree Traversals 前序遍历和中序推后序

题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4205    Accepted Submission(s): 1904 Problem Description A binary tree i

HDU 3152 Obstacle Course(BFS+优先队列 重载)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3152 Problem Description You are working on the team assisting with programming for the Mars rover. To conserve energy, the rover needs to find optimal paths across the rugged terrain to get from its sta

HDU 1710 二叉树的遍历 Binary Tree Traversals

Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4330    Accepted Submission(s): 1970 Problem Description A binary tree is a finite set of vertices that is either empty or

hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)

Binary Tree Traversals Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called

HDU 1710-Binary Tree Traversals(重建二叉树)

Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3340    Accepted Submission(s): 1500 Problem Description A binary tree is a finite set of vertices that is either empty or

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往