Levko and Array
题意:
有一长度为n的正整数序列,你可以选择K个数字任意改变它,使得$max \{ a(i+1) - a(i) \} $ 最小,求最小值。
解法:
1.$O(n^2log(MAX_A) )$,考虑二分出$d = max \{ a(i+1) - a(i) \} $,这样考虑dp
$f(i,j)$表示前i个数字,末位的数字为j的时候最少修改了多少数字
$f(i,j) = min \{ f(i-1,k) \} (j-d ≤ k ≤ j+d,j = a(i))$
$f(i,j) = min \{ f(i-1,k) \} (j-d ≤ k ≤ j+d,j ≠ a(i))$
注意到有效的j只有$a(i),a(i)-d,a(i)+d$,从而对第二维离散化,同时用单调队列维护区段min即可
(维护方法,对于 前面出现的 比后面的数字小 的数一定不会出现在答案中,这样只要维护一个单调增的双端队列即可)
此方法常数过大,TLE
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <ctime> #define LL long long
#define N 2010
#define INF 0x3f3f3f3f using namespace std; int n,m,K,minh,maxh,tots;
int a[N],a0[N*];
int f[N][N*],q[N*];
double tott; int Abs(int x)
{
if(x<) return -x;
return x;
} bool check(int d)
{
a0[]=;
for(int i=;i<=n;i++)
{
a0[++a0[]]=a[i];
if(a[i]-(LL)d>=(LL)minh) a0[++a0[]]=a[i]-d;
if(a[i]+(LL)d<=(LL)maxh) a0[++a0[]]=a[i]+d;
}
sort(a0+,a0+a0[]+);
m=;
for(int i=;i<=a0[];i++) if(a0[i]!=a0[i-]) a0[++m]=a0[i];
for(int i=;i<=m;i++) f[][i]=;
int t1=lower_bound(a0+,a0+m+,a[])-a0;
f[][t1]=;
int st=,ed=;
for(int i=;i<=n;i++)
{
st=, ed=;
int tmp=,minv=K+;
for(int j=;j<=m;j++)
{
while(tmp<=m && a0[tmp]<=a0[j]+d)
{
while(st<=ed && f[i-][q[ed]]>=f[i-][tmp]) ed--;
q[++ed]=tmp++;
}
while(st<ed && a0[q[st]]<a0[j]-d) st++;
int k=q[st];
if(a0[j]==a[i]) f[i][j]=f[i-][k];
else f[i][j]=f[i-][k]+;
minv = min(minv, f[i][j]);
if(f[i][j] + n-i <= K) return ;
}
if(minv > K) return ;
}
return ;
} int main()
{
freopen("test.txt","r",stdin);
while(~scanf("%d%d",&n,&K))
{
unsigned int l=,r=;
a0[]=;
minh=INF;
maxh=-INF;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
minh=min(minh,a[i]);
maxh=max(maxh,a[i]);
if(i>) r = max(r,(unsigned int)Abs(a[i]-a[i-]));
}
while(r-l>)
{
unsigned int mid=(l+r)>>;
if(check(mid)) r=mid;
else l=mid;
}
for(unsigned i=l;i<=r;i++)
if(check(i))
{
// cout << clock()/1000.0 << endl;
cout << i << endl;
break;
}
}
return ;
}
2.注意到方法一中第二维实际只有 j=a(i) 和其他的j ,两种j。
从而设$f(i)$表示前i个数字,数字i不改变 最少修改了多少数字。
这样有
$f(i) = min \{ f(j) + j-i-1 \}, ( \frac{|a(i)-a(j)|}{i-j} ≤ d )$
(i到j之间的数字全都改变)
小常数飘过
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <ctime> #define LL long long
#define N 2010
#define INF 0x3f3f3f3f using namespace std; int n,m,K,a[N];
int f[N]; LL Abs(LL x)
{
if(x<) return -x;
return x;
} bool check(int d)
{
f[]=;
if(n-<=K) return ;
for(int i=;i<=n;i++)
{
f[i]=i-;
for(int j=;j<i;j++)
if(Abs(a[i]-(LL)a[j]) <= d*(LL)(i-j))
f[i] = min(f[i], f[j]+i-j-);
if(f[i]+n-i<=K) return ;
}
return ;
} int main()
{
// freopen("test.txt","r",stdin);
while(~scanf("%d%d",&n,&K))
{
LL l=,r=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(i>) r = max(r,(LL)Abs(a[i]-a[i-]));
}
while(r-l>)
{
LL mid=(l+r)>>;
if(check(mid)) r=mid;
else l=mid;
}
for(unsigned i=l;i<=r;i++)
if(check(i))
{
cout << i << endl;
break;
}
}
return ;
}
Levko and Array的更多相关文章
- [codeforces 360]A. Levko and Array Recovery
[codeforces 360]A. Levko and Array Recovery 试题描述 Levko loves array a1, a2, ... , an, consisting of i ...
- CF360B Levko and Array (二分查找+DP)
链接:CF360B 题目: B. Levko and Array time limit per test 2 seconds memory limit per test 256 megabytes i ...
- Codeforces 361D Levko and Array(二分)(DP)
Levko and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- 有意思的DP(CF360B Levko and Array)
刚才面试了一个蛮有意思的DP题目,脑子断片,没写出来,不过早上状态还是蛮好的 一个长度为n的序列最多改变k次,使相邻两数之差绝对值的最大值最小 三维的dp我先尝试写一下 Codeforces 360B ...
- CodeForces - 361D Levko and Array
Discription Levko has an array that consists of integers: a1, a2, ... , an. But he doesn’t like this ...
- Codeforces Round #210 (Div. 2) C. Levko and Array Recovery
题目链接 线段树的逆过程,想了老一会,然后发现应该是包含区间对存在有影响,就不知怎么做了...然后尚大神,说,So easy,你要倒着来,然后再正着来,判断是不是合法就行了.然后我乱写了写,就过了.数 ...
- cf D. Levko and Array
http://codeforces.com/contest/361/problem/D 用二分搜索相邻两个数的差的绝对值,然后用dp记录数改变的次数.dp[i]表示在i之前改变的次数,如果|a[i]- ...
- cf C. Levko and Array Recovery
http://codeforces.com/contest/361/problem/C 这道题倒着一次,然后正着一次,在正着的一次的时候判断合不合法就可以. #include <cstdio&g ...
- codeforces 361 D. Levko and Array(dp+二分)
题目链接:http://codeforces.com/contest/361/problem/D 题意:最多可以修改K次数字,每次修改一个数字变成任意值,C=max(a[i+1]-a[i]):求操作之 ...
随机推荐
- Spring Cloud(十一):Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式
上篇文章主要介绍了Zuul网关使用模式,以及自动转发机制,但其实Zuul还有更多的应用场景,比如:鉴权.流量转发.请求统计等等,这些功能都可以使用Zuul来实现. Zuul的核心 Filter是Zuu ...
- (总结)Nginx配置文件nginx.conf中文具体解释
#定义Nginx执行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错误日志定义类型,[ debug | ...
- Selenium学习(一)环境搭建
一.安装selenium有两种方式: 1.pip install -U selenium(网络条件好) 2.下载selenium安装包,通过python setup.py install(压缩包)或p ...
- maven的坑2
导入工程后,pom.xml文件中以下插件报错: <plugin> <groupId>com.jayway.maven.plugins.android.generation2&l ...
- React Native 学习(三)之 FlexBox 布局
React Native 学习(三)之 FlexBox 布局
- 02-合并frame
进入IJKMediaPlayer工程--->
- MySQL 数据类型转换
版权个人所有,欢迎转载如转载请说明出处.(东北大亨) http://www.cnblogs.com/northeastTycoon/p/5505523.html 网络越来越达到所以带来的好处不容置疑. ...
- php 批量删除数据
php 批量删除数据 :比如我们在看邮箱文件的时候,积攒了一段时间以后,看到有些文件没有用了 这时候我们就会想到把这些 没用的文件删除,这时候就用到了批量删除数据的功能,这里我是用了数据库原有的一个表 ...
- 基于struts2的学生报道管理系统(附github源码地址)
本项目参考了<java web轻量级开发全体验>,加入了对mysql的支持. 一.基本业务功能 通过struts2框架,结合mysql数据库构建一个学生报到管理系统,来模拟学生报到登记的过 ...
- Vue 单页面应用 SEO SPA single page application advantages and disadvantages
处理 Vue 单页面应用 SEO 的另一种思路 - muwoo - 博客园 https://www.cnblogs.com/tiedaweishao/p/7493971.html SPA网站SEO完美 ...