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 ...
随机推荐
- Makefile详解--隐含规则
Makefile详解--隐含规则(转) Makefile系列文章,这里有个前辈连续洗了一个系列来介绍,共有26篇博客文章. http://www.cppblog.com/ivenher/archive ...
- 传输层:TCP UDP SCTP
总图 虽然协议族被称为“TCP/IP”,但除了TCP和IP这两个主要协议外,还有许多其他成员.图2-1展示了这些协议的概况. 图2-1中同时展示了IPV4和IPV6.从右向左看该图,最右边的5个网络应 ...
- LabVIEW系列——自定义错误
1.自定义错误代码文本文件在labview中三处: a).E:\Program Files\National Instruments\LabVIEW 8.6\project\errors ...
- Cocos2d-x中获取设备语言的方法
1.cocos2dx获取设备语言的方法:CCApplication::sharedApplication()->getCurrentLanguage() 2.cocos2dx 2.1.4支持识别 ...
- 分布式服务框架 Zookeeper -- 管理分布式环境中的数据--转载
原文:http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/ Zookeeper 分布式服务框架是 Apache Hadoop ...
- ArcGIS 设置地图显示范围大小
Arcmap的FullExtent默认是地图加载的时候的extent.其实这个fullExtent是可以设置的. 打开ArcMap,选择左边图例的Layers ,右键点击,选择“Properties. ...
- HTML5 <a>标签download 属性
一.简单实例 <a href="../images/1.jpg" download="下载图片.jpg"> 点击按钮下载 </a> 二. ...
- C# DateTime显示时间格式的使用
代码DateTime.ToString() Patterns All the patterns: 0 MM/dd/yyyy 08/22/2006 1 dddd, dd MMMM yyyy Tuesda ...
- 完全步卸载oracle11g步骤
完全 步 卸载oracle11g骤: . 开始->设置->控制面板->管理工具->服务 停止所有Oracle服务. . 开始->程序->Oracle - OraH ...
- C# 静态类和非静态类的区别
静态类和非静态类的区别 静态类: static 关键字 调用 类名.方法 在静态方法中只能访问静态成员 在静态类中只能有静态成员 在非静态类中 即可有非静态成员,也可以有静态成员 在静态 ...