第一次寫博客..今天總算把前天的題補完了,爲了補題還專門學了樹狀數組,廢了半個下午的時間啊,綜合了3個博客才差不多學會了,很慚愧只做了兩道題...明天在加油吧,把上週的題補完!有時間在做做最短路的題吧!;
只有兩道題:
一:
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 n, k and d (1 ≤ k ≤ n ≤ 5·105, 0 ≤ 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.
題意:有n個鉛筆,每隻鉛筆有個value,現在把他們分發到幾個筆筒中,每個筆筒最少放k個筆,且放入的每支筆的value差不能大於d。求解能不能找到一種方案滿足;
////dp 樹狀數組優化
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<memory.h>
using namespace std;
typedef long long ll;
ll n,k,d;
ll a[500005];
ll c[500005];
bool dp[500005];
ll lowbit(ll x)
{
return x&(-x);
}
void add(ll x)
{
while(x<=n)
{
c[x]+=(ll)1;
x+=lowbit(x);
}
}
ll sum(ll x)
{
ll res=0;
while(x)
{
res+=c[x];
x=x-lowbit(x);
}
return res;
}
int main()
{
memset(c,0,sizeof(c));
cin>>n>>k>>d;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
{
if(i<k)dp[i]=0;
else
{
if(a[i]-a[1]<=d)dp[i]=1;
else
{
int t=lower_bound(a+1,a+1+n,a[i]-d)-a-1;
if(i-t<k)dp[i]=0;
else dp[i]=((sum(i-k)-sum(t-1))>0 );
}
if(dp[i])add(i);
}
}
if(dp[n])printf("YES");
else printf("NO");
return 0;
}