题目链接   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)的更多相关文章

  1. 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 ...

  2. codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)

    题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...

  3. Codeforces 713C Sonya and Problem Wihtout a Legend(单调DP)

    [题目链接] http://codeforces.com/problemset/problem/713/C [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上 ...

  4. Codeforces713C Sonya and Problem Wihtout a Legend(DP)

    题目 Source http://codeforces.com/problemset/problem/713/C Description Sonya was unable to think of a ...

  5. 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 ...

  6. Codeforces 713C Sonya and Problem Wihtout a Legend

    题意:给一个序列,可以进行若干次操作,每次操作选择一个数让它+1或-1,问最少要几次操作使得序列变为严格单调递增序列. 题解:首先考虑非严格递增序列,则每个数字最终变成的数字一定是该数组中的某个数.那 ...

  7. CodeForces 714E Sonya and Problem Wihtout a Legend(单调数列和DP的小研究)

    题意:给你n个数字,每个数字可以加减任何数字,付出变化差值的代价,求最后整个序列是严格单调递增的最小的代价. 首先我们要将这个题目进行转化,因为严格单调下是无法用下面这个dp的方法的,因此我们转化成非 ...

  8. 【CF713C】Sonya and Problem Wihtout a Legend(离散化,DP)

    题意:给你一个数列,对于每个数字你都可以++或者−− 然后花费就是你修改后和原数字的差值,然后问你修改成一个严格递增的,最小花费 思路:很久以前做过一道一模一样的 严格递增很难处理,就转化为非严格递增 ...

  9. 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 ...

随机推荐

  1. C++ static关键字

    一.面向过程中的static 1.修饰全局变量(静态全局变量) (1)静态全局变量在全局数据区分配内存: (2)未经初始化的静态全局变量会被程序自动初始化为0: (3)静态全局变量在申明它的整个文件是 ...

  2. vue 点击倒计时 ajax 封装

    方法:function(){ var that = this; if (that.time == 0) { that.disabled = false; that.text ="点击获取&q ...

  3. 分享一个C++与Python开发的中小型通用游戏服务端框架(跨平台,开源,适合MMORPG游戏)

    在开发一款游戏项目时,在立项时我们往往会考虑或者纠结很多,比如: 1,对于开发来说:服务端和客户端应该选择什么语言?用什么协议通信才更效率?协议后期如何维护?Socket是用长连接还是短连接?TCP还 ...

  4. Java--对象和引用 转载

    这个讲的很详细,看了以后终于懂了.特转载供以后学习使用. 原文链接:http://www.cnblogs.com/dolphin0520/p/3592498.html

  5. [LUOGU] P4767 [IOI2000]邮局

    https://www.luogu.org/problemnew/show/P4767 四边形不等式好题! 可以设f[i][j]表示前i个村庄,建了j个邮局的最小代价. 转移:f[i][j]=min{ ...

  6. vim的卸载以及环境的配置小记

    一.背景 由于之前配置错误,导致我的YouCompleteMe这个插件不能用了,一直提示: ERROR:Required vim compiled with +python. YouCompleteM ...

  7. mysql:10道mysql查询语句面试题

    表结构 学生表student(id,name) 课程表course(id,name) 学生课程表student_course(sid,cid,score) 创建表的sql代码 ```sql creat ...

  8. 译文 编写一个loader

    https://doc.webpack-china.org/contribute/writing-a-loader loader是一个导出了函数的node模块,当资源须要被这个loader所转换的时候 ...

  9. hashlib模块,hmac模块

    6.11自我总结 1.hashlib模块(文件传输中将传输内容用指定算法进行处理) hash是一种算法(Python3.版本里使用hashlib模块代替了md5模块和sha模块,主要提供 SHA1.S ...

  10. shell相关指令介绍$*和$#以及$?和if [[ ! -z $1 ]]

    $#,脚本运行时后跟的参数个数 #! /bin/bash case "$#" in 0) printf "Enter a number: " read n=$R ...