HDU 5289 Assignment

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5289


Surface:

Assignment Time limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 672 Accepted Submission (s): 335


Problem Descriptiontom 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.
Inputin 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.
Outputfor each Test,output the number of groups.
Sample Input
24 23 1 2 410 50 3 4 5 2 1 6 7 8 9


528Hint

Authorfzuacm
Source2015 multi-university Training Contest 1


Solving:

At the time of the game, it is wrong to think, to find the nearest illegal point, the complexity is too high. See the puzzle to know is using ST algorithm. Previously not known, this is a very good introduction of St algorithm. http://blog.csdn.net/david_jett/article/details/46990651

Enumerate the left endpoint, the right end point, the St algorithm to determine whether the interval is legal, until the right end to the limit (that is, the two points of the left and the edge of the intersection).

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <map># Include <vector> #include <cmath> #include <algorithm> #define MoD 1000000007using namespace Std;int t,      N,k;int A[100100],minn[100010][20],maxn[100010][20],mid;long Long ans;void rmq_init () {int m=19;      for (int i=1;i<=n;i++) maxn[i][0]=minn[i][0]=a[i];              for (int i=1;i<=m;i++) for (int j=n;j;j--) {maxn[j][i]=maxn[j][i-1];              MINN[J][I]=MINN[J][I-1];                  if (j+ (1<< (i-1)) <=n) {Maxn[j][i]=max (maxn[j][i],maxn[j+ (1<<)) i-1]);              Minn[j][i]=min (minn[j][i],minn[j+ (1<< (i-1))][i-1]);      }}} int query_dif (int l,int r) {int M=floor (log (double) (r-l+1))/log (2.0));      int Max=max (maxn[l][m],maxn[r-(1<<m) +1][m]);      int Min=min (minn[l][m],minn[r-(1<<m) +1][m]);  return max-min; } int Solve (int l) {int le,ri;    Le=l;        Ri=n;            while (Le<=ri) {mid= (Le+ri)/2;            if (Query_dif (l,mid) >=k) {ri=mid-1;            } else {le=mid+1;        }}/*if (Query_dif (l,mid) >=k) return mid-l; else return mid-l+1;*/return ri-l+1;}    int main () {scanf ("%d", &t);      while (t--) {ans=0;      scanf ("%d%d", &n,&k);      for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);      } rmq_init ();        for (int i=1;i<=n;i++) {ans=ans+solve (i);        cout<<i<< "" <<query_dif (i,n) <<endl;      cout<<ans<<endl;    } printf ("%lld\n", ans); } return 0;}


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

HDU 5289 Assignment

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.