I. Photo Processing 二分差值
来源:互联网 发布:高斯算法如何确定项数 编辑:程序博客网 时间:2024/06/01 22:03
I. Photo Processing
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
题意:n个数分组,每组至少有k个数。令t为每组的最大数和最小数的差,y为这些组的最大的t。 求 最小的y是多少?
题解: 二分 y
#include <bits/stdc++.h>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 500002;const int Mod = 1e9 + 7;#define ll long long#define mem(x,y) memset(x,y,sizeof(x))#define IO ios_base::sync_with_stdio(0);int n, k, v[maxn];bool can[maxn];bool check(int x) { int s = 1; mem(can, 0); can[0] = 1; for (int i = 1; i <= n; i++) { while (v[i] - v[s] > x)s++; while (i - s + 1 >= k) { if (can[s - 1]) { can[i] = 1; break; } s++; } } return can[n];}int main() { cin >> n >> k; for (int i = 1; i <= n; i++)cin >> v[i]; sort(v + 1, v + 1 + n); int L = 0, R = INF, ans; while (L <= R) { int mid = (L + R) >> 1; if (check(mid)) ans = mid, R = mid - 1; else L = mid + 1; } cout << ans << endl; return 0;}
- I. Photo Processing 二分差值
- codeforces 883 I. Photo Processing(dp+二分)
- [二分][dp] cf883I Photo Processing
- Processing I/O Operations
- photo
- photo
- photo
- Photo
- photo
- photo
- photo
- photo
- 二分查找的改进--差值查找
- 二分查找与拉格朗日差值查询
- How Can i Store photo (image) in column of table
- 差值
- 差值
- Periodic Processing in Axapta, Part I
- 通过修改表结构添加约束条件
- Hibernate 基于注解的方式 实现的关系映射案例(一对一关系,一对多关系,多对多关系)
- java 多态-抽象类-接口
- 简单聊一聊
- 基于深度学习的目标检测
- I. Photo Processing 二分差值
- SQL join
- redis缓存搭建
- bzoj 4237: 稻草人 cdq分治+单调栈+二分
- 大话Ceph -- RBD那点事儿
- 论文阅读:《Associative Embedding:End-to-End Learning for Joint Detection and Grouping》
- Android View的绘制流程
- SQL Server的数据定义和更新
- UVa11361