Codeforces 713 C Sonya and Problem Wihtout a Legend
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 \le n \le 3000)\) — the length of the array.
Next line contains \(n\) integer \(a_{i}(1 \le a_{i} \le 10^{9})\).
Output
Print the minimum number of operation required to make the array strictly increasing.
Sample Input
7
2 1 5 11 5 9 11
Sample Output
9
与BZOJ1049 数字序列类似,所有我想到了\(O(N^{3})\)做法,果断TLE。标解懂了些,做法太神了,什么维护中位数,但就是不知道转移怎么会没有后效性。
此处介绍另一种做法。首先也是将单调上升变为单调不降(见BZOJ1049 数字序列)。\(f_{i,j}\)表示前\(i\)个数,最大为\(j\)的合法序列最小代价。转移方程
\]
当然\(A\)值域太大,我们可以离散化。代码如下:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef long long ll;
#define inf (1LL<<50)
#define maxn (3010)
int N,cnt; ll f[maxn][maxn],ans = inf,A[maxn],B[maxn];
int main()
{
freopen("E.in","r",stdin);
freopen("E.out","w",stdout);
scanf("%d",&N);
for (int i = 1;i <= N;++i) scanf("%I64d",A+i),A[i] -= i;
memcpy(B,A,sizeof(B)); B[N+1] = -inf,B[N+2] = inf;
sort(B+1,B+N+3); cnt = unique(B+1,B+N+3)-B-1;
A[0] = -inf; A[N+1] = inf;
memset(f,0x7,sizeof(f)); f[0][1] = 0;
for (int i = 1;i <= N+1;++i)
{
ll tmp = inf;
for (int j = 1;j <= cnt;++j)
tmp = min(tmp,f[i-1][j]),f[i][j] = tmp+abs(A[i]-B[j]);
}
for (int i = 1;i <= cnt;++i) ans = min(ans,f[N+1][i]);
printf("%I64d",ans);
fclose(stdin); fclose(stdout);
return 0;
}
Codeforces 713 C Sonya and Problem Wihtout a Legend的更多相关文章
- 【CodeForces】713 C. Sonya and Problem Wihtout a Legend
[题目]C. Sonya and Problem Wihtout a Legend [题意]给定n个数字,每次操作可以对一个数字±1,求最少操作次数使数列递增.n<=10^5. [算法]动态规划 ...
- 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 ...
- 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 Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend 贪心
C. Sonya and Problem Wihtout a Legend 题目连接: http://codeforces.com/contest/713/problem/C Description ...
- 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 ...
- Codeforces Round #371 (Div. 1) C - Sonya and Problem Wihtout a Legend
C - Sonya and Problem Wihtout a Legend 思路:感觉没有做过这种套路题完全不会啊.. 把严格单调递增转换成非严格单调递增,所有可能出现的数字就变成了原数组出现过的数 ...
- 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 ...
- Codeforces 713C Sonya and Problem Wihtout a Legend(DP)
题目链接 Sonya and Problem Wihtout a Legend 题意 给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...
- 把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend
//把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend //dp[i][j]:把第i个数转成第j小的数,最小花费 //此题与po ...
随机推荐
- group by是什么意思 mysql中
mysql语法中group by是什么意思? 在百度中搜索半天,最后找到一篇解释比较好的(不是博文,是百度知道,很郁闷那么多网友怎么就没人解释的清楚),链接如下: http://zhidao.baid ...
- 去掉cajviewer 右上角的“中国知网数字出版物超市
cajviewer软件是一款可以提取pdf字码的软件(即使pdf是扫描版的) 下面是转的一个博文可以去除软件右上角图标的方法: 去掉cajviewer 7.1.2右上角的“中国知网数字出版物超市” 1 ...
- hadoop错误org.apache.hadoop.mapred.TaskAttemptListenerImpl Progress of TaskAttempt
错误: org.apache.hadoop.mapred.TaskAttemptListenerImpl: Progress of TaskAttempt 原因: 错误很明显,磁盘空间不足,但郁闷的是 ...
- Android开发之ViewPager实现轮播图(轮播广告)效果的自定义View
最近开发中需要做一个类似京东首页那样的广告轮播效果,于是采用ViewPager自己自定义了一个轮播图效果的View. 主要原理就是利用定时任务器定时切换ViewPager的页面. 效果图如下: 主页面 ...
- mRemote配置
配置完mRemote后 备份C:\Users\Administrator\AppData\Local\Felix_Deimel\mRemote\confCons.xml文件 覆盖到其他电脑可以直接使用
- Js的History对象
History回顾 window.history表示window对象的历史记录 window.history的简单回顾 历史记录中前进/后退,移动到指定历史记录点 window.history.bac ...
- DataDictionaryTool 一款生成数据库字典工具支持mysql和oracle
因为常常查看mysql数据结构,频繁操作.很不爽,于是想把数据表制作成数据字典,于是网上搜的一款工具 DataDictionaryTool ,最终制作成功,分享给大家! 1,此工具需要安装jre ,简 ...
- javascript技巧字典【转藏】
2015-12-31 js判断undefined类型 if (reValue== undefined){ alert("undefined"); } 发现判断不出来, ...
- [DEncrypt] DESEncrypt--加密/解密帮助类 (转载)
点击下载 DESEncrypt.zip 这个类是关于加密,解密的操作,文件的一些高级操作1.DESEncrypt加密2.DESEncrypt解密看下面代码吧 /// <summary> / ...
- The Better Way to Debug Your JavaScript Program
Debugging JS program is not as easy as we do in Visual Studio or any other IDE. I usually us "a ...