HDU 5289 Assignment (2015 Multi-school training)

Source: Internet
Author: User
Tags cmath

Topic Link: Poke Me

Main topic:

The company has n employees, numbering 1....N, each employee has the ability to a[i], these employees are divided into several groups, each group of employees must be linked to the number, and to meet the staff of each group of any two person ability of the difference is less than k, can be a group of people

Ask how many groups can be divided into.

Sample Explanation:

2 //two test sample

4 2 //For n, K, respectively

3 1 2 4 //On behalf of each employee's ability, it can be grouped as [Max], [2,2], [3,3], [+], [2,3], where the array subscript, others do not conform to test instructions, because the difference between the value >= 2

5 //Ibid .

0 3 4 5 2 1 6 7 8 9 //Ibid .

Output:

5

28

Problem Solving Ideas:

A simple question. Because the data is too large, the output answer variable is a long long type, and the middle variable is not used long long, resulting in the intermediate variable calculation overflow, resulting in an error, WA many times.

The official puzzle is that:

Solution One: Enumerate left endpoints, two points right short point, use St algorithm to find interval maximum

Solution Two: Maintain the maximum value with a monotone queue.

I solution:

Maintain a range of the maximum and minimum values, while recording the maximum and minimum values of the subscript x, Y, when the difference between the maximum and minimum value of the interval does not satisfy the K value to jump out, get the number of this interval len, there is (len-1) *LEN/2 kind of continuous combination, but also minus the number of the last repetition. At this point the continuation loop does not start with i+1, but starts at min (x, y) +1. The reason why this should be done is obvious.

Code:

I code, 358ms

Author ljh//www.cnblogs.com/tenlee#include <cstdio> #include <cstdlib> #include <cstring># Include <cctype> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <map> #define CLC (A, B) memset (A, B, sizeof (a)) using namespace Std;const int inf = 0x3 f;const int INF = 0x3f3f3f3f;const int maxn = 1e6+10;long long a[maxn], N, k;int main () {freopen ("1002.in", "R", stdin)    ;    int T, x;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n, &k);        for (int i = 0; i < n; i++) {scanf ("%lld", &a[i]);        } A long long ans = n;        Long Long ma = a[0], MI = a[0], x, y;        Long Long len = 1, i = 0, J;            while (I < n-1) {x = y = i;//reinitialize mi = a[i];//reinitialize ma = a[i];//re-initialization     Len = 1;//reinitialization for (j = i+1; J < N; j + +) {if (a[j] > Ma)           {ma = a[j]; y = j;                } if (A[j] < mi) {mi = a[j]; x = j;                } if (Ma-mi < k) {len++;                } else {break;            }} ans + = (len-1) * LEN/2;            if (j >= N) break; i = min (x, y) + 1;//The next loop starts from here if (I < J) {ans-= (j-i) * (j-i-1)/2;//Because there may be repeated sequences    column, so subtract}} printf ("%lld\n", ans); } return 0;}

Standard Code 1201ms

#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #define LL __int64# Define Min (a) < (b)? ( A):(B)) #define MAX (a) > (b) ( A):(B)) const int N = 200007;int min[n][20];int max[n][20];int querymin (int l,int r) {int k=log2 ((double) (r-l+1)); return Mi N (min[l][k],min[r-(1<<k) +1][k]);} int Querymax (int l,int r) {int k=log2 ((double) (r-l+1)); return Max (max[l][k],max[r-(1<<k) +1][k]);} int calc (int l,int r) {int k=log2 (double) (r-l+1)); int Max=max (max[l][k],max[r-(1<<k) +1][k]); int min=min (Min[l] [K],min[r-(1<<k) +1][k]); return max-min;} Char filein[]={"9.in"};char fileot[]={"9.out"};int Main () {//Freopen ("Data/data1002/1002.in", "R", stdin);//for (int t =0;t<10;++t) {//filein[0]=t+ ' 0 ';//fileot[0]=t+ ' 0 ';//freopen (Filein, "R", stdin);//freopen (Fileot, "w", stdout); int T;int n,k,i,j,p; LL ans;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&k), for (I=1;i<=n;++i) {scanf ("%d", &j); Min[i] [0]=max[i][0]=j;} for (j=1; (1<<j) <=n;++j) for (i=1;i+ (1<<j) -1<=n;++i) {p= (1<< (j-1)); Min[i][j]=min (Min[i][j-1],min[i+p][j-1]); Max[i][j]=max (Max[i][j-1],max[i+p][j-1]);} int L,r;ans=0;for (i=1;i<=n;++i) {l=i,r=n;while (l+1<r) {p= (l+r) >>1;if (Calc (i,p) <k) {l=p;} Else{r=p;}} if (Calc (i,r) <k) {p=r;ans=ans+ (LL) (r-i+1);} Else{p=l;ans=ans+ (LL) (l-i+1);}} printf ("%i64d\n", ans);} Fclose (stdin);//fclose (stdout);//}return 0;}

HDU 5289 Assignment (2015 Multi-school training)

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.