Topic Portal
Meaning of the questions:
n balls, each ball there are a number of AI, two integer k and d. Now you want to stack divided into a plurality of n balls, there is no limit to the number of stacks, each stack comprising at least k balls, each stack number does not exceed the maximum value minus minimum d. You can ask whether such stock piles.
Data range: 1 <= k <= n <= 5e5, 0 <= d <= 1e9, 1 <= ai <= 1e9.
answer:
When think the current number of pile points more likely to make the results better, it should be aware of dp.
dp [i] = 1 denotes the front i is the number of points can be made to meet the requirements of the stack, dp [i] = 0 indicates the number i is not carried out prior to meet the requirements of the sub-stack.
0 is a valid number, which is the initial state, i.e. dp [0] = 1.
This number n sort, from small to large to traverse.
Two legal limits can be drawn interval [L, R], L satisfies aL> = ai - d smallest subscript, R <= i - k.
If dp [j] = 1 and j∈ [L, R], the dp [i] = 1.
Get dp [J] is the process in the course of seeking the array dp [L. R] interval maximum value.
I did not open dp array, because I keep the state tree line inside.
Experience:
When setting the initial state forget to change a parameter, resulting in 2A.
Not enough stable.
This is a problem when you realize dp would have done, different levels of players aware of the time, I was not one, had to think for a while.
Indeed raise the level of some of the previously encountered this problem, see a solution to a problem you think about it, and now can do independent of (although looked wrong test cases).
Code:
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const int maxn = 5e5 + 5 ;
int n , k , d ;
int a[maxn] ;
bool max1[maxn << 2] ;
int ls(int x)
{
return x << 1 ;
}
int rs(int x)
{
return x << 1 | 1 ;
}
void update(int id , int l , int r , int x , bool y)
{
int mid = (l + r) / 2 ;
if(l == r && l == x)
{
max1[id] = y ;
return ;
}
if(x <= mid)
update(ls(id) , l , mid , x , y) ;
else
update(rs(id) , mid + 1 , r , x , y) ;
max1[id] = max(max1[ls(id)] , max1[rs(id)]) ;
}
bool query(int id , int l , int r , int x , int y)
{
if(x > y) return 0 ;
bool ans = 0 ;
int mid = (l + r) / 2 ;
if(x <= l && r <= y)
return max1[id] ;
if(x <= mid) ans = max(ans , query(ls(id) , l , mid , x , y)) ;
if(y > mid) ans = max(ans , query(rs(id) , mid + 1 , r , x , y)) ;
return ans ;
}
void solve(int i)
{
int l = lower_bound(a + 1 , a + i + 1 , a[i] - d) - a ;
int r = i - k ;
l -- ;
l = max(l , 0) ;
bool x = query(1 , 0 , n , l , r) ;
if(x) update(1 , 0 , n , i , 1) ;
}
int main()
{
scanf("%d%d%d" , &n , &k , &d) ;
for(int i = 1 ; i <= n ; i ++) scanf("%d" , &a[i]) ;
sort(a + 1 , a + n + 1) ;
update(1 , 0 , n , 0 , 1) ;
for(int i = 1 ; i <= n ; i ++)
solve(i) ;
if(query(1 , 0 , n , n , n)) printf("YES\n") ;
else printf("NO\n") ;
return 0 ;
}