CF360B Levko and Array (二分查找+DP)

本文介绍CF360B题目的解题思路及代码实现。通过二分查找结合动态规划的方法,解决在限定次数内修改数组元素,使得数组的特定度量值最小的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:CF360B

题目:

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.

Sample test(s)
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.

           
       

 

大意:在数组a中改变任意k个元素的值,使得到的的值最小。

题解:

二分答案,用dp检查可不可行。

dp[j]表示a[0]~a[j]中,a[j]不变时所要改变的数的数量。

 1 bool check(ll x)
 2 {
 3     int i,j;
 4     for(i=0; i<n; i++)
 5         dp[i]=i;
 6     for(i=0; i<n; i++)
 7         for(j=i+1; j<n; j++)
 8             if(abs(a[i]-a[j])<=(j-i)*x) dp[j]=min(dp[j],dp[i]+j-i-1);
 9     for(i=0; i<n; i++)
10         if (dp[i]+n-i-1 <= k)
11             return true;
12     return false;
13 }

check如上,转移方程时假设a[i]和a[j]之间的数都要改变,那个if是判断是否全部改变了,达成一个超碉的阶梯状,a[i]和a[j]还是差的太远的话就不转移了。

然后后面算的dp[i]+n-i-1是a[i]不变,后面的元素全变的需要变的元素数量。只要有一种方案行,就行。

 

代码:

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 using namespace std;
 8 #define ll __int64
 9 
10 int a[2222];
11 int dp[2222];
12 int n,k;
13 bool check(ll x)
14 {
15     int i,j;
16     for(i=0; i<n; i++)
17         dp[i]=i;
18     for(i=0; i<n; i++)
19         for(j=i+1; j<n; j++)
20             if(abs(a[i]-a[j])<=(j-i)*x) dp[j]=min(dp[j],dp[i]+j-i-1);
21     for(i=0; i<n; i++)
22         if (dp[i]+n-i-1 <= k)
23             return true;
24     return false;
25 }
26 
27 int main()
28 {
29     int i;
30     ll mid,l,r;
31     while(scanf("%d%d",&n,&k)!=EOF)
32     {
33         for(i=0; i<n; i++)
34             scanf("%d",&a[i]);
35         l=0;
36         r=2000000000;
37         while(l<=r)
38         {
39             mid=(l+r)>>1;
40             //cout<<l<<'<'<<mid<<'<'<<r<<endl;
41             if(check(mid)) r=mid-1;
42             else l=mid+1;
43         }
44         printf("%I64d\n",l);
45     }
46     return 0;
47 }
View Code

 

转载于:https://www.cnblogs.com/yuiffy/p/3885353.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值