Topic Links: E. Pencils and Boxes
Test instructions: N number, required to be divided into any heap, requiring each heap as long as there are k, the difference between any number in the same heap can not exceed D;
Puzzle: Use a tree-like array. Arrange the order and then start looking from the back, with the current number as the minimum value to see if the pile, to a pile of words required i+k, to the first position greater than a[i]+d between the position can be stacked. (In this case, the tree array is less than 0 to see if you can) note the position of the n+1 at the time of initialization plus 1.
1#include <bits/stdc++.h>2#include <iostream>3#include <string.h>4#include <algorithm>5#include <stdio.h>6#include <math.h>7 #definell Long Long8 #definePB Push_back9 using namespacestd;Ten Const intn=800010; One Const intinf=1<< -; A Const intinf=0x3f3f3f3f; - Const intmaxn=5e5+Ten; - Const intmod=1e9+7; the intn,k,d; - intLowbit (intx) - { - returnx& (-x); + } - intA[MAXN],B[MAXN]; + BOOLV[MAXN]; A intAddintx) at { - while(x<=n+1) - { -b[x]++;x+=lowbit (x); - } - } in int Get(intx) - { to intans=0; + while(x>0) - { theans+=b[x];x-=lowbit (x); * } $ returnans;Panax Notoginseng } - intMain () the { +scanf" %d%d%d",&n,&k,&d); A for(intI=1; i<=n;i++) the { +scanf"%d",&a[i]); - } $Sort (A +1, A +1+n); $Add (n+1); - for(inti=n-k+1; i>=1; i--) - { the intPos=upper_bound (A +1, A +1+N,A[I]+D)-A; - if(pos>=i+k-1&&Get(POS)-Get(i+k-1) >0)Wuyi { thev[i]=true; Add (i); - } Wu } - if(v[1]) printf ("yes\n"); About Elseprintf"no\n"); $ return 0; -}
View Code
Educational Codeforces Round (rated for Div. 2) +e. Pencils and boxes+ Tree array