• CodeForces


        可以证明的是,总是存在一种最优策略使得每个组内的权值都是连续的。

        所以排完序一遍 two pointers就好啦。

    Discription

    Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of ninteger numbers — saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:

    • Each pencil belongs to exactly one box;
    • Each non-empty box has at least k pencils in it;
    • If pencils i and j belong to the same box, then |ai - aj| ≤ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils iand j such that |ai - aj| ≤ d and they belong to different boxes.

    Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".

    Input

    The first line contains three integer numbers nk and d (1 ≤ k ≤ n ≤ 5·1050 ≤ d ≤ 109) — the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.

    The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109) — saturation of color of each pencil.

    Output

    Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".

    Examples

    Input
    6 3 10
    7 2 7 7 4 2
    Output
    YES
    Input
    6 2 3
    4 5 3 13 4 10
    Output
    YES
    Input
    3 2 5
    10 16 22
    Output
    NO

    Note

    In the first example it is possible to distribute pencils into 2 boxes with 3pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.

    In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    const int maxn=500005;
    int n,a[maxn],K,D,now,L,R;
    bool can[maxn];
    
    inline int read(){
    	int x=0; char ch=getchar();
    	for(;!isdigit(ch);ch=getchar());
    	for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
    	return x;
    }
    
    inline void solve(){
    	L=R=0,can[0]=now=1;
    	for(int i=1;i<K;i++) can[i]=0;
    	for(int i=K;i<=n;i++){
    		while(a[i]-a[L+1]>D){ now-=(int)can[L],L++;}
    		while(R<i-K){ R++,now+=(int)can[R];}
    //		printf("%d %d %d %d
    ",i,L,R,now);
    		can[i]=now>0?1:0;
    	}
    }
    
    int main(){
    	n=read(),K=read(),D=read();
    	for(int i=1;i<=n;i++) a[i]=read();
    	sort(a+1,a+n+1);
    	
    	solve();
    	
    	puts(can[n]?"YES":"NO");
    	return 0;
    }
    

      

  • 相关阅读:
    jQuery height()、innerHeight()、outerHeight()函数的区别
    [css][移动设备]禁止横竖屏时内容自动调整
    document模板
    CSS3的appearance属性--改变元素的外观
    jQuery API中文文档
    jquery 获取元素的 实际宽度和高度
    为什么使用"use strict"可以节约你的时间
    ueditor的过滤、转义、格式丢失问题
    flask mysql
    WPF进阶之接口(2):IDisposable,ICollectionView
  • 原文地址:https://www.cnblogs.com/JYYHH/p/9071227.html
Copyright © 2020-2023  润新知