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 ...
随机推荐
- HTML5拖放(drag和drog)作品
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- java 一个对象多少大,占用多少内存
1.instrumentation这种方法还是靠谱的 一个对象占用多少字节? 2.sizeof库 <!-- https://mvnrepository.com/artifact/com.carr ...
- 请大神看看10.10Beta1的AppleRTC怎么破?原版会导致BIOS重置!
看起来 跟10.9一样 sudo perl -pi -e 's|\x75\x2e\x0f\xb6|\xeb\x2e\x0f\xb6|' /System/Library/Extensions/Apple ...
- noip_最后一遍_1-数学部分
它就是要来了 noip数论一般会以三种形式呈现 注 码风可能有些毒 (有人说我压行qwq) 大概保持标准三十五行左右 为什么是三十五行呢 因为我喜欢这个数字 我喜欢三十五而已(足球球衣也会用这个号哒) ...
- (48)zabbix报警媒介:自定义脚本Custom alertscripts
自定义脚本媒介.zabbix会将信息传递给脚本,接下来你在脚本里面随意处理,一共会传递三个参数,按顺序接受也就是$1,$2,$3了,为了方便记忆,一般分别给他们赋值到To\Subject\body 配 ...
- 记我的小网站发现的Bug之一 —— 某用户签到了两次
1.故事背景 今天上午我忙完手中的事情之后突然想起来我还没签到,于是赶紧打开签到页面,刚点击了签到按钮,提示"签到成功,获得25阅读额度!",正准备退出浏览器,忽然发现签到列表有异 ...
- 三、Pandas速查手册中文版
本文翻译自文章:Pandas Cheat Sheet - Python for Data Science,同时添加了部分注解. 对于数据科学家,无论是数据分析还是数据挖掘来说,Pandas是一个非常重 ...
- shell-code-1
#!/bin/bash # online test tool: http://www.shucunwang.com/RunCode/shell/ name="pxy"#Attent ...
- 【转】js里的时间函数集
$(function(){ var mydate = new Date(); var t=mydate.toLocaleString(); $("#time").text(t); ...
- luogu3755 [CQOI2017]老C的任务
扫描线水题. #include <algorithm> #include <iostream> #include <cstdio> using namespace ...