Codeforces 361D Levko and Array(二分)(DP)
Levko and Array
2 seconds
256 megabytes
standard input
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.
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).
A single number — the minimum value of c(a) Levko can get.
5 2
4 7 4 7 4
0
3 1
-100 0 100
100
6 3
1 2 3 7 8 9
1
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.
【分析】题意很简单,就是给你一个数组,定义V为max(abs(a[i+1]-a[i])),给你K次改动机会,就是最多可以改动数组中的K个数,使得V最小。求最小的V。
这题思路好漂亮啊(可能是我很菜没见过吧)。先二分答案,然后看看满足这个答案的情况下需要改动多少数,如果需要改动的数的个数<=K,则保存答案继续二分。
强无敌。。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
typedef long long ll;
using namespace std;
const int N = 3e5+;
const int M = 1e6+;
ll dp[];
ll a[];
ll n,k;
ll Dp_Slove(ll mid) {
memset(dp,0x3f3f3f3f,sizeof(dp));
dp[]=;
for(ll i=; i<=n; i++) {
dp[i]=i-;
for(ll j=i-; j>=; j--) {
if(abs(a[i]-a[j])<=mid*(i-j)) {
dp[i]=min(dp[i],dp[j]+i-j-);
}
}
if(dp[i]+n-i<=k)return ;
}
if(dp[n]<=k)return ;
else return ;
}
int main() {
while(~scanf("%lld%lld",&n,&k)) {
for(ll i=; i<=n; i++) {
scanf("%lld",&a[i]);
}
ll l=,r=;
ll ans=;
while(r>=l) {
ll mid=(l+r)/;
if(Dp_Slove(mid)) {
r=mid-;
ans=mid;
} else l=mid+;
}
printf("%lld\n",ans);
}
}
Codeforces 361D Levko and Array(二分)(DP)的更多相关文章
- CodeForces - 361D Levko and Array
Discription Levko has an array that consists of integers: a1, a2, ... , an. But he doesn’t like this ...
- codeforces 361 D. Levko and Array(dp+二分)
题目链接:http://codeforces.com/contest/361/problem/D 题意:最多可以修改K次数字,每次修改一个数字变成任意值,C=max(a[i+1]-a[i]):求操作之 ...
- codeforces 487B B. Strip(RMQ+二分+dp)
题目链接: B. Strip time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces 749E Gosha is hunting 二分+DP
很神奇的一题 看完题解不由惊叹 题意:$n$个神奇宝贝 $a$个普通球 $b$个高级球 普通球抓住$i$神奇宝贝的概率为$u[i]$ 高级球为$p[i]$ 一起用为$u[i]+p[i]-u[i]*p[ ...
- 有意思的DP(CF360B Levko and Array)
刚才面试了一个蛮有意思的DP题目,脑子断片,没写出来,不过早上状态还是蛮好的 一个长度为n的序列最多改变k次,使相邻两数之差绝对值的最大值最小 三维的dp我先尝试写一下 Codeforces 360B ...
- CF360B Levko and Array (二分查找+DP)
链接:CF360B 题目: B. Levko and Array time limit per test 2 seconds memory limit per test 256 megabytes i ...
- [codeforces 360]A. Levko and Array Recovery
[codeforces 360]A. Levko and Array Recovery 试题描述 Levko loves array a1, a2, ... , an, consisting of i ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- Codeforces 865C Gotta Go Fast 二分 + 期望dp (看题解)
第一次看到这种骚东西, 期望还能二分的啊??? 因为存在重置的操作, 所以我们再dp的过程中有环存在. 为了消除环的影响, 我们二分dp[ 0 ][ 0 ]的值, 与通过dp得出的dp[ 0 ][ 0 ...
随机推荐
- [Leetcode] Remove duplicate from sorted list ii 从已排序的链表中删除重复结点
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- JS Cookie相关操作
function setCookie(cookieName, cookieValue, expires) { // 设置Cookie function getCookieName(cookieName ...
- Spring源码解析-配置文件的加载
spring是一个很有名的java开源框架,作为一名javaer还是有必要了解spring的设计原理和机制,beans.core.context作为spring的三个核心组件.而三个组件中最重要的就是 ...
- 状压DP初识~~炮兵阵地
炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31718 Accepted: 12253 Descriptio ...
- pip 使用国内源
常用国内的pip源如下:阿里云 http://mirrors.aliyun.com/pypi/simple/中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple ...
- GoogleMap在js中的应用
<html> <head> <meta name="viewport" content="initial-scale=1.0, user-s ...
- 转:Mybatis系列之集合映射
转:Mybatis系列之集合映射 上篇文章我们讲了关联映射,实现了销售与登录用户之间的关联.本文我们接着来讲一讲集合映射,实现销售与客户的多对多关系. 实现销售与客户多对多关系 本文中仍延用<M ...
- 查找算法总结Java实现
之前对查找算法做的一些简单总结与实现: 查找算法时间复杂度: 1.二分查找的实现(待补充) public class Test { //循环实现二分查找 public static int binar ...
- bzoj 1009 DP+矩阵加速
我们用DP来解决这个问题 W[I,J]表示准考证的第I位,和不吉利的数匹配到了第J位的方案数,这个状态的表示也可以看成 当前到第I位了,准考证的后J位是不吉利的数的前J位,的方案数 那么我们最后的an ...
- bayer转dng实现过程记录
前言 项目中需要将imx185出来的raw数据转成dng格式,一开始认为很简单的事情,后面才发现还是挺复杂的!!!首先考虑的是不写任何代码,直接用adobe提供的转换工具来转,结果发现,不仅是adob ...