2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Prefer

I. Photo Processing
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Evlampiy has found one more cool application to process photos. However the application has certain limitations.

Each photo i has a contrast vi. In order for the processing to be truly of high quality, the application must receive at least k photos with contrasts which differ as little as possible.

Evlampiy already knows the contrast vi for each of his n photos. Now he wants to split the photos into groups, so that each group contains at least k photos. As a result, each photo must belong to exactly one group.

He considers a processing time of the j-th group to be the difference between the maximum and minimum values of vi in the group. Because of multithreading the processing time of a division into groups is the maximum processing time among all groups.

Split n photos into groups in a such way that the processing time of the division is the minimum possible, i.e. that the the maximum processing time over all groups as least as possible.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 3·105) — number of photos and minimum size of a group.

The second line contains n integers v1, v2, ..., vn (1 ≤ vi ≤ 109), where vi is the contrast of the i-th photo.

Output

Print the minimal processing time of the division into groups.

Examples
input
5 2
50 110 130 40 120
output
20
input
4 1
2 3 4 1
output
0
Note

In the first example the photos should be split into 2 groups: [40, 50] and [110, 120, 130]. The processing time of the first group is 10, and the processing time of the second group is 20. Maximum among 10 and 20 is 20. It is impossible to split the photos into groups in a such way that the processing time of division is less than 20.

In the second example the photos should be split into four groups, each containing one photo. So the minimal possible processing time of a division is 0.


#include <bits/stdc++.h>

using namespace std;
const int N = 3E5 + 7;
int n, k, a[N], dp[N];
bool judge(int key)
{
    int pre = 0;
    for(int i = k;i <= n;i ++) {
        int j = dp[i - k] + 1;
        if(a[i] - a[j] <= key) pre = i;
        dp[i] = pre;
    }
    return dp[n] == n;
}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i = 1;i <= n;i ++) scanf("%d",&a[i]);
    sort(a + 1, a + 1 + n);
    int l = 0, r = a[n] - a[1];
    while(l <= r) {
        int m = (l + r) / 2;
        if(judge(m)) r = m - 1;
        else l = m + 1;
    }
    printf("%d\n", l);
    return 0;
}

二分然后随便DP下,dp[i]表示1-i可满足要求的最接近i的位置,如果最后整段都满足要求一定有dp[n] == n;

转移为如果a[i] - a[dp[i-k]+1] <= key(当前二分的)值则1~i都满足要求,因为dp[i - k]位置之前的都满足要求了,所以两断连起来也满足,则完成转移。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值