poj3666-基础DP
来源:互联网 发布:木村秋则 知乎 编辑:程序博客网 时间:2024/06/05 08:53
题意:然你求如何使得这个序列单调不减或者单调不增然后代价比较小,因为数据比较弱只要求单调不增就可以了
题解:dp[i][j]表示第i个位置改成第j小的数字代价花的最少然后记录一下i-1个位置改成第1-第j小的代价是多少就可以
dp[i][j] = pre[i-1][j]+abs(b[j]-a[i]);
pre[i][j] = min(dp[i][j],pre[i][j-1]);
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int inf = 0x3f3f3f3f;const int mx = 2005;int dp[mx][mx];int pre[mx][mx];int a[mx];int b[mx];int main(){ int n; while(scanf("%d",&n)!=EOF){ memset(dp,inf,sizeof(dp)); memset(pre,inf,sizeof(pre)); for(int i = 1; i <= n; i++){ scanf("%d",&a[i]); b[i] = a[i]; pre[0][i] = 0; } sort(b+1,b+1+n); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++){ dp[i][j] = pre[i-1][j]+abs(b[j]-a[i]); pre[i][j] = min(dp[i][j],pre[i][j-1]); } int ans = inf; for(int i = 1; i <= n; i++) ans = min(ans,dp[n][i]); printf("%d\n",ans); } return 0;}
阅读全文
0 0
- poj3666-基础DP
- poj3666(dp前缀优化)
- poj3666 Making the Grade dp
- POJ3666 离散化的dp
- POJ3666-Making the Grade-dp
- poj3666
- poj3666
- poj3666
- POJ3666
- poj3666
- POJ3666 Making the Grade [DP,离散化]
- POJ3666 Making the Grade [DP,离散化]
- POJ3666 Making the Grade [DP,离散化]
- poj3666(离散思想,lis,dp)
- 线性DP POJ3666 Making the Grade
- POJ3666 Making the Grade 离散化+DP
- POJ3666 making the grade dp+前缀+离散化
- POJ3666 Making the Grade(DP,离散化 待整理)
- nginx使用geoip判断国家
- 剑指offer 二叉树中和为某一值的路径 java实现
- Android项目基础
- android(安卓)代码规范参考指南
- 螺旋矩阵
- poj3666-基础DP
- 乐观锁和悲观锁介绍
- 1.1 组件注册 vue shop
- ubuntu下mysql基本操作笔记
- linux mysql access denied for user ‘root’@’localhost'(using password:YES)
- synchronized和ReentrantLock
- Activity启动过程简要介绍
- 下拉多选控件介绍
- x86内存框架简介