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 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.
input
7
2 1 5 11 5 9 11
output
9
input
5
5 4 3 2 1
output
12
hint
In the first sample, the array is going to look as follows:
2 3 5 6 7 9 11
|2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9
And for the second sample:
1 2 3 4 5
|5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12
题解
考虑求非严格递增序列的解法,最优方案为造成递减的数字转为已经存在的数字(刚好转成不递减即可)。
如:
1 5 2 3 6 这里 5变成2最优
1 2 3 10 9 这里最优方案为 9变成10
可以设\(f[i][j]\)表示前i个数不超过j的最小代价
\]
根据我们之前的推论,那么可以把j(1~max(a))转为为已经存在的数字,把a数组排序去重之后得到b数组
原方程优化为
\]
已知最坏情况下是将每个数都转成一个相同的数,而这个数在原数组出现过,那么n个数肯定不会超过max(a)
答案为f[n][tot],总的时间复杂度为\(O(n^2)\)
至于原题要求严格的递增序列,可以根据
\(ai<a[j] \Leftrightarrow ai \leq aj-1\)
让每个ai-i即可转为一个新的数组,这个数组变成非严格递增的代价就是原数组变成严格递增的代价。
import java.io.*;
import java.util.*;
public class Main {
static final int N=(int)3000+5;
static final long inf=(long)3e12;
long []a=new long[N];
long []b=new long[N];
long [][]f=new long[N][N];
public void solve() {
Scanner in=new Scanner(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
int n=in.nextInt();
for(int i=1;i<=n;i++){
a[i]=in.nextInt()-i;
b[i]=a[i];
}
Arrays.sort(b,1,n+1);
int tot=1;
for(int i=2;i<=n;i++) {
if(b[i]!=b[tot]) b[++tot]=b[i];
}
for(int i=0;i<=n;i++) {
Arrays.fill(f[i], inf);
f[0][i]=0;
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=tot;j++) {
f[i][j]=Math.min(f[i][j-1], f[i-1][j]+Math.abs(a[i]-b[j]));
}
}
out.println(f[n][tot]);
out.flush();
in.close();out.close();
}
public static void main(String[] args) {
(new Main()).solve();
}
}
存在一种贪心做法,记录之前出现的大数,如果这个大数大于当前数,就把他变成当前数。
对于这个贪心解法,暂时还没想到证明,先记录一下。
import java.io.*;
import java.util.*;
public class Main {
static final int N=(int)3000+5;
static final int inf=(int)1e9;
public void solve() {
Scanner in=new Scanner(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
PriorityQueue<Integer>que=new PriorityQueue<Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer o1,Integer o2) {
if(o1.equals(o2)) return 0;
return o1.compareTo(o2)>0?-1:1;
}
});
int n=in.nextInt();
long ans=0;int x;
for(int i=1;i<=n;i++) {
x=in.nextInt()-i;
que.offer(x);
if(x<que.peek()) {
ans+=que.poll()-x;
que.offer(x);
}
}
out.println(ans);
out.flush();
in.close();out.close();
}
public static void main(String[] args) {
(new Main()).solve();
}
}
Codeforces C. 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 or 思维)
题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...
- Codeforces713C Sonya and Problem Wihtout a Legend(DP)
题目 Source http://codeforces.com/problemset/problem/713/C Description Sonya was unable to think of a ...
- 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的方法的,因此我们转化成非 ...
- 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
题意:给一个序列,可以进行若干次操作,每次操作选择一个数让它+1或-1,问最少要几次操作使得序列变为严格单调递增序列. 题解:首先考虑非严格递增序列,则每个数字最终变成的数字一定是该数组中的某个数.那 ...
- 【CF713C】Sonya and Problem Wihtout a Legend(离散化,DP)
题意:给你一个数列,对于每个数字你都可以++或者−− 然后花费就是你修改后和原数字的差值,然后问你修改成一个严格递增的,最小花费 思路:很久以前做过一道一模一样的 严格递增很难处理,就转化为非严格递增 ...
- 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 #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A
Description There are literally dozens of snooker competitions held each year, and team Jinotega tri ...
- 模拟ssh、黏包、hashlib模块(MD5)
待补充..... 一.模拟ssh 二.黏包 1.黏包现象 让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd) res=subprocess.Popen(cm ...
- R 语言中 data table 的相关,内存高效的 增量式 data frame
面对的是这样一个问题,不断读入一行一行数据,append到data frame上,如果用dataframe, rbind() ,可以发现数据大的时候效率明显变低. 原因是 每次bind 都是一次重新 ...
- [BZOJ1047][HAOI2007]理想的正方形 二维单调队列
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 我们对每矩阵的一列维护一个大小为$n$的单调队列,队中元素为矩阵中元素.然后扫描每一 ...
- GetDC(),ReleaseDC()
用GetDC()得到的DC, 必须调用ReleaseDC()用CreateDC()创建的DC, 必须调用DeleteDC() 两者是不能混淆的.一种典型的错误代码如下:CDC* pDC = GetDC ...
- as 开启代码混淆和混淆规则
app的builde.gradle的文件下,buildTypes节点添加release节点,minifyEnabled属性表示是否开启混淆,proguardFiles表示混淆依赖的文件,具体开启方法如 ...
- Xmind几个有用的技巧
Xmind是一个很好的思维导图工具,是学习研究总结的好帮手. Xmind功能很丰富,这里只是简要列出几个比较有用的技巧. 1.善用属性 选中一个xmind元素(专业名词叫[主题])后,一般在右下角会出 ...
- HDU 4003 Find Metal Mineral (树形DP,经典)
题意:给定一棵树图,n个节点,有边权,要派k<11个机器人从节点s出发,遍历所有的点,每当1只机器人经过1条边时就会花费该边的边权,边是可重复走的.问遍历完所有点的最小花费? 思路: 非常经典, ...
- UIButton 左对齐 省略号最右边
//左对齐 [_btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; //省略号靠右侧 _btn.ti ...
- 11gR2新特性---Gpnp守护进程
在这篇文章中,我们会对11gR2 新的守护进程(资源名称ora.gpnpd)进行介绍,其中包含的gpnp的功能,启动顺序和基本的诊断方法. gpnp全称为grid plug and play,该组件的 ...