POJ3336 Making the Grade
思路: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的更多相关文章
- 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 ...
- POJ 3666 Making the Grade
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- 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 ...
- POJ3666Making the Grade[DP 离散化 LIS相关]
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6445 Accepted: 2994 ...
- [CareerCup] 15.7 Student Grade 学生成绩
15.7 Imagine a simple database storing information for students' grades. Design what this database m ...
- 英语语法 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 ...
- FPGA speed grade
Altera的-6.-7.-8速度等级逆向排序,Xilinx速度等级正向排序. 不很严密地说,“序号越低,速度等级越高”这是Altera FPGA的排序方法, “序号越高,速度等级也越高”这是Xili ...
- HDU 5038 Grade(分级)
Description 题目描述 Ted is a employee of Always Cook Mushroom (ACM). His boss Matt gives him a pack of ...
- hdu---(5038)Grade(胡搞)
Grade Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Sub ...
随机推荐
- Can you answer these queries III
Can you answer these queries III 题目:洛谷 SPOJ [题目描述] 给定长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“0 x y”,把A[x]改 ...
- 剑指offer15:反转链表后,输出新链表的表头。
1 题目描述 输入一个链表,反转链表后,输出新链表的表头. 2 思路和方法 (1)利用栈作为中间存储,进行链表的反转,将压入的数据按先进后出的顺序弹出依次赋给链表再输出表头pHead. (2)将当前节 ...
- hdu--1232 继续通畅工程
wa了8次,超级崩溃,险些自闭,不过倒是学到了很多,先来一段代码: #include<bits/stdc++.h> using namespace std; ]; //储存查并集 int ...
- Solr综合案例深入练习
1. 综合案例 1.1. 需求 使用Solr实现电商网站中商品信息搜索功能,可以根据关键字.分类.价格搜索商品信息,也可以根据价格进行排序,并且实现分页功能. 界面如下: 1.2. 分析 开发人员需要 ...
- java 框架-分布式服务框架1ZooKeeper
https://www.cnblogs.com/felixzh/p/5869212.html Zookeeper的功能以及工作原理 1.ZooKeeper是什么?ZooKeeper是一个分布式的, ...
- kong命令(三)route
介绍 route 是一套匹配客户端请求的规则.每个route都会匹配一个service,每个service可定关联多个route. 可以说service:route=1:n.一对多的关系.每个匹配到r ...
- SIP协议参数详解
1.1 SIP消息分类 SIP协议是以层协议的形式组成的,就是说它的行为是以一套相对独立的处理阶段来描述的,每个阶段之间的关系不是很密切. SIP协议将Server和User Agent之间的通讯的 ...
- Python多个装饰器的顺序 转载
3.使用两个装饰器当一个装饰器不够用的话,我们就可以用两个装饰器,当然理解起来也就更复杂了,当使用两个装饰器的话,首先将函数与内层装饰器结合然后在与外层装饰器相结合,要理解@语法的时候到底执行了什么, ...
- [Lua性能] 小试验一例
local s1 = os.clock() local list1 = {} , do list1[#list1 + ] = end local e1 = os.clock() print(" ...
- 如何在vscode中调试vue-cli项目?
一:参考官网文档,写的还是很清楚的:https://cn.vuejs.org/v2/cookbook/debugging-in-vscode.html 二:需要安装的东西和初始项目 2.1 用vue ...