Codeforces 713C Sonya and Problem Wihtout a Legend DP(经典)
来源:互联网 发布:linux做网站需要环境 编辑:程序博客网 时间:2024/05/21 18:37
题意:给出长度为n的序列a,op:将任意一个数增大/减小1.
n<=3000,a[i]<=1e9,问使序列a严格递增所需要的最少op次数
当a递增时:a[i]<a[i+1] a[i]<=a[i+1]-1 a[i]-i<=a[i+1]-(i+1)
也就是b[i]=a[i]-i非递减.同样b非递减时,a也就是递增的.转为求使b非递减最小代价
n<=3000,a[i]<=1e9,问使序列a严格递增所需要的最少op次数
当a递增时:a[i]<a[i+1] a[i]<=a[i+1]-1 a[i]-i<=a[i+1]-(i+1)
也就是b[i]=a[i]-i非递减.同样b非递减时,a也就是递增的.转为求使b非递减最小代价
序列b非递减,则最后b中元素都为原来的某些元素(归纳法证).设dp[i][j]:前i个元素变非递减,第i个元素为原来第j大时的最小代价.
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N=3e3+20;const ll inf=2e16;ll n,a[N],b[N];ll dp[N][N];int main(){while(cin>>n){for(int i=1;i<=n;i++)scanf("%I64d",&a[i]),a[i]-=i,b[i]=a[i];sort(b+1,b+1+n);for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)dp[i][j]=inf;dp[0][0]=0;for(int i=1;i<=n;i++){ll mn=inf;for(int j=1;j<=n;j++){//dp[i][j]=min(dp[i][j],dp[i-1][k]+abs(a[i]-b[j]));k<=jmn=min(mn,dp[i-1][j]);dp[i][j]=mn+abs(a[i]-b[j]);}}ll ans=inf;for(int i=1;i<=n;i++)ans=min(ans,dp[n][i]);cout<<ans<<endl;}return 0;}
阅读全文
0 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)
- [DP] Codeforces #714E. Sonya and Problem Wihtout a Legend
- 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变形>
- cf/Codeforces Round #371 714E - Sonya and Problem Wihtout a Legend - dp +lis
- Codeforces Round #371 (Div. 2) E. Sonya and Problem Wihtout a Legend (DP/LIS变形)
- codeforces714e Sonya and Problem Wihtout a Legend
- LightOJ 1056 Olympics(数学公式)
- 各种分类算法比较
- 解析KMP模式匹配算法
- uva11491
- Action Bar
- Codeforces 713C Sonya and Problem Wihtout a Legend DP(经典)
- 【Android】SQLite 创建数据库,并实现增删改查
- DSRC
- C盘空间异常(坑)
- 8-11
- html的基本标签
- kata题目
- xamarin学习笔记A10(安卓SQLite)
- 利用什么软件可以将PDF文件进行文件的拆分