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 ...
随机推荐
- python 基础(四) 函数
函数 一.什么是函数? 函数是可以实现一些特定功能的 小方法 或者是小程序 优点: 提高 了代码的后期维护 增加了代码的重复使用率 减少了代码量 提高了代码可读性 二.函数的定义 使用 def关键+函 ...
- Codeigniter CI 框架的一些优化思考
前段时间使用CI做了两个小项目,对CI的流程和设计理念也有了一些新的认识.CI架构的一些基本优化这里就不做介绍了,如搬离system 文件夹等. 最近有一个稍微大一点的系统,也准备拿CI来做.设计时遇 ...
- hdu4553约会安排(线段树区间合并)
链接 poj3667的加强版 当时的题解 这里只不过对于女神需要另开算,DS的占用的时间不加在女神身上,女神的时间都要加,清空的时候也都要算. #include <iostream> #i ...
- docker命令自动安装
docker命令自动安装 操作 操作就是执行两句脚本 curl -fsSL get.docker.com -o get-docker.sh 这句命令会在当前文件夹下下载一个get-docker.sh的 ...
- WPF 控件树
WPF控件树按照基类进行分类,记录下来便于编写自定义控件时查阅 RangeBase范围控件 Thumb拖到控件 TextBoxBase文本控件 ItemControl组控件 ContrentContr ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第七天(非原创)
文章大纲 一.课程介绍二.Redis基础实战三.Redis之高可用.集群.云平台搭建实战四.淘淘商城Jedis整合spring五.项目源码与资料下载六.参考文章 一.课程介绍 一共14天课程(1) ...
- slf4j介绍以及与Log4j、Log4j2、LogBack整合方法
翻了一下百度和官网.这么介绍slf4j. slf4j 全称 Simple Logging Facade for Java,是日志框架的一种抽象,那么也就是说 slf4j 是不能单独使用的必须要有其他实 ...
- H5网站加载速度优化总结
1. 在代码文件结构 尽量优化的同时,能力再强已经到极限了,但你服务器辣鸡,搭配不当,你代码优化上天 也是徒劳啊. 2.你不怎么优化, 服务器 各种技术配置到位的话, now你也看到了,我一个系统首页 ...
- Java基础之面向对象
面向对象 1.面向对象思想: (1)概述:面向对象是相对于面向过程而言的,面向过程强调的是功能,面向对象强调的是将功能封装进对象,强调具备功能的对象: (2)思想特点: ...
- Linux下环境搭建(二)——jenkins+gitlab配置
配置jenkins+gitlab环境,需要依托java环境,在之前的博文中,已配置好java环境,所以可以直接搭建这两个工具的环境即可. jenkins配置 jenkins的配置方法,在之前windo ...