cf I Photo Processing (二分答案+dp)
2018-03-19 17:11
471 查看
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
Copy
Output
Input
Copy
Output
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个数字的数列,让你分组,每组最少m个,使得最大价值最小(每组的价值就是组里面最大数-最小数)
解析:
logn优化:二分答案
最后配合O(n)的dp
这里dp网上有好多版本,最牛是无常数的O(n),然后就是可能带点常数的O(n)
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
Copy
5 2 50 110 130 40 120
Output
20
Input
Copy
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个数字的数列,让你分组,每组最少m个,使得最大价值最小(每组的价值就是组里面最大数-最小数)
解析:
logn优化:二分答案
最后配合O(n)的dp
这里dp网上有好多版本,最牛是无常数的O(n),然后就是可能带点常数的O(n)
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 3e5+10; #define INF 0x3f3f3f3f3f3f3f3f typedef long long int lli; lli aa[MAXN]; int n,m; int dp[MAXN]; /*bool check(lli x) { int last=0; // for(int i=m;i<=n;i++) { int tmp=dp[i-m]; //[1,d[i-m]]是已经切好了,选择i-m的原因是只有这里一定能保证切的区间没有交集 if(aa[i]-aa[tmp+1]<=x) last=i; dp[i]=last; //将d[i]赋值成包括i的那一块的开头 } return dp ==n; }*/ bool check(lli x) { memset(dp,0,sizeof(dp)); //dp[i]表示[1,i]能否被完整分成满足条件的几段 dp[0]=1; int s=1; for(int i=1;i<=n;i++) { while(aa[i]-aa[s]>x) s++; while(i-s+1>=m) { if(dp[s-1]) { dp[i]=1; break; } s++; } } return dp ; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%lld",&aa[i]); } aa[0]=INF; sort(aa+1,aa+1+n); lli l=0; lli r=aa -aa[1]; if(m==1) { printf("0\n"); return 0; } while(l<r) { lli mid=(l+r)>>1; if(check(mid)) r=mid; else l=mid+1; } printf("%lld\n",r); return 0; }
相关文章推荐
- BZOJ 1044: [HAOI2008]木棍分割(二分答案 + dp)
- [dp][二分答案]最大正方形
- 二分答案加DP判断
- DLUTOJ 1158艾尔大停电(二分答案+预处理DP)
- BZOJ 4985: 评分 二分答案 dp判定
- NOIP模拟题 2016.10.5 [Trie] [数学] [二分答案] [杂题] [复杂状态DP]
- 【NOIP2017普及组T4】跳房子-二分答案+DP单调队列优化
- NOIP模拟题 2016.10.18 [二分答案] [从上到下的树形DP] [链表翻转]
- [BZOJ1044]HAOI2008木棍分割|DP|二分答案
- 【BZOJ-4692】Beautiful Spacing 二分答案 + 乱搞(DP?)
- NOIP模拟赛 军训(二分答案+单调队列优化DP)
- sdut 2609 A-Number and B-Number (数位dp,二分答案)
- 【NOIP 模拟题】[T2] 王者荣耀(二分答案+dp)
- [贪心 模拟 + 二分答案 DP] BZOJ 1181 [CROATIAN2009]IZBROI选举
- BZOJ 1044: [HAOI2008]木棍分割 DP,前缀和优化,二分答案
- CodeChef FAVNUM FavouriteNumbers(AC自动机+数位dp+二分答案)
- HDU 3586 Information Disturbing(二分答案+树形DP)
- 1044: [HAOI2008]木棍分割 二分答案+DP+前缀和优化
- hdu3586(树形DP+二分答案)
- so就 (二分答案 dp 贪心)