poj 3666 Making the Grade 动态规划+滚动数组
来源:互联网 发布:钢结构详图软件 编辑:程序博客网 时间:2024/05/17 07:11
题意:给定一个序列,以最小代价将其变成单调不增或单调不减序列。
d[i][j] 代表前i个数最大为b[j]时的最小代价,d[i][j]=min(d[i-1][k])+abs(a[i]-b[j]),(0<k<=j),这里可以用滚动数组来存储min(d[i-1][k])。这里的滚动数组简直神来之笔,把一个二维数组压缩成一个变量,节省了大量时间空间。
题目bug,这里只求单调不减序列。
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define INF 0x7fffffff#define ll long long#define N 2200#define Abs(a) ((a)>0?(a):-(a))using namespace std;ll a[N],b[N];long long int d[N][N];int main(){ int n; while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); b[i]=a[i]; } sort(b+1,b+n+1); memset(d,0,sizeof(d)); for(int i=1;i<=n;i++) { ll mmin=d[i-1][1]; for(int j=1;j<=n;j++) { mmin=min(mmin,d[i-1][j]); d[i][j]=mmin+Abs(a[i]-b[j]); } } ll ans=INF; for(int i=1;i<=n;i++) ans=min(ans,d[n][i]); cout<<ans<<endl; }}
1 0
- poj 3666 Making the Grade 动态规划+滚动数组
- poj 3666 Making the Grade (动态规划)
- 动态规划-POJ 3666 Making the Grade
- POJ 3666 Making the Grade 滚动数组+状态压缩DP
- poj 3666 Making the Grade (动态规划)
- POJ 3666 Making the Grade(动态规划)
- poj 3666 Making the Grade (动态规划)
- poj 3666 Making the Grade (离散化+动态规划)
- 动态规划 Making the Grade
- 动态规划训练23 [Making the Grade POJ
- Making the Grade poj 3666(离散化+滚动数组+dp)
- POJ 3666 Making the Grade
- POJ 3666 Making the Grade
- POJ 3666 Making the Grade
- poj 3666--Making the Grade
- POJ 3666 Making the Grade
- poj 3666 Making the Grade
- POJ 3666 Making the Grade
- 如何在gns3上用路由器模拟PC机和交换机
- mybatis注解
- 三个最简单的排序算法:插入、冒泡、选择
- 第5周项目1—三角形类雏形(3、构造函数)
- 博客是什么东西?写这个有什么要求么?
- poj 3666 Making the Grade 动态规划+滚动数组
- Android基于HttpURLConnection的异步下载图片
- hi3516a的uboot自动升级相关问题的解决
- leetcode—— 171——Excel Sheet Column Number
- centos启动时间查看
- 自定义View之onMeasure()
- SSH图片上传
- nrf51822 --- 动态修改连接间隔
- CSS---cursor鼠标属性