HDU5289 Assignment of Problem solving report

Source: Internet
Author: User

HDU5289 Assignment of Problem solving report

Description

Tom owns a company and he's the boss. There is n staffs which is numbered from 1 to N in the this company, and every the staff have a ability. Now, Tom was going to assign a special task to some staffs who were in the same group. In a group, the difference of the ability for any of the. than K, and their numbers are continuous. Tom want to know the number of groups like this.

Input

In the first line a number T indicates the number of test cases. Then for each case the first line contain 2 numbers n, K (1<=n<=100000, 0<k<=10^9), indicate the company have n Persons, k means the maximum difference between abilities of the staff in a group are less than K. The second line contains n integers:a[1],a[2],..., A[n] (0<=a[i]<=10^9), indicate the i-th staff ' s ability.

Output

For each test,output the number of groups.

Sample Input

Sample Output

Hint

First Sample, the satisfied groups include:[1,1], [2,2], [3,3], [bis], [2,3]          

Title: give you a sequence, 1~n. Q You have several consecutive sub-sequences that satisfy the difference between the maximum and minimum values less than K.


Analysis: Again a monotonous queue of questions, we set up two pointers I, J, from the beginning to the right to move to the O (n) complexity of the scan to solve the problem. What is the purpose of the monotone queue, which is to dynamically update the maximum minimum value in the current sequence at a very small time. (If you do not understand this, learn the monotone queue yourself, here you only need to know that it dynamically update the maximum minimum value). [J,i-1] represents the current sequence of concerns , we will keep the I pointer forward, to update the maximum minimum value until [j,i] the difference >=k, indicating that at this time [j,i-1] is the limit. Then the sequence with J as the starting point can only be selected from [J,i-1], and there is a i-j seed selection method. Then J moves forward one bit, repeating the above process until I update the entire sequence.
on the code:
#include <iostream> #include <cstdio> #include <algorithm> #include <deque>using namespace std; const int MAXN = 1e5 + 10;int NUMBERS[MAXN];d eque<int> high;deque<int> low;int main () {int kase;cin >> ka Se;int N, K;while (kase--) {cin >> n >> k;for (int i = 1; I <= n; i++) {scanf ("%d", &numbers[i]);} High.clear (); low.clear (); int I, J; I was pre pointerlong long ans = 0;for (i = j = 1; I <= n; i++) {while (!high.empty () && high.back () < numbers [i]) high.pop_back (); High.push_back (Numbers[i]); while (!low.empty () && low.back () > Numbers[i]) low.pop_ Back (); Low.push_back (Numbers[i]), while (!high.empty () &&!low.empty () && High.front ()-Low.front () >= k) {ans + = i-j;if (High.front () = Numbers[j]) high.pop_front (); if (low.front () = = Numbers[j]) low.pop_front ();//If J The minimum value has been moved out of the position, then the J + + is popped;}} while (j <= N)//Do not forget {ans + = i-j;j++;} cout << ans << endl;} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU5289 Assignment of Problem solving report

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.