hdu2836——Traversal

Traversal

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

Total Submission(s): 742    Accepted Submission(s): 275

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

Hint

Explanation

There are 4 possibilities:
1 3
1 3 5
3 5
7 5


 

Source

2009 Multi-University Training Contest 3 - Host by WHU

线段树dp

  #include <map>
    #include <set>
    #include <list>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>  

    using namespace std;
    const int N = 100010;
    __int64 dp[N];
    int xis[N];
    int arr[N];
    int cnt;  

    struct node
    {
        int l, r;
        __int64 sum;
    }tree[N << 2];  

    int BinSearch(int val)
    {
        int l = 1, r = cnt, mid, ans;
        while (l <= r)
        {
            mid = (l + r) >> 1;
            if (xis[mid] == val)
            {
                ans = mid;
                break;
            }
            else if (xis[mid] > val)
            {
                r = mid - 1;
            }
            else
            {
                l = mid + 1;
            }
        }
        return ans;
    }  

    int BinSearch_left(int val)
    {
        int l = 1;
        int r = cnt, mid, ans;
        while (l <= r)
        {
            mid = (l + r) >> 1;
            if (xis[mid] >= val)
            {
                ans = mid;
                r = mid - 1;
            }
            else
            {
                l = mid + 1;
            }
        }
        return ans;
    }  

    int BinSearch_right(int val)
    {
        int l = 1;
        int r = cnt, mid, ans;
        while (l <= r)
        {
            mid = (l + r) >> 1;
            if (xis[mid] <= val)
            {
                ans = mid;
                l = mid + 1;
            }
            else
            {
                r = mid - 1;
            }
        }
        return ans;
    }  

    void build(int p, int l, int r)
    {
        tree[p].l = l;
        tree[p].r = r;
        tree[p].sum = 0;
        if (l == r)
        {
            return;
        }
        int mid = (l + r) >> 1;
        build(p << 1, l, mid);
        build(p << 1 | 1, mid + 1, r);
    }  

    void update(int p, int pos, __int64 val)
    {
        if (tree[p].l == tree[p].r)
        {
            tree[p].sum += val;
            return;
        }
        int mid = (tree[p].l + tree[p].r) >> 1;
        if (pos <= mid)
        {
            update(p << 1, pos, val);
        }
        else
        {
            update(p << 1 | 1, pos, val);
        }
        tree[p].sum = tree[p << 1].sum + tree[p << 1 | 1].sum;
        tree[p].sum %= 9901;
    }  

    __int64 query(int p, int l, int r)
    {
        if (tree[p].l >= l && tree[p].r <= r)
        {
            return tree[p].sum;
        }
        int mid = (tree[p].l + tree[p].r) >> 1;
        if (r <= mid)
        {
            return query(p << 1, l, r);
        }
        else if (l > mid)
        {
            return query(p << 1 | 1, l, r);
        }
        else
        {
            return query(p << 1, l, mid) + query(p << 1 | 1, mid + 1, r);
        }
    }  

    int main()
    {
        int n, d;
        while(~scanf("%d%d", &n, &d))
        {
            memset (dp, 0, sizeof(dp));
            __int64 ans = 0;
            for (int i = 1; i <= n; ++i)
            {
                scanf("%d", &arr[i]);
                xis[i] = arr[i];
            }
            sort(xis + 1, xis + n + 1);
            cnt = unique(xis + 1, xis + n + 1) - xis - 1;
            build(1, 1, cnt);
            dp[1] = 0;
            update(1, BinSearch(arr[1]), dp[1] + 1);
            int l, r;
            for (int i = 2; i <= n; i++)
            {
                l = BinSearch_left(arr[i] - d);
                r = BinSearch_right(arr[i] + d);
                dp[i] = query(1, l, r);
                dp[i] %= 9901;
                int x = BinSearch(arr[i]);
                update(1, x, dp[i] + 1);
                ans += dp[i];
                ans %= 9901;
            }
            printf("%I64d\n", ans);
        }
        return 0;
    }  
时间: 2024-08-01 05:08:59

hdu2836——Traversal的相关文章

HDU2836 Traversal

题解: dp+树状数组优化 跟hdu2227差不多,只不过改变了一下范围.更改一下sum的方式即可 但是这题的a[i]范围比较大. 在用树状数组时,一种是直接使用map,另一种是使用hash+二分 代码: map优化 1060ms #include<bits/stdc++.h> using namespace std; #define LL long long const int N=100010; const int M=100000100; const int mod=9901; int

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

LeetCode Binary Tree Inorder Traversal

LeetCode解题之Binary Tree Inorder Traversal 原题 不用递归来实现树的中序遍历. 注意点: 无 例子: 输入: {1,#,2,3} 1 2 / 3 输出: [1,3,2] 解题思路 通过栈来实现,从根节点开始,不断寻找左节点,并把这些节点依次压入栈内,只有在该节点没有左节点或者它的左子树都已经遍历完成后,它才会从栈内弹出,这时候访问该节点,并它的右节点当做新的根节点一样不断遍历. AC源码 # Definition for a binary tree node

Java [Leetcode 107]Binary Tree Level Order Traversal II

题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order

[LeetCode]Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 re

Leetcode-Binary Tree Preorder Traversal

题目链接:https://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题目: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial,

LeetCode-Binary Tree Postorder Traversal

题目链接:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 分析:求二叉树的中序