我是传送门
There are n students numerated from 1 to n. The level of the i-th student is ai. You need to split the students into stable groups. A group of students is called stable, if in the sorted array of their levels no two neighboring elements differ by more than x.
For example, if x=4, then the group with levels [1,10,8,4,4] is stable (because 4−1≤x, 4−4≤x, 8−4≤x, 10−8≤x), while the group with levels [2,10,10,7] is not stable (7−2=5>x).
Apart from the n given students, teachers can invite at most k additional students with arbitrary levels (at teachers’ choice). Find the minimum number of stable groups teachers can form from all students (including the newly invited).
For example, if there are two students with levels 1 and 5; x=2; and k≥1, then you can invite a new student with level 3 and put all the students in one stable group.
Input
The first line contains three integers n, k, x (1≤n≤200000, 0≤k≤1018, 1≤x≤1018) — the initial number of students, the number of students you can additionally invite, and the maximum allowed level difference.
The second line contains n integers a1,a2,…,an (1≤ai≤1018) — the students levels.
Output
In the only line print a single integer: the minimum number of stable groups you can split the students into.
Examples
Input
8 2 3
1 1 5 8 12 13 20 22
Output
2
Input
13 0 37
20 20 80 70 70 70 420 5 1 5 1 60 90
Output
3
Note
In the first example you can invite two students with levels 2 and 11. Then you can split the students into two stable groups:
[1,1,2,5,8,11,12,13],
[20,22].
In the second example you are not allowed to invite new students, so you need 3 groups:
[1,1,5,5,20,20]
[60,70,70,70,80,90]
[420]
题意:给出n,k,x三个数字,输入n个数字,求最少可以构成多个组合(如果排序后相邻的两个数差小于等于x即为一个组合)。
首先应该知道排序后两相邻数子的差值如果大于x(等于不行)就应该添加数字了。添加多少个应该举例分析,尤其是边界,不能想当然,见代码最下方。
AC代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a[200010],b[200010];
int main()
{
ll n,k,x,i,f,ans,g,xx;
scanf("%lld%lld%lld",&n,&k,&x);
for(i=1;i<=n;i++)
scanf("%lld",&a[i]);
sort(a+1,a+1+n);
if(n==1||x==0)//特判:就一个数时或者允许最小间距为0时
{
printf("1\n");
return 0;
}
g=1;
f=1;
while(f<n)
{
if(a[f+1]-a[f]>x)//见下方
{
xx=a[f+1]-a[f]-1;
b[g++]=xx;
}
f++;
}
ans=g;//初始的组合数 ,b数组有g-1个
sort(b+1,b+g);
for(i=1;i<=g-1;i++)
{
if(k>=b[i]/x)
{
k-=b[i]/x;
ans--;
}
}
printf("%lld\n",ans);
}
/*3 3 3
1 4 10
1到4不用添加新的数字,4到10-->4 7 10 但是如果 xx=a[f+1]-a[f];k-=b[i]/x; ans--;
这样写就添加了2个数字,就错了。所以 xx=a[f+1]-a[f]-1; ; 因为b数组中都是大于x的数,所以xx不可能为0!
*/