Codeforces 713C Sonya and Problem Wihtout a Legend(DP)
题目链接 Sonya and Problem Wihtout a Legend
题意 给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$。
求把原序列变成严格递增子序列的所需最小花费。
考虑$DP$。
首先比较常见的套路就是把每个$a[i]$减去$i$,然后把这个新的序列升序排序,记为$b[i]$。
这里有个结论:最后操作完成之后的每个数都是$b[i]$中的某个数。
然后就可以$DP$了,令$f[i][j]$为前$i$个数操作之后每个数都小于等于$b[j]$的最小花费。
则有\begin{equation*}f[i][1] = \sum_{i=1}^nabs(a[i] - b[1])\end{equation*}
$f[i][j] = min(f[i][j - 1], f[i - 1][j] + abs(a[i] - b[j]))$
时间复杂度$O(nlogn + n^{2})$
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define MP make_pair
#define fi first
#define se second typedef long long LL; const int N = 3010; int n;
LL a[N], b[N], f[N][N];
LL ret, ans; int main(){ scanf("%d", &n);
rep(i, 1, n) scanf("%lld", a + i), a[i] -= i, b[i] = a[i];
sort(b + 1, b + n + 1); rep(i, 0, n + 1) rep(j, 0, n + 1) f[i][j] = 1e18;
rep(i, 0, n + 1) f[0][i] = 0; rep(i, 1, n) f[i][1] = f[i - 1][1] + abs(a[i] - b[1]); rep(i, 1, n){
rep(j, 1, n){
f[i][j] = min(f[i][j - 1], f[i - 1][j] + abs(a[i] - b[j]));
}
} printf("%lld\n", f[n][n]);
return 0;
}
Codeforces 713C Sonya and Problem Wihtout a Legend(DP)的更多相关文章
- 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 [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上 ...
- 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
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,问最少要几次操作使得序列变为严格单调递增序列. 题解:首先考虑非严格递增序列,则每个数字最终变成的数字一定是该数组中的某个数.那 ...
- 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)
题目链接: C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 ...
随机推荐
- PAT (Basic Level) Practise (中文)-1033. 旧键盘打字(20)
PAT (Basic Level) Practise (中文)-1033. 旧键盘打字(20) http://www.patest.cn/contests/pat-b-practise/1033 旧 ...
- PAT (Basic Level) Practise (中文)-1034. 有理数四则运算(20)
PAT (Basic Level) Practise (中文)-1034. 有理数四则运算(20) http://www.patest.cn/contests/pat-b-practise/1034 ...
- Bootstrap历练实例:响应式导航栏
响应式的导航栏 为了给导航栏添加响应式特性,您要折叠的内容必须包裹在带有 classes .collapse..navbar-collapse 的 <div> 中.折叠起来的导航栏实际上是 ...
- tkinter学习-菜单与画布
阅读目录 Menu 菜单控件 Menubutton 菜单按钮控件 OptionMenu 选项菜单 Canvas 画布控件 Menu: 说明:菜单控件,显示菜单栏,下拉菜单和弹出菜单 属性:创建一个顶级 ...
- Python模块目录
阅读目录 模块 模块语法 常用模块 collections模块 time模块 random模块 os模块 sys模块 序列化模块 shelve模块 pickle模块 json模块 configpars ...
- I2C驱动框架(三)
参考:I2C子系统之platform_device初始化——smdk2440_machine_init() I2C驱动框架还应用了另一种总线-设备-驱动模型,平台设备总线platform_bus_ty ...
- DFS:POJ3620-Avoid The Lakes(求最基本的联通块)
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Description Farmer John's farm was flooded i ...
- Hi3519V101 Uboot和Kernel编译
前面已经搭建好了Ubuntu下的海思开发环境,现在对编译Uboot和Kernel的过程做一个简单的记录.参考文档<Hi3519V101 U-boot 移植应用开发指南.pdf>和<H ...
- iphone数据库(sqlite3)的用法操作oc,xcode
首先你在用之前要在项目中加入libsqlite3.dylib 1.定义模型 [cpp] #import <Foundation/Foundation.h> #import " ...
- vmware的vmdk格式虚拟机转换为qcow2格式
今天应其他部门的需求,把一个笔记本里的vmware的window2008的虚拟机转换为qcow2格式,在公司服务器上运行. 第一次做,用了许多时间 首先: 虚拟机的镜像分为多个vmdk文件,所以我 ...