思路: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. Python【BeautifulSoup解析和提取网页数据】

    [解析数据] 使用浏览器上网,浏览器会把服务器返回来的HTML源代码翻译为我们能看懂的样子 在爬虫中,也要使用能读懂html的工具,才能提取到想要的数据 [提取数据]是指把我们需要的数据从众多数据中挑 ...

  2. Linux十大顶级发行版本

    当前顶级发行版概览   对于Linux新手来说,在各发行版之间困惑得进行选择和不断增加的数量实在令人头晕.这就是写作本文的原因,其中列出的10个(附加一个值得一提的FreeBSD,BSD一族中最为流行 ...

  3. awr报告没有数据11.2.0.3

    有个朋友,反馈AWR没有数据: 咨询版本:oracle企业版本11.2.0.3 SQL> select * from v$version; BANNER -------------------- ...

  4. vue实现web登陆权限控制

    实现原理:vueRouter控制前端页面跳转路由,当登录成功后,返回用户登录token信息,将token信息放到store中,router路由跳转取store中状态有token时,当取到token时跳 ...

  5. 关于安装Chrome插件elasticsearch-head

    因为Chrome商店不能使用,安装插件很麻烦. 可以在github上下载elasticsearch-Head插件:https://github.com/mobz/elasticsearch-head/ ...

  6. 在.netcore webapi项目中使用后台任务工具Hangfire

    安装Hangfire 在webapi项目中通过nuget安装Hangfire.Core,Hangfire.SqlServer,Hangfire.AspNetCore,截止到目前的最新版本是1.7.6. ...

  7. 如何判断 Session是否存在

    相信很多人都跟我一样,在写网页中有些位置通过其他网页设置了 Session然后跳转到目标页面就需要要用 Session,但是那个位置如果是直接打开的就用不到 Session,那么问题就来了,例如:系统 ...

  8. 基于【 Docker】三 || Docker的基本操作

    一.Docker常用命令 1.搜索镜像:docker search 镜像名称 2.下载镜像:docker pull 镜像名称:版本号 3.查看镜像:docker images 4.删除镜像:docke ...

  9. Unity UGUI Button 无法点击问题一例

    理论上,只要一个按钮自己这一层或者子节点有一个控件勾选了RaycastTarget,并且按钮为Interactable的.并且不被其他可以点击的控件阻挡,那么这个按钮就可以被点击 在调一个界面时发现一 ...

  10. nodejs入门API之http模块

    HTTP上的一些API及应用 HTTP模块上的服务(server)与响应(response) HTTP模块上的消息(message) HTTP模块上的代理(agent)与请求(request) HTT ...