Codeforces 713C C. Sonya and Problem Wihtout a Legend (经典DP)
来源:互联网 发布:整站源码带数据下载 编辑:程序博客网 时间:2024/06/08 17:51
给你一个数列,对于每个数字你都可以++或者−−
直接考虑结果的整个数列 必然存在那么几个数 他是从始至终 都没有变过 那么假设有一些数会受到这个数影响 比如一个数ai不变 如果aj 是受ai影响 那么就会消耗
先考虑这样一个问题,如果是非严格单调递增该如何做,我们会发现每次调整,都是调整某个数字为原先数列中存在的数字,最后才是最优的,所以,我们设DP[i][j]表示前i个数字,最后一个数为原先数列排序后第j大的数字的最小代价,那么做一遍n2的DP就能够获得答案,现在题目中要求的是严格单调递增,那么就用到一种经典的处理方法,a[i]=a[i]-i,这样子就转化为非严格单调的问题了。
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 3e3 + 5;const ll INF = 0x7fffffffffffffff; //这里不是const int 是 const llll a[maxn], b[maxn], dp[maxn][maxn]; //因为每个数都在1e9范围内,所以要用llint main(){ int n; cin >> n; for(int i = 1; i <= n; i++) { cin >> a[i]; a[i] -= i; //这里就是把严格递增转换成非严格递增 b[i] = a[i]; } sort(b+1, b+n+1); for(int i = 1; i <= n; i++) { ll min1 = INF; for(int j = 1; j <= n; j++) { min1 = min(min1, dp[i-1][j]); //这个min1表示的就是i-1个数并且最后一个数小于等于b[j]的最小值,这种记录方式很好 dp[i][j] = min1 + abs(a[i]-b[j]); } } ll ans = INF; for(int j = 1; j <= n; j++) { ans = min(dp[n][j], ans); //看哪个结尾最小 } cout << ans << endl; return 0;}
1 0
- 【DP】[CodeForces - 713C]Sonya and Problem Wihtout a Legend
- codeforces 713C Sonya and Problem Wihtout a Legend【dp】
- Codeforces 713C C. Sonya and Problem Wihtout a Legend (经典DP)
- Codeforces 713C Sonya and Problem Wihtout a Legend DP(经典)
- Codeforces-713C Sonya and Problem Wihtout a Legend
- Codeforces-713C-Sonya and Problem Wihtout a Legend
- Codeforces 713C Sonya and Problem Wihtout a Legend(离散化dp)
- Codeforces Round 371 C Sonya and Problem Wihtout a Legend
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend(贪心+DP)
- Codeforces Round #371 C. Sonya and Problem Wihtout a Legend (DP)
- codeforces 713C(Round #371 Div2 E) Sonya and Problem Wihtout a Legend Dp + 前缀 +离散化
- codeforces 713C(Round #371 Div2 E) Sonya and Problem Wihtout a Legend ★ ★
- hihocoder#1529 : 不上升序列&&Codeforces-713C:Sonya and Problem Wihtout a Legend(思维)
- 【CF 713C】Sonya and Problem Wihtout a Legend(离散化+dp)<POJ 3666变形>
- [DP] Codeforces #714E. Sonya and Problem Wihtout a Legend
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend
- 动态规划,离散化(Sonya and Problem Wihtout a Legend,cf 713C)
- Codeforces #713C: Sonya and Problem without a Legend 题解
- 文章标题
- Problem C: 编写函数:矩阵乘法 (Append Code)
- 获取指定文件/目录命令
- 第14周 拓展练习(二)成绩处理函数版
- 观察者模式
- Codeforces 713C C. Sonya and Problem Wihtout a Legend (经典DP)
- 微服务化改造系列之四:授权中心
- hibernate批量处理
- android:TimePicker仿照IOS时间选择器,可自定义选择器
- AOP的实现的几种方式
- 守护进程
- 基于Puppet的自动化配置服务器系统
- 动态库制作
- sae+git+django上传