Hdu 5289 Assignment multi-university Training Contest 1

Source: Internet
Author: User
Assignment Time limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): Accepted submission (s): 43


Problem 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
2 4 2 3 1 2 4 10 5 0 3 4 5 2 1 6 7 8 9
Sample Output
5 Hint First Sample, the satisfied groups include:[1,1], [2,2], [3,3], [+], [2,3]
Source multi-university Training Contest 1



Figure out how many bands there are. Maximum Value-min < K satisfied

Solution:

Enumerating each position x is the leftmost position of the interval. Binary lookup, right-most position r, meet [X,r] maximum-minimum < K

R-x+1 is the solution

Updates the maximum and minimum values in a tree-like array, two-point position.





#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace
Std
#define MAXN 100007 int TREE1[MAXN];
int TREE2[MAXN]; int lowbit (int i) {return i& (-i),} void Addmax (int p,int num) {while (P < MAXN) {tree1[p] = max (tre
        E1[p],num);
    p + = Lowbit (p);
    }} int Querymax (int p) {int ans = 0;
        while (P > 0) {ans = max (ans,tree1[p]);
    P-= Lowbit (p);
} return ans;
        } void addmin (int p,int num) {while (P < MAXN) {tree2[p] = min (tree2[p],num);

    p + = Lowbit (p);
    }} int querymin (int p) {int ans = 1000000007;
        while (P > 0) {ans = min (ans,tree2[p]);
    P-= Lowbit (p);

} return ans;
} int NUM[MAXN];
    int main () {int t,n,k;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &n,&k);
        for (int i = 1;i <= n; i++) {scanf ("%d", &num[i]);
 } memset (Tree1,0,sizeof (tree1));       memset (tree2,0x3f,sizeof (tree2));
        A long long ans = 0;
            for (int i = n; i > 0; i--) {Addmax (i,num[i]);
            Addmin (I,num[i]);
            int low = I,high = N;
                while (low <= high) {int mid = (Low+high)/2;
                int ma = Querymax (mid);
                int mi = querymin (mid);
                if (Ma-mi >= k) high = mid-1;
            else low = mid+1;
            } ans + = low-i;
        cout<<low<<endl;
    } cout<<ans<<endl;
} return 0;













 }




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.