【题目链接】 http://codeforces.com/problemset/problem/713/C

【题目大意】

  给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上或者减去某个数,调整的代价是加上或者减去的数的绝对值之和,请你输出最小代价。

【题解】

  先考虑这样一个问题,如果是非严格单调递增该如何做,我们会发现每次调整,都是调整某个数字为原先数列中存在的数字,最后才是最优的,所以,我们设DP[i][j]表示前i个数字,最后一个数为原先数列排序后第j大的数字的最小代价,那么做一遍n2的DP就能够获得答案,现在题目中要求的是严格单调递增,那么就用到一种经典的处理方法,a[i]=a[i]-i,这样子就转化为非严格单调的问题了。

【代码】

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. const int N=3010;
  5. int n,a[N],b[N];
  6. long long dp[N][N];
  7. int main(){
  8. scanf("%d",&n);
  9. for(int i=1;i<=n;i++){
  10. scanf("%d",a+i);
  11. a[i]-=i; b[i]=a[i];
  12. }sort(b+1,b+n+1);
  13. for(int i=1;i<=n;i++){
  14. long long mn=dp[i-1][1];
  15. for(int j=1;j<=n;j++){
  16. mn=min(mn,dp[i-1][j]);
  17. dp[i][j]=abs(a[i]-b[j])+mn;
  18. }
  19. }long long ans=dp[n][1];
  20. for(int i=1;i<=n;i++)ans=min(ans,dp[n][i]);
  21. printf("%I64d\n",ans);
  22. return 0;
  23. }

  

Codeforces 713C Sonya and Problem Wihtout a Legend(单调DP)的更多相关文章

  1. Codeforces 713C Sonya and Problem Wihtout a Legend(DP)

    题目链接   Sonya and Problem Wihtout a Legend 题意  给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...

  2. Codeforces 713C Sonya and Problem Wihtout a Legend DP

    C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

  3. codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)

    题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...

  4. Codeforces 713C Sonya and Problem Wihtout a Legend

    题意:给一个序列,可以进行若干次操作,每次操作选择一个数让它+1或-1,问最少要几次操作使得序列变为严格单调递增序列. 题解:首先考虑非严格递增序列,则每个数字最终变成的数字一定是该数组中的某个数.那 ...

  5. Codeforces C. Sonya and Problem Wihtout a Legend(DP)

    Description Sonya was unable to think of a story for this problem, so here comes the formal descript ...

  6. CodeForces 714E Sonya and Problem Wihtout a Legend(单调数列和DP的小研究)

    题意:给你n个数字,每个数字可以加减任何数字,付出变化差值的代价,求最后整个序列是严格单调递增的最小的代价. 首先我们要将这个题目进行转化,因为严格单调下是无法用下面这个dp的方法的,因此我们转化成非 ...

  7. Codeforces713C Sonya and Problem Wihtout a Legend(DP)

    题目 Source http://codeforces.com/problemset/problem/713/C Description Sonya was unable to think of a ...

  8. codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)

    题目链接: C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 ...

  9. codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)(将一个数组变成严格单增数组的最少步骤)

    E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

随机推荐

  1. Ubuntu安装Mysql及使用

    (1)在线安装:sudo apt-get install mysql-server(2)管理mysql: sudo /etc/init.d/mysql start===========>开启my ...

  2. Android API在不同版本系统上的兼容性

    随着安卓版本的不断更新,新的API不断涌出,有时候高版本的API会在低版本crash的. 如果minSdkVersion设置过低,在build的时候,就会报错(Call requires API le ...

  3. MySQLdb autocommit

    MySQLdb 中 autocommit 默认是关闭的,下面是例子. import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root ...

  4. php正则表达式图谱

    php 正则表达式分类图:

  5. 电池和Adapter切换电路改进实验

    目的:很多单电池的机器在大负载的情况下,如把背光开到最亮,运行3D游戏,此时拔DC电源很容易出现机器死机,或花屏现象: 原因:Q5的导通时间不够,希望通过G极的快速放电,加快到导通时间: 修改前的电路 ...

  6. Unix/Linux环境C编程入门教程(7) OPENBSDCCPP开发环境搭建

    1. 年发起了OpenBSD 专案,希望创造一个注重安全的操作系统. 2.创建一个虚拟机. 3.选择默认的workstation10.0 4.我们选择稍后安装操作系统. 5.我们选择FreeBSD64 ...

  7. 自定义UITextField(UITextField重写)

    // CustomField.h #import <UIKit/UIKit.h> @interface CustomField : UITextField @end // CustomFi ...

  8. DBCP|C3P0参数详解

    1.<!-- 数据源1 --> 2. <bean id="dataSource" 3. class="org.apache.commons.dbcp.B ...

  9. ExecuteScalar

    ExecuteScalar运行查询,并返回查询所返回的结果集中第一行的第一列或空引用(假设结果集为空).忽略其它列或行. 使用 ExecuteScalar 方法从数据库中检索单个值. 由于不用创建行集 ...

  10. winfrom运用webservice上传文件到服务器

    winfrom做文件上传的功能显然没有BS的简单,本实例是运用了webservice获取二进制流转换的字符串.然后,解析字符串,把流文件再转成pdf. webservice 里面的代码为下: [Web ...