Codeforces713C Sonya and Problem Wihtout a Legend(DP)
题目
Source
http://codeforces.com/problemset/problem/713/C
Description
Sonya was unable to think of a story for this problem, so here comes the formal description.
You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array.
Next line contains n integer ai (1 ≤ ai ≤ 109).
Output
Print the minimum number of operation required to make the array strictly increasing.
Sample Input
7
2 1 5 11 5 9 11
5
5 4 3 2 1
Sample Output
9
12
分析
题目打给说给一个序列,可以进行若干次操作,每次操作选择一个数让它+1或-1,问最少要几次操作使得序列变为严格单调递增序列。
首先考虑不是严格递减的情况,这其实是个经典问题的样子,太弱都没做过。。
- 不严格递减最后修改完成后的各个数一定是原序列中的某一个数
这个大概可以这么理解:原序列,从左到右扫过去,如果左边的大于右边的,要嘛左边的减掉使其等于右边的,要嘛右边的加上使其等于左边的。
于是就可以考虑用dp来做了:
- dp[i][j]表示序列前i个数都单调递增且第i个数更改为不大于原序列中第j个数的最少代价
- 转移就是dp[i][j]=min(dp[i][j-1],dp[i-1][j]+abs(a[i],b[j])),a是原序列,b是原序列排序去重的序列
回到原问题,原问题是求严格单调递增,这个可以转化成不严格单调递增:
- 让原序列每个数a[i]减去i
这样转化可行是因为:

这个转化感觉挺神奇的。。
代码
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
long long d[3333][3333],a[3333],b[3333];
int main(){
int n;
scanf("%d",&n);
for(int i=1; i<=n; ++i){
scanf("%d",a+i);
a[i]-=i;
b[i]=a[i];
}
sort(b+1,b+1+n);
int bn=unique(b+1,b+1+n)-b-1;
memset(d,127,sizeof(d));
for(int i=1; i<=bn; ++i){
d[1][i]=min(d[1][i-1],abs(a[1]-b[i]));
}
for(int i=2; i<=n; ++i){
for(int j=1; j<=bn; ++j){
d[i][j]=min(d[i][j-1],d[i-1][j]+abs(a[i]-b[j]));
}
}
printf("%lld",d[n][bn]);
return 0;
}
Codeforces713C Sonya and Problem Wihtout a Legend(DP)的更多相关文章
- Codeforces 713C Sonya and Problem Wihtout a Legend(DP)
题目链接 Sonya and Problem Wihtout a Legend 题意 给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...
- 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 ...
- codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)
题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...
- Codeforces 713C Sonya and Problem Wihtout a Legend(单调DP)
[题目链接] http://codeforces.com/problemset/problem/713/C [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上 ...
- CodeForces 714E Sonya and Problem Wihtout a Legend(单调数列和DP的小研究)
题意:给你n个数字,每个数字可以加减任何数字,付出变化差值的代价,求最后整个序列是严格单调递增的最小的代价. 首先我们要将这个题目进行转化,因为严格单调下是无法用下面这个dp的方法的,因此我们转化成非 ...
- 【CF713C】Sonya and Problem Wihtout a Legend(离散化,DP)
题意:给你一个数列,对于每个数字你都可以++或者−− 然后花费就是你修改后和原数字的差值,然后问你修改成一个严格递增的,最小花费 思路:很久以前做过一道一模一样的 严格递增很难处理,就转化为非严格递增 ...
- 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 ...
- 【CodeForces】713 C. Sonya and Problem Wihtout a Legend
[题目]C. Sonya and Problem Wihtout a Legend [题意]给定n个数字,每次操作可以对一个数字±1,求最少操作次数使数列递增.n<=10^5. [算法]动态规划 ...
- Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]
E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...
随机推荐
- ARM处理器解析
按图分析: ARM处理器有七种工作模式,为的是形成不同的使用级别,以防造成对系统的破坏.不同模式可以访问的寄存器不同,可以运行的指令不同. (1)user(10000):普通应用程序运行的模式(应用程 ...
- 借One-Class-SVM回顾SMO在SVM中的数学推导--记录毕业论文5
上篇记录了一些决策树算法,这篇是借OC-SVM填回SMO在SVM中的数学推导这个坑. 参考文献: http://research.microsoft.com/pubs/69644/tr-98-14.p ...
- Solve
/// <summary> /// Solves this instance. /// </summary> /// <returns>IFeatureClass. ...
- cnblog中添加数学公式支持
在博客中使用数学公式,是一件相对麻烦的事儿,大量的截图和插入图片不仅耗费极大的精力,而且影响写作体验. 虽然对于公式显示已经有多种解决办法,但大多数需要安装插件.而MathML这一雄心勃勃的网页数学语 ...
- 学习MySQL之数据库操作(一)
所有代码,均为自学时用到的测试与注释,知识细节或知识点不会面面俱到,亦不会有任何讲解,只做为自己学习复习用. ##数据库操作 ##创建数据库 myTest ,并将数据库字符集设为GBK CREATE ...
- 关于学习JavaScript 的 高三编程 一些心得(三)
最近在学习高三的 过程中,遇到的了一些 难以理解的问题, 在看到第五章之前都是 OK 的.但是到了 引用类型的时候就有点蒙了. 首先我们看下,引用类型的 解释:[引用类型的值(对象)是引用类型的一个 ...
- 【WIN10】绑定x:Bind
在WP8.WP8中,我们知道有一个绑定{Binding},而在Win10中,新增了一个绑定{x:Bind} x:Bind :为编译时绑定 ,内存.内存相对于传统绑定都有优化 特性: 1.为强类型 ...
- Windows下,MySQL root用户忘记密码解决方案
同时打开2个命令行窗口,并按如下操作: <1>.在第一个“命令行窗口”输入: cd D:\Program Files\MySQL\MySQL Server 5.5\bin net sto ...
- 利用iis虚拟目录实现文件服务器功能(分布式存储)
要求说明: 通过网站上传文件保存到统一的文件服务器上. 服务器说明: 1.文件服务器以下称为FilesServer,IP地址为:192.168.1.213 2.Web服务器为以下称为WebServer ...
- DB2表的重组
DB2在存储大数据的时候,遇到一个问题,将数据导入表中保存不了,最后是重组后才解决. 下面是从IBM官网上搜集的资料: 官网地址:http://publib.boulder.ibm.com/infoc ...