POJ 3666 Making the Grade【dp】
来源:互联网 发布:tina仿真软件下载 编辑:程序博客网 时间:2024/04/29 19:01
网上把这个题称为了深度好题:其实,当做脑洞+DP更为合适的吧
先说说别人的想法:离散化加DP,为什么要离散化?!因为需要枚举的数太多了,但是真正有用的只有最多2000个(n最大为2000)与n有关
为什么?!
很简单:因为我们需要判断的数值只可能是当前出现过的值!在数组中的值
比如样例:1 3 2 4 5 3 9
要想是最优解,那么不可能出现一个从来没有出现过的值:因为如果要把某个数值调整大,肯定是往“相邻”的大值上考虑,变大一格没有意义;同理,调整小也是没有意义的,当然,最小值和最大值是不需要调整的
那么意味着,每个位置可能调整的值最多n个(n个数都不相同的时候)
那么,就可以开始设计动态规划思想了
设dp【i】【j】为当前处理到了第i个数,当前的数列已经非递减,当前的最大值是从小到大排序后的第j个数(这儿被称为了离散化,但是我觉得是个贪心或者脑洞)所需要的最小花费
转移方程为:dp【i】【j】=abs(b【j】-a【i】)+Min(dp【i-1】【k】),其中1<=k<=j
当j从1变化到n时,可以发现dp【i-1】【k】是一个前缀的最小值,每次前缀处理就好了
代码如下:
#include<iostream>#include<cstdio>#include<stdio.h>#include<cstdlib>#include<stdlib.h>#include<algorithm>#include<string.h>#include<cstring>using namespace std;#define LL long longconst int maxn=2050;const LL INF=0x3f3f3f3f3f3f3f3f;LL a[maxn],b[maxn];LL dp[maxn][maxn];int n;int main(){ while(scanf("%d",&n)!=EOF){ for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); b[i]=a[i]; } sort(b+1,b+n+1); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++){ LL Min=INF; for(int j=1;j<=n;j++){ Min=min(Min,dp[i-1][j]); if (a[i]-b[j]>0) dp[i][j]=a[i]-b[j]+Min; else dp[i][j]=b[j]-a[i]+Min; //dp[i][j]=abs(a[i]-b[j])+Max; } } LL ans=INF; for(int i=1;i<=n;i++) ans=min(ans,dp[n][i]); printf("%lld\n",ans); } return 0;}
0 0
- POJ 3666 Making the Grade [DP]
- POJ 3666 Making the Grade (dp)
- 【POJ 3666】Making the Grade(简单DP)
- poj 3666 Making the Grade 【dp】
- POJ 3666--Making the Grade(dp)
- POJ - 3666 Making the Grade(DP)
- DP-POJ-3666-Making the Grade
- POJ 3666 Making the Grade(dp)
- [POJ 3666]Making the Grade[DP]
- POJ 3666 Making the Grade(DP)
- POJ 3666 Making the Grade【DP】
- POJ 3666 Making the Grade【dp】
- Poj Making the Grade【dp】
- 【dp】Making the Grade POJ
- POJ 3666 Making the Grade
- POJ 3666 Making the Grade
- POJ 3666 Making the Grade
- poj 3666--Making the Grade
- DB2 how to drop overloaded functions
- storm安装
- 【汇编】十以内冒泡排序
- A - 搜索I 简单图论
- 同个电脑多个ssh key的配置使用
- POJ 3666 Making the Grade【dp】
- Spring的代理(proxy)模式
- 便携版git(git-portable)的初始化配置
- mysql查询缓存
- NOIP-2016-PJ-总结
- 高中Oj1434. 灌水
- mysql 学习笔记
- Scala开篇-开发环境搭建-IntelliJ IDEA
- Gif格式简要介绍