POJ3666 Making the Grade

题意:

给定一个长度为n的序列A,构造一个长度为n的序列B,满足b非严格单调,并且最小化S=∑i=1|Ai-Bi|,求出这个最小值S,1<=N<=2000,1<=Ai<=1e9.

引理:在满足S最小化的情况下,一定存在一种构造序列B的方案,使得B中的数值都在A中出现过。

由此,用一个数组b[i]初始化=a[i],然后对b从小到大排序,用f[i][j]表示完成了B中前i个数的构造,第i个数为b[j]时的最小的S.当第i个数等于b[j]时,因为B序列是单调递增的,所以之前构造的数一定在b[1]~b[j]之间,用tmp维护其最小值即可,则有:

    for(res i= ; i<=n ; i++)
{
LL tmp=f[i-][];
for(res j= ; j<=n ; j++)
{
tmp=min(tmp,f[i-][j]);
f[i][j]=tmp+abs(a[i]-b[j])
}
}

最后答案即为f[1~n]的最小值。

进一步优化:

发现第一维可以省略掉。

完整代码:

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; #define INF (2147483640)
const int N=+;
int a[N],b[N];
int f[N],n; int main()
{
scanf("%d",&n);
for(int i= ; i<=n ; i++) scanf("%d",&a[i]),b[i]=a[i]; sort(b+,b+n+);
int ans=INF;
for(int i= ; i<=n ; i++)
{
int t=INF;
for(int j= ; j<=n ; j++)
{
t=min(t,f[j]);
f[j]=abs(b[j]-a[i])+t;
}
}
for(int i= ; i<=n ; i++)
ans=min(ans,f[i]);
printf("%d\n",ans);
return ;
}

POJ3666 Making the Grade的更多相关文章

  1. poj3666 Making the grade【线性dp】

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:10187   Accepted: 4724 ...

  2. POJ3666 Making the Grade [DP,离散化]

    题目传送门 Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9090   Accepted: ...

  3. poj3666 Making the Grade(基础dp + 离散化)

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

  4. [poj3666]Making the Grade(DP/左偏树)

    题目大意:给你一个序列a[1....n],让你求一个序列b[1....n],满足 bi =a && bc,则最小的调整可以是把b变成c. 所以归纳可知上面结论成立. dp[i][j] ...

  5. 常规DP专题练习

    POJ2279 Mr. Young's Picture Permutations 题意 Language:Default Mr. Young's Picture Permutations Time L ...

  6. 「kuangbin带你飞」专题十二 基础DP

    layout: post title: 「kuangbin带你飞」专题十二 基础DP author: "luowentaoaa" catalog: true tags: mathj ...

  7. BZOJ1592 POJ3666 [Usaco2008 Feb]Making the Grade 路面修整 左偏树 可并堆

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3666 题目传送门 - BZOJ1592 题意概括 整条路被分成了N段,N个整数A_1, ... , ...

  8. LG2893/POJ3666 「USACO2008FEB」Making the Grade 线性DP+决策集优化

    问题描述 LG2893 POJ3666 题解 对于\(A\)中的每一个元素,都将存在于\(B\)中. 对\(A\)离散化. 设\(opt_{i,j}\)代表\([1,i]\),结尾为\(j\)的最小代 ...

  9. Making the Grade(POJ3666)

    题目大意: 给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调增或者单调减(不严格). 题解: 1.一开始我有一个猜想,就是不管怎么改变,最终的所有数都是原来的某个数. ...

随机推荐

  1. 关于IOS新手在安装cocoa pods失败,因为ruby版本过低的解决方法+ (void) {升级ruby}

    http://blog.csdn.net/zhaoen95/article/details/51995520     现在: 使用 OS 系统, 正在学习iOS 需要配置cocoapods 命令行中显 ...

  2. cs4.1 编译与安装

    cs4.1编译报 https://issues.apache.org/jira/browse/CLOUDSTACK-2913 cs4.1安装报

  3. [c++] How many bytes do pointers take up?

    How many bytes do pointers take up? on 16 bit systems take up 2 bytes on 32 bit systems take up 4 by ...

  4. SliceBox

    SliceBox相当于一个轮播图插件,只不过是3D的. 先来查看它能实现的效果: 官网:http://tympanus.net/codrops/2011/09/05/slicebox-3d-image ...

  5. 原型设计工具Mockplus新年送福利,见者有份

    为感谢大家对Mockplus的喜爱与支持,给大家送福利了! 送送送,见者有份! 参与活动,三分钟时间,均可最高获赠专业版1个月使用时间.见者有份! 领福利步骤: 1. 扫码关注微信公众号:Mockpl ...

  6. Mosquitto 单向SSL配置

    Mosquitto 单向SSL配置 摘自:https://blog.csdn.net/a_bcd_123/article/details/70167833 2017年04月14日 06:56:06 s ...

  7. 在windows上编译wireshark源代码

    终于在windows上成功编译了wireshark源代码,个中酸辛,都是泪..只能说要多试! windows上编译wireshark共用到三个东西:wireshark源代码.python.cygwin ...

  8. win7设置开机启动virtualBOX虚拟机

    如果常用VirtualBox虚拟机系统的话,设置随开机启动也是很方便的.不需要打开VirtualBox窗口,直接启动VirtualBox虚拟机系统就可以了. 设置开机自启动VirtualBox虚拟机系 ...

  9. 使用word 2007 发布csdn博客

    目前大部分的博客作者在写博客这件事情上都会遇到以下3个痛点:1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.2.发布到博客或公众号平台 ...

  10. phpize命令在安装AMQP插件是报错phpize:Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF envir的解决方法

    phpize命令在安装AMQP插件是报错phpize:Cannot find autoconf. Please check your autoconf installation and the $PH ...