思路:DP

提交:1次

题解:

最开始我们可以想到,分两种序列都做一遍。

先来证明一个结论:

存在一种构造,使 \(B\) 中的数都在 \(A\) 中出现过,且这样不劣。

(目的是为了转化暂时看起来虚无缥缈的DP)

显然一个数成立,考虑 \(B\) 的前 \(k-1\) 项,向后插入一个数 \(B_k\)。

若 \(B_{k-1}\leq A_k\) ,我们直接递增插入,否则 \(B_k=B_{k-1}\) ,亦或者存在一个 \(j\) ,使 \(B_j,B_{j+1},\cdots,B_{k}\) 相等,且代价更小(中位数的性质)。

这样我们考虑DP:

\(f[i][j]\) 前 \(i\) 个数,最后一个数为 \(j\) (注意 \(j\) 是离散化后的位置)

\(f[i][j]=\min_{0 \leq k \leq j}(f[i-1][k]+|j-A_i|)\)

注意到对于一个 \(i\) 决策集合是在单调扩大的,所以可以用一个变量记录前面的最小值,直接转移。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define R register int
using namespace std;
namespace Luitaryi {
inline int g() { R x=0,f=1;
register char ch; while(!isdigit(ch=getchar())) f=ch=='-'?-1:f;
do x=x*10+(ch^48); while(isdigit(ch=getchar())); return x*f;
} const int N=2010;
int n,m,a[N],b[N];
int f[N][N],ans=1e+9;
inline void main() {
n=g(); for(R i=1;i<=n;++i) b[i]=a[i]=g();
sort(b+1,b+n+1); R m=unique(b+1,b+n+1)-b-1;
memset(f,0x3f,sizeof f); f[0][0]=0;
for(R i=1;i<=n;++i) {
R mn=f[i-1][0];
for(R j=1;j<=m;++j) {
mn=min(mn,f[i-1][j]);
f[i][j]=mn+abs(a[i]-b[j]);
}
} for(R i=1;i<=m;++i) ans=min(ans,f[n][i]);
printf("%d\n",ans);
}
} signed main() {Luitaryi::main(); return 0;}

2019.09.18

58

POJ3336 Making the Grade的更多相关文章

  1. kaungbin_DP S (POJ 3666) Making the Grade

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  2. POJ 3666 Making the Grade

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  3. CF719C. Efim and Strange Grade[DP]

    C. Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input sta ...

  4. POJ3666Making the Grade[DP 离散化 LIS相关]

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6445   Accepted: 2994 ...

  5. [CareerCup] 15.7 Student Grade 学生成绩

    15.7 Imagine a simple database storing information for students' grades. Design what this database m ...

  6. 英语语法 It all started the summer before second grade when our moving van pulled into her neighborhood

    It all started the summer before second grade when our moving van pulled into herneighborhood It all ...

  7. FPGA speed grade

    Altera的-6.-7.-8速度等级逆向排序,Xilinx速度等级正向排序. 不很严密地说,“序号越低,速度等级越高”这是Altera FPGA的排序方法, “序号越高,速度等级也越高”这是Xili ...

  8. HDU 5038 Grade(分级)

    Description 题目描述 Ted is a employee of Always Cook Mushroom (ACM). His boss Matt gives him a pack of ...

  9. hdu---(5038)Grade(胡搞)

    Grade Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Sub ...

随机推荐

  1. Office常用快捷键大全,包含 Word、Excel、PowerPoint

    功能 快捷键 查找文字.格式和特殊项 Ctrl+G 使字符变为粗体 Ctrl+B 为字符添加下划线 Ctrl+U 删除段落格式 Ctrl+Q 复制所选文本或对象 Ctrl+C 剪切所选文本或对象 Ct ...

  2. Calibre 和 Kindle 配合的使用方法

    1. 前言 使用"Calibre"软件,把网上下载的电子书转换成适合kindle阅读的格式. 2. 使用经验总结 2.1 首行缩进.段落间距设置 2.2 输出为mobi格式设置 2 ...

  3. 把cgrep mgrep集成到bashrc

    https://android.googlesource.com/platform/build/+/android-4.4.3_r1/envsetup.sh 在~/.bashrc里面增加: #Andr ...

  4. wordpress 后台添加 快速编辑 栏目

    前两篇其实是同一篇,都是讲在后台添加菜单类型的http://www.ashuwp.com/courses/highgrade/664.htmlhttps://shibashake.com/wordpr ...

  5. sql根据时间戳按年月日分组统计

    sql根据时间戳按年月日分组统计,用于按日期分类: create_time为时间格式(字段名create_time 根据自己表字段修改,数据库中存为201610071202) SELECT DATE_ ...

  6. WUSTOJ 1365: 矩阵旋转(Java)

    题目链接:

  7. hdu 1325

    .,. 还是待整理 #include <stdio.h> const; typedef struct { int num,root,conn;//数据.根.入度 }Node; Node n ...

  8. (十一)springmvc和spring的整合

    1:Maven引入相关的jar包. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

  9. Eclipse 反编译工具 jad

    ** 1 下载 jad工具 ** 2 将.exe文件放在jdk安装路径下,里面有java ,javac 等命令,然后将jad.jar放在eclipse的dropins目录下 ** 3 启动eclips ...

  10. IE6图片透明问题

    网上很多解决IE6下png透明问题的方案,但是经本人实践,有的时候有用,有的时候并不能解决自己的问题.当是后者的时候,想到另外一种办法,就是当在IE6.IE7下使用gif图片,自己在测试的时候,如果g ...