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的方法的,因此我们转化成非 ...
随机推荐
- SCI论文的时态
如果有的杂志对时态有要求,则以下所述都没有用了. 有些杂志也会专门有些比较“特别”的要求,比如Cell,要求Abstract全部使用一般现在时. 英语谓语动词时态共有16种,在英文科技论文中用得较为频 ...
- 爬虫环境搭建及 scrapy 启动
创建虚拟环境 C:\Users\Toling>mkvirtualenv article 这个是普通的创建虚拟环境,但是实际开发中可能会使用python2或python3所以我们需要指定开发的环境 ...
- HiveQL DDL 常用QL示例资料
hive-version2.1.1 DDL操作 Create/Drop/Alter/Use Database 创建数据库 //官方指导 CREATE (DATABASE|SCHEMA) [IF NOT ...
- 关于input[type='checkbox']全选的问题
今天在做一个全选功能的时候,发现了一个问题,就是如果我在选择全选之前,我就已经选择了一个input,然后我再去选择全选并且以后再取消全选的时候,这个我之前选择的input始终处于选择状态,但是他的ch ...
- ASP.NET Core MVC 之控制器(Controller)
操作(action)和操作结果(action result)是 ASP.NET MVC 构建应用程序的一个基础部分. 在 ASP.NET MVC 中,控制器用于定义和聚合一组操作.操作是控制器中处理传 ...
- pythonday05数据类型(三)
---恢复内容开始--- 今日内容 1.字典 2.强制转换 3.习题讲解 1.字典 帮助用户去表示一个事物的信息(事物是有多个属性). info = {"name":'刘伟达',' ...
- C语言之左移和右移运算符
C语言中的左移和右移运算符移位后的结果老是忘记,最近在刷有关位操作的题目,正好整理下: 1. 左移运算符(<<) 左移运算符是用来将一个数的各二进制位左移若干位,移动的位数由右操作数指 ...
- websocket初体验(小程序)
之前上个公司做过一个二维码付款功能,涉及到websocket功能,直接上代码 小程序onShow方法下加载: /** 页面的初始数据 **/ data: { code: "", o ...
- 对平底锅和垃圾的O奖论文的整理和学习[2](2018-02-08发布于知乎)
其实这篇论文看了一段时间,愣是没看出来这个模型怎么建立的.虽然看不懂,但是有一些部分还是很喜欢. 首先是摘要: 摘要分为八段 第一段:背景引入,太空垃圾的问题日益严重. 第二段:本文工作,包括基本的i ...
- java后端_百度一面
参考: https://www.nowcoder.com/discuss/215891?type=2&order=0&pos=10&page=1 1.会啥框架.不会. 2.锁的 ...