【POJ 3666】Making the Grade(简单DP)
来源:互联网 发布:浙江政务服务软件 编辑:程序博客网 时间:2024/05/16 08:12
首先可以明确一个方面,那就是如果将X改成Y,那么Y肯定是这N个数中的某一个(为什么仔细想想)
这样已经可以AC了,但是我们还可以利用滚动数组去优化空间
之后可以得到一个状态转移那就是dp[i][j]代表已经考虑了i位的情况下,结尾为j的最小更改数。
状态转移为dp[i][j] = min(dp[i-1][k] + abs(a[i] - b[j])) 这样的话可以写出一个初步的代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int INF = 1 << 30;const int maxn = 2005;int arr[maxn];int Hash[maxn],cnt;int dp[maxn][maxn];int n;void Hash_init(){ sort(Hash,Hash + cnt); cnt = unique(Hash,Hash + cnt) - Hash; //printf("%d\n",cnt);}void solve(){ dp[0][0] = 0; for(int i = 1; i < n; i++){ for(int j = 0; j < cnt; j++){ dp[i][j] = INF; for(int k = 0; k <= j; k++){ dp[i][j] = min(dp[i][j],dp[i - 1][k] + abs(Hash[j] - arr[i])); } } } int ans = dp[n - 1][0]; for(int i = 1; i < cnt; i++) ans = min(ans,dp[n - 1][i]); printf("%d\n",ans);}int main(){ while(scanf("%d",&n) != EOF){ cnt = 0; for(int i = 0; i < n; i++){ scanf("%d",&arr[i]); Hash[cnt++] = arr[i]; } Hash_init(); solve(); } return 0;}
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int INF = 1 << 30;const int maxn = 2005;int arr[maxn],pre[maxn];int Hash[maxn],cnt;int dp[maxn][maxn];int n;void Hash_init(){ sort(Hash,Hash + cnt); cnt = unique(Hash,Hash + cnt) - Hash;}void solve(){ dp[0][0] = 1; for(int i = 1; i < n; i++){ for(int j = 0; j < cnt; j++){ dp[i][j] = pre[j] + abs(Hash[j] - arr[i]); if(!j) pre[j] = dp[i][j]; else pre[j] = min(pre[j - 1],dp[i][j]); } } int ans = dp[n - 1][0]; for(int i = 1; i < cnt; i++) ans = min(ans,dp[n - 1][i]); printf("%d\n",ans);}int main(){ while(scanf("%d",&n) != EOF){ cnt = 0; for(int i = 0; i < n; i++){ scanf("%d",&arr[i]); Hash[cnt++] = arr[i]; } Hash_init(); solve(); } return 0;}
这样已经可以AC了,但是我们还可以利用滚动数组去优化空间
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 2005;int arr[maxn],pre[maxn];int Hash[maxn],cnt;int dp[2][maxn];int n;void Hash_init(){ sort(Hash,Hash + cnt); cnt = unique(Hash,Hash + cnt) - Hash;}void solve(){ dp[0][0] = 1; int k = 0; for(int i = 1; i < n; i++){ for(int j = 0; j < cnt; j++){ dp[k][j] = pre[j] + abs(Hash[j] - arr[i]); if(!j) pre[j] = dp[k][j]; else pre[j] = min(pre[j - 1],dp[k][j]); } k ^= 1; } k ^= 1; int ans = dp[k][0]; for(int i = 1; i < cnt; i++) ans = min(ans,dp[k][i]); printf("%d\n",ans);}int main(){ while(scanf("%d",&n) != EOF){ cnt = 0; for(int i = 0; i < n; i++){ scanf("%d",&arr[i]); Hash[cnt++] = arr[i]; } Hash_init(); solve(); } return 0;}恩,就是这样~
1 0
- 【POJ 3666】Making the Grade(简单DP)
- POJ 36666 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
- 构建一个二叉堆实例
- 谈谈Fragment的构造函数、重新创建(recreate)及相关
- 使用session在不同页面之间传递参数,sqldatasource按条件查询
- PEM_密钥对生成与读取方法
- 初开技术博客
- 【POJ 3666】Making the Grade(简单DP)
- win8.1、win10系统修改窗口背景颜色为健康的护眼颜色
- 对于begin和end的认识
- Android4.0 Launcher源码研究
- 【AJAX02】AJAX实战
- 在VS2013中配置Opengl
- 数据结构与算法——链栈
- Ant 批量打包
- ”Windows 无法安装到这个磁盘。选中的磁盘具有MBR分区表“ 的处理