codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)
题目链接:http://codeforces.com/contest/713/problem/C
题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i+1]-(i+1),处理一下。
首先是最基础的dp[i][j]前i位最大值为j的最小值为多少。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define inf 0X3f3f3f3f
using namespace std;
const int M = 3e3 + 10;
typedef long long ll;
ll dp[M][M] , a[M] , b[M];
int main() {
int n;
scanf("%d" , &n);
memset(b , 0 , sizeof(b));
for(int i = 1 ; i <= n ; i++) scanf("%lld" , &a[i]) , b[i] = a[i] = a[i] - i;
memset(dp , inf , sizeof(dp));
int cnt = 0;
b[0] = -1000000000000000;
sort(b + 1 , b + 1 + n);
for(int i = 1 ; i <= n ; i++) {
if(b[i] != b[i - 1]) b[++cnt] = b[i];
}
for(int i = 1 ; i <= cnt ; i++) dp[0][i] = 0;
for(int i = 1 ; i <= n ; i++) {
ll tmp = dp[i - 1][1];
for(int j = 1 ; j <= cnt ; j++) {
tmp = min(tmp , dp[i - 1][j]);
dp[i][j] = tmp + abs(a[i] - b[j]);
}
}
ll ans = 1000000000000000;
for(int i = 1 ; i <= cnt ; i++) {
ans = min(ans , dp[n][i]);
}
printf("%lld\n" , ans);
return 0;
}
然后是滚动优化注意那个dp的转移方式只和上一个状态有关系所以可以用滚动优化dp[2][M],这样优化了空间
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
const int M = 3e3 + 10;
int a[M] , b[M];
ll dp[2][M];
int main() {
int n , m;
scanf("%d" , &n);
for(int i = 1 ; i <= n ; i++) scanf("%d" , &a[i]) , a[i] -= i , b[i] = a[i];
m = 0;
sort(b + 1 , b + 1 + n);
b[0] = -1e9 - 10;
for(int i = 1 ; i <= n ; i++) {
if(b[i] != b[i - 1]) b[++m] = b[i];
}
memset(dp , inf , sizeof(dp));
for(int i = 1 ; i <= n ; i++) dp[0][i] = 0;
for(int i = 1 ; i <= n ; i++) {
ll tmp = dp[(i - 1) % 2][1];
for(int j = 1 ; j <= m ; j++) {
tmp = min(tmp , dp[(i - 1) % 2][j]);
dp[i % 2][j] = tmp + abs(a[i] - b[j]);
}
}
ll Min = 1e15 + 10;
for(int i = 1 ; i <= m ; i++) Min = min(Min , dp[n % 2][i]);
printf("%lld\n" , Min);
return 0;
}
最后是最强的优化复杂度可以到达O(nlogn),就是用优先队列来优化,其实这个具体去证明也不太好证明但是理解还是好理解的具体画一下图意会一下
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
priority_queue<int>q;
int main() {
int n;
cin >> n;
long long ans = 0;
for(int i = 1 ; i <= n ; i++) {
int x;
scanf("%d" , &x);
x -= i;
q.push(x);
if(q.top() > x) {
int t = q.top();
q.pop();
ans += t - x;
q.push(x);
}
}
cout << ans << endl;
return 0;
}
codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)的更多相关文章
- 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 Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]
E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...
- 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 ...
- codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)(将一个数组变成严格单增数组的最少步骤)
E. 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(DP)
题目链接 Sonya and Problem Wihtout a Legend 题意 给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...
- Codeforces 713C Sonya and Problem Wihtout a Legend(单调DP)
[题目链接] http://codeforces.com/problemset/problem/713/C [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上 ...
- 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 713C Sonya and Problem Wihtout a Legend
题意:给一个序列,可以进行若干次操作,每次操作选择一个数让它+1或-1,问最少要几次操作使得序列变为严格单调递增序列. 题解:首先考虑非严格递增序列,则每个数字最终变成的数字一定是该数组中的某个数.那 ...
- CodeForces 714E Sonya and Problem Wihtout a Legend(单调数列和DP的小研究)
题意:给你n个数字,每个数字可以加减任何数字,付出变化差值的代价,求最后整个序列是严格单调递增的最小的代价. 首先我们要将这个题目进行转化,因为严格单调下是无法用下面这个dp的方法的,因此我们转化成非 ...
随机推荐
- MySQL Schema与数据类型优化
Schema与数据类型优化 选择优化的数据类型 1.更小的通常更好 更小的数据类型通常更快,因为它们占用更少的磁盘,内存和CPU缓存 2.简单就好 简单数据类型的操作通常需要更少的CPU周期.例如:整 ...
- Python 与数据库交互
安装:pip3 install pymysql 引入模块在python3里:from pymysql import * 使用步骤:1.创建Connection对象,用于建立与数据库的连接,创建对象调用 ...
- 基于Spring注解的上下文初始化过程源码解析(一)
最近工作之余有时间和精力,加上平时对源码比较感兴趣,就开始啃起了Spring源码.为加深印象写了这篇博客,如有错误,望各位大佬不吝指正. 我看的是Spring5的源码,从同性社区download下来后 ...
- xpath定位的一些方法
- Consul的反熵
熵 熵是衡量某个体系中事物混乱程度的一个指标,是从热力学第二定律借鉴过来的. 熵增原理 孤立系统的熵永不自动减少,熵在可逆过程中不变,在不可逆过程中增加.熵增加原理是热力学第二定律的又一种表述,它更为 ...
- python(自用手册)三
第三章 基础 3.1编码初识 ascii 256字母没有中文 一个字节 8位 gbk 中国 中文2字节 16位 英文1字节8位 unicode 万国码 前期 2字节 8位 后期变成4个字节 32位 u ...
- linux系统磁盘满了,怎么解决?
1.使用命令:df -lk 或 df -hl 发现果然有个磁盘已满 2.使用命令:du --max-depth=1 -h 查找大文件,发现/home文件夹下有17G的东西,因为我的apache是装在 ...
- C#之项目常用方法之静态扩展
一般项目中我们经常用到数据Json的序列化与反序列化,为了方便在需要的地方快速使用,一般建议都封装为静态扩展方法,在需要的地方可直接使用. 而目前C#项目中序列化一般都是用的 Newtonsoft.J ...
- 天眼查sign 算法破解
天眼查sign 算法破解 最近真的在sign算法破解上一去不复返 前几天看过了企查查的sign破解 今天再看看天眼查的sign算法破解,说的好(zhuang)点(bi)就是破解,不好的就是这是很简单的 ...
- 亲,麻烦给个五星好评!—RatingBar
引言 上一篇的CheckBox已经让大家越来越接近实战演练了,本章我们继续分享干货给大家,今天介绍一个实用的UI控件RatingBar(星级评分条),对于使用过电商APP(某东,某宝等)的小伙伴们来说 ...