B. Levko and Array----二分答案
2017-08-07 13:06
381 查看
B. Levko and Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Levko has an array that consists of integers: a1, a2, ...
, an. But he doesn’t like this array at all.
Levko thinks that the beauty of the array a directly depends on value c(a),
which can be calculated by the formula:
The less value c(a) is, the more beautiful the array is.
It’s time to change the world and Levko is going to change his array for the better. To be exact, Levko wants to change the values of at most k array
elements (it is allowed to replace the values by any integers). Of course, the changes should make the array as beautiful as possible.
Help Levko and calculate what minimum number c(a) he can reach.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2000).
The second line contains space-separated integers a1, a2, ...
, an ( - 109 ≤ ai ≤ 109).
Output
A single number — the minimum value of c(a) Levko can get.
Examples
input
output
input
output
input
output
Note
In the first sample Levko can change the second and fourth elements and get array: 4, 4, 4, 4, 4.
In the third sample he can get array: 1, 2, 3, 4, 5, 6.
题目链接:http://codeforces.com/contest/360/problem/B
题目的意思是说最多改变数组中k个数,使数组满足c=max(a[i+1]-a[i]) 1<=i<n&&n>1 n<=1时 c=0;也就是说满足两个相邻的数的绝对值差的最大值最小。
这个题一开始看出是二分答案了,但是并没有想出来,去看了网上的博客,但是博客都是二分+dp,我又交了几发,终于让我搞出了二分答案的做法。
f(i)表示前i个的最小修改次数,且i不修改,枚举上一个不修改的位置(感觉我的check里还是融入了dp的思想)。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#define inf 1000000000#define ll long long
using namespace std;
int n,k;
int a[2005],f[2005];
bool check(ll x){
for(int i=1;i<=n;i++){
f[i]=i-1;
for(int j=1;j<i;j++){
if(abs(a[i]-a[j])<=(i-j)*x)
f[i]=min(f[i],f[j]+(i-j-1));
}
if(f[i]+n-i<=k)
return 1;
}
return 0;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
ll l=0,r=2*inf;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)){
r=mid-1;
}
else{
l=mid+1;
}
}
printf("%I64d\n",l);
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Levko has an array that consists of integers: a1, a2, ...
, an. But he doesn’t like this array at all.
Levko thinks that the beauty of the array a directly depends on value c(a),
which can be calculated by the formula:
The less value c(a) is, the more beautiful the array is.
It’s time to change the world and Levko is going to change his array for the better. To be exact, Levko wants to change the values of at most k array
elements (it is allowed to replace the values by any integers). Of course, the changes should make the array as beautiful as possible.
Help Levko and calculate what minimum number c(a) he can reach.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2000).
The second line contains space-separated integers a1, a2, ...
, an ( - 109 ≤ ai ≤ 109).
Output
A single number — the minimum value of c(a) Levko can get.
Examples
input
5 2 4 7 4 7 4
output
0
input
3 1-100 0 100
output
100
input
6 3 1 2 3 7 8 9
output
1
Note
In the first sample Levko can change the second and fourth elements and get array: 4, 4, 4, 4, 4.
In the third sample he can get array: 1, 2, 3, 4, 5, 6.
题目链接:http://codeforces.com/contest/360/problem/B
题目的意思是说最多改变数组中k个数,使数组满足c=max(a[i+1]-a[i]) 1<=i<n&&n>1 n<=1时 c=0;也就是说满足两个相邻的数的绝对值差的最大值最小。
这个题一开始看出是二分答案了,但是并没有想出来,去看了网上的博客,但是博客都是二分+dp,我又交了几发,终于让我搞出了二分答案的做法。
f(i)表示前i个的最小修改次数,且i不修改,枚举上一个不修改的位置(感觉我的check里还是融入了dp的思想)。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#define inf 1000000000#define ll long long
using namespace std;
int n,k;
int a[2005],f[2005];
bool check(ll x){
for(int i=1;i<=n;i++){
f[i]=i-1;
for(int j=1;j<i;j++){
if(abs(a[i]-a[j])<=(i-j)*x)
f[i]=min(f[i],f[j]+(i-j-1));
}
if(f[i]+n-i<=k)
return 1;
}
return 0;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
ll l=0,r=2*inf;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)){
r=mid-1;
}
else{
l=mid+1;
}
}
printf("%I64d\n",l);
return 0;
}
相关文章推荐
- codeforces D. Levko and Array(二分加dp) 挺好的一个题
- CF 360B - Levko and Array (二分+dp)
- CodeForces 360 B.Levko and Array(二分+dp)
- 【Coder Force】#360B - Levko and Array(DP 二分枚举)
- [LeetCode 410] Split Array Largest Sum (二分答案/最大值最小化)
- Codeforces Round #210 (Div. 2) C. Levko and Array Recovery && Codeforces 360 A 详解(思维+维护理论值)
- Codeforces Round #210 (Div. 1)——Levko and Array
- CodeForces - 361C A - Levko and Array Recovery 思维
- 【Educational Codeforces Round 3 D】【二分答案 贪心排序】Gadgets for dollars and pounds m个物品n天价格买k个的最早天数
- hdu 5682 zxa and leaf 二分答案
- Codeforces Round #210 (Div. 1) B. Levko and Array(dp 思维)
- cf C. Levko and Array Recovery
- CodeForces 589F-Gourmet and Banquet-二分答案
- 第四届山东省赛 A-Number and B-Number [数位dp+二分答案]【动态规划】
- 【HDU5565 BestCoder Round 62 (div1)C】【STL or 二分答案 or 计数排序】Clarke and baton n个人减肥m次求最后异或值
- CF-Codeforces Round #210 (Div. 1)-A-Levko and Array Recovery
- A. Levko and Array Recovery----思维题
- C. Anton and Fairy Tale----思维+二分答案
- codeforces 812 C. Sagheer and Nubian Market(二分答案)
- Educational Codeforces Round 3:D. Gadgets for dollars and pounds(二分答案+贪心)