cf 372 div2 E. Sonya and Problem Wihtout a Legend dp
来源:互联网 发布:广发期货软件下载 编辑:程序博客网 时间:2024/06/06 03:32
题目地址:http://codeforces.com/contest/714/problem/E
题意:给一序列,可对这个序列的每个数做加一或减一的操作,问最小的操作数使这个序列变为严格递增序列
思路:看这个博客才明白的:http://blog.csdn.net/miracle_ma/article/details/52537208
首先如果题目要求的序列是非严格递增序列的话,则产生的新序列所含有的数一定可以都是原序列的数。
大致的证明思路:如果一个数要变小,那么这个数只要小到和它右边的数一样大就好了,再小得到的解不会是最优解;如果一个数要变大,那么这个数只要大到和它左边的数一样小就好了,再大得到的解不会是最优解。
试着把原问题转换为非严格递增序列的问题:只要使a[i]-=i就好了
转化为非严格递增后,如何解决,既然每个数都是原序列中的数,用dp[i][j]表示第i个数变为原序列第j大的数需要的成本,则有dp[i][j]=abs(b[i]-a[j])+min(dp[i-1][k]),b[i]指第i个数,a[j]指序列中第j大的数
#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;typedef long long LL;const int MAXN=3000+5;int a[MAXN],b[MAXN];LL maxn[MAXN],dp[MAXN][MAXN];int main(){ int n; while(~scanf("%d",&n)){ for(int i=0;i<n;++i){ scanf("%d",a+i); a[i]-=i; b[i]=a[i]; } sort(a,a+n); int sub=unique(a,a+n)-a; for(int i=0;i<sub;++i){ dp[0][i]=abs(b[0]-a[i]); if(i==0) maxn[i]=dp[0][i]; else maxn[i]=min(dp[0][i],maxn[i-1]); } for(int i=1;i<n;++i){ for(int j=0;j<sub;++j){ dp[i][j]=abs(b[i]-a[j])+maxn[j]; if(j==0) maxn[j]=dp[i][j]; else maxn[j]=min(maxn[j-1],dp[i][j]); } } LL ans=1e18; for(int i=0;i<sub;++i) ans=min(ans,dp[n-1][i]); printf("%I64d\n",ans); }}
0 0
- cf 372 div2 E. Sonya and Problem Wihtout a Legend dp
- cf 714 e Sonya and Problem Wihtout a Legend
- [DP] Codeforces #714E. Sonya and Problem Wihtout a Legend
- cf/Codeforces Round #371 714E - Sonya and Problem Wihtout a Legend - dp +lis
- 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 ★ ★
- 【CF 713C】Sonya and Problem Wihtout a Legend(离散化+dp)<POJ 3666变形>
- 【DP】[CodeForces - 713C]Sonya and Problem Wihtout a Legend
- codeforces 713C Sonya and Problem Wihtout a Legend【dp】
- Codeforces Round #371 (Div. 2) E. Sonya and Problem Wihtout a Legend (DP/LIS变形)
- codeforces714e Sonya and Problem Wihtout a Legend
- Codeforces Round #371 (Div. 2) E. Sonya and Problem Wihtout a Legend(技巧 + 离散化dp)
- 动态规划,离散化(Sonya and Problem Wihtout a Legend,cf 713C)
- Codeforces Round 371 C Sonya and Problem Wihtout a Legend
- Codeforces-713C Sonya and Problem Wihtout a Legend
- Codeforces-713C-Sonya and Problem Wihtout a Legend
- [堆 折线] Codeforces713C. Sonya and Problem Wihtout a Legend
- Codeforces 713C Sonya and Problem Wihtout a Legend(离散化dp)
- angullarjs实现锚点楼层跳转
- 堂娜·哈拉维:赛博格宣言
- 1024! 末尾有多少个0?
- golang beego结合editable进行成果登记系统的在线修改
- C++基础(三)成员函数 友元函数
- cf 372 div2 E. Sonya and Problem Wihtout a Legend dp
- POJ-3295-Tautology
- poj3295 Tautology(构造法)
- [socket]socketpair测试程序
- 关于nodejs的npm命令无反应的解决方案
- 百度面试题之二叉树层次遍历(从上到下,从下到上)
- 【Qzone】缘何似个愁 第二步 编译Android源代码
- Git_Git基础命令_第8课_重写历史记录
- 操作系统总结(一)