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 ...
随机推荐
- ionic2 手风琴效果
user.ts import { Component } from '@angular/core';import { IonicPage, NavController, NavParams } fro ...
- 线程--promise furture 同步
http://www.cnblogs.com/haippy/p/3279565.html std::promise 类介绍 promise 对象可以保存某一类型 T 的值,该值可被 future 对象 ...
- gcc用法小记
By francis_hao Feb 13,2017 概要 这里只列出了最常用的选项 选项解释 -c|-S|-E 启动gcc编译器时,它会顺序执行预处理.编译.汇编和连接(四个阶段的详细介绍 ...
- POJ1984:Navigation Nightmare(带权并查集)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7871 Accepted: 2 ...
- HTTP协议中的ContenType类型大全(转)
".*"="application/octet-stream"".001"="application/x-001"&qu ...
- 【洛谷 P4555】 [国家集训队]最长双回文串 (Manacher)
题目链接 \(|S|<=10^5\),时间还是很宽松的. 允许我们使用线性/\(N\log N\)/甚至\(N \sqrt N\)的算法. 设\(l[i]\)表示以\(a[i]\)结尾的最长回文 ...
- Linux下git源码安装【转】
转自:http://blog.csdn.net/u012889638/article/details/51167123 版权声明:本文为博主原创文章,未经博主允许不得转载. 版本信息:CentOS r ...
- 安全测试===Mysql 注入技巧学习 MySQL注入技巧(2)
原文地址:http://websec.files.wordpress.com/2010/11/sqli2.pdf 0x00.介绍 也可以参考瞌腄龙的mysql注入科普:http://drops.woo ...
- 虚拟机VMware 安装CentOS6.5
对linux完全小白的情况下,也能依据下面的文章,一步一步安装使用成功! CentOS 6.5 下载http://www.linuxdown.net/CentOS/2014/0928/3371.htm ...
- easyUi根据一个日期给另一日期自动赋值的js
$('#loanbegindate').datebox({ onSelect:function(date){ changeDate(); } }); $('#loanterm,#loantermtyp ...