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

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

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

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

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

  5. Codeforces 713C Sonya and Problem Wihtout a Legend(DP)

    题目链接   Sonya and Problem Wihtout a Legend 题意  给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...

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

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

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

  8. Codeforces 713C Sonya and Problem Wihtout a Legend

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

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

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

随机推荐

  1. request获取url链接和参数

            //Returns the part of this request's URL from the protocol name up to the query string in th ...

  2. ns3 802.11b PHY model

    I use the ubuntu and do not install the chinse input. The Code: c file requires gnu gsl library, it ...

  3. 2019牛客多校训练第四场K.number(思维)

    题目传送门 题意: 输入一个只包含数字的字符串,求出是300的倍数的子串的个数(不同位置的0.00.000等都算,并考虑前导零的情况). sample input: 600 1230003210132 ...

  4. react-navigation报错

    用react-navigation配置路由时,出现如下报错或白屏. 我的代码原来是 import {AppRegistry} from 'react-native'; import App from ...

  5. MacOS VSCode 安装 GO 插件失败问题解决

    0x00 问题重现 Installing golang.org/x/tools/cmd/guru FAILED Installing golang.org/x/tools/cmd/gorename F ...

  6. Mybatis中使用PageHelper插件进行分页

    分页的场景比较常见,下面主要介绍一下使用PageHelper插件进行分页操作: 一.概述: PageHelper支持对mybatis进行分页操作,项目在github地址: https://github ...

  7. 自定义Dialog---实现优美对话框

    PS:自定义dialog,一些系统的dialog已经不能满足开发人员的需求了,所以,我们需要自定义一个属于并且适合自己项目的对话框,无论是颜色还是功能需求上都是和自己的项目紧密相关的,一些系统的对话框 ...

  8. django前后端分离部署

    部署静态文件: 静态文件有两种方式1:通过django路由访问2:通过nginx直接访问 方式1: 需要在根目录的URL文件中增加,作为入口 url(r'^$', TemplateView.as_vi ...

  9. [HNOI2009]双递增序列(动态规划,序列dp)

    感觉这个题还蛮难想的. 首先状态特别难想.设\(dp[i][j]\)表示前i个数,2序列的长度为j的情况下,2序列的最后一个数的最小值. 其中1序列为上一个数所在的序列,2序列为另外一个序列. 这样设 ...

  10. jmeter+Fiddler:通过Fiddler抓包生成jmeter脚本

    Fiddler是目前最常用的抓包工具之一,它作为客户端和服务器端之间的代理,记录客户端和服务器之间的所有请求(http/https),可以针对特定的请求过滤,分析请求和响应的数据.设置断点.调试.修改 ...