Hdu2836--traversal

Source: Internet
Author: User

Traversal Time limit:2000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 742 Accepted Submission (s): 275


Problem Descriptioni arrive at a big lake and I had to cross it. Luckily, I ' m a very good jumper, but the lake was too big to being 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'll float and it'll 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 t O Box. The only things to consider is:
The lake is big, so I must throw at least 2 boxes, which means this in order to cross the lake I had to make at least 3 J Umps.
Not all the boxes has to be thrown; Some of them may be ignored.
The boxes can thrown into the lake only in the order they is found on the shore and I had the to jump on them in this Ord Er.
The height difference between II consecutive boxes I use must is at most H meters, because I can jump a lot in length, BU T I has 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 the "What's the" the box is.

Facing so many possibilities this respect the above conditions, I begin counting the number of possibilities that I had, Instead of actually crossing the lake. I quickly find the answer and I wonder whether can also find it as fast as I did.

Task

Write A program This determines the number of possibilities to cross the lake in the above conditions. Since the number can be quite big and you are only having to output the remainder of this number when divided by 9901.
Inputthere is multiple test cases. Each test case contains-integers N and H, separated by a space, representing the number of boxes and the maximum Heigh T difference between, consecutive boxes thrown into the lake. The following N lines contain the heights of the boxes, in the order the boxes is set on the shore. The (i+1) th line contains the height of the ith box.
Outputfor Each test case should output a, containing the number of possibilities modulo 9901.

Constraints

1 < N < 100 001
0 < H < 100 000 001
The height of any box was a strictly positive integer and does not exceed 100 000 000
Sample Input
4 21 3) 7 5

Sample Output
4HintExplanationthere is 4 possibilities:1 31 3 53 57 5

Source2009 multi-university Training Contest 3-host by WHU

Line segment Tree DP

  #include <map> #include <set> #include <list> #include <queue> #include < stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib&      Gt      #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;   }


Hdu2836--traversal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.