这两个题是一样的,不过数据范围不同。

思路1:

在CF713C中,首先考虑使生成序列单调不下降的情况如何求解。因为单调上升的情况可以通过预处理将a[i]减去i转化成单调不下降的情况。

首先,生成的序列中的每个数一定是原序列中的数。于是可以通过dp+离散化技巧解决。dp[i][j]表示把前i个数变成单调不下降的序列,并且a[i]不超过第j大的数所需要的最小代价。

实现1:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = ;
ll a[N], dp[N][N];
int main()
{
int n;
while (cin >> n)
{
for (int i = ; i <= n; i++) { cin >> a[i]; a[i] -= i; }
vector<ll> v(a + , a + n + );
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int m = v.size();
for (int i = ; i <= n; i++)
{
for (int j = ; j < m; j++)
{
dp[i][j] = dp[i - ][j] + abs(a[i] - v[j]);
if (j) dp[i][j] = min(dp[i][j], dp[i][j - ]);
}
}
cout << dp[n][m - ] << endl;
}
return ;
}

思路2:

n2的复杂度不足以解决hihocoder1942,因此需要进行优化。下面这种数形结合的斜率优化思路参考了

https://codeforces.com/blog/entry/47094?#comment-315161

以下的代码是针对hihocoder1942的实现:

实现2:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = ;
int a[N];
ll solve(int n)
{
ll ans = ;
priority_queue<int> pq;
for (int i = ; i <= n; i++)
{
pq.push(a[i]);
if (a[i] < pq.top())
{
ans += (ll)pq.top() - a[i];
pq.pop();
pq.push(a[i]);
}
}
return ans;
}
int main()
{
int n, x;
while (cin >> n)
{
for (int i = ; i <= n; i++) cin >> a[i];
ll ans = solve(n);
reverse(a + , a + n + );
ans = min(ans, solve(n));
cout << ans << endl;
}
return ;
}

CF713C Sonya and Problem Wihtout a Legend & hihocoder1942 单调序列的更多相关文章

  1. CF713C Sonya and Problem Wihtout a Legend

    考虑我们直接选择一个暴力\(dp\). \(f_{i,j} = min_{k<=j}\ (f_{i - 1,k}) + |a_i - j|\) 我们考虑到我们直接维护在整个数域上\(min(f_ ...

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

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

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

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

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

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

  6. 把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend

    //把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend //dp[i][j]:把第i个数转成第j小的数,最小花费 //此题与po ...

  7. Sonya and Problem Wihtout a Legend

    Sonya and Problem Wihtout a Legend Sonya was unable to think of a story for this problem, so here co ...

  8. Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend 贪心

    C. Sonya and Problem Wihtout a Legend 题目连接: http://codeforces.com/contest/713/problem/C Description ...

  9. 【CodeForces】713 C. Sonya and Problem Wihtout a Legend

    [题目]C. Sonya and Problem Wihtout a Legend [题意]给定n个数字,每次操作可以对一个数字±1,求最少操作次数使数列递增.n<=10^5. [算法]动态规划 ...

随机推荐

  1. DataGrid 显示选中的item

    Datagrid或者listview 中想要把相应的项 滚动到当前可见的位置, 必须满足2个条件: 1) 必须去掉虚拟化      VirtualizingStackPanel.IsVirtualiz ...

  2. Go 方法和接口

    转自:http://studygolang.com/topics/549 Go 没有类,但可以在结构体类型上定义方法. package main import ( "fmt" &q ...

  3. iOS端IM开发从入门到填坑

      让App聊起来 IM开发从入门到填坑Demo IM的实现方式 拿来主义,使用第三方IM服务 IM的第三方服务商国内有很多,底层协议基本上都是基于TCP的,类似有网易云信.环信.融云.极光IM.Le ...

  4. MemoryStream转string

    MemoryStream rtfTx = new MemoryStream(); var bs = rtfTx.ToArray(); string s = Encoding.UTF8.GetStrin ...

  5. 如何快速编写大项目的Makefile文件

    在构建C++的后台服务时,经常需要自己来编写makefile文件,而如果没有合适的方法或模板时,编写makefile文件是一件很费时费力的事情.因此,为了帮助程序员高效准确的编写makefile文件, ...

  6. React 从入门到进阶之路(五)

    之前的文章我们介绍了  React 事件,方法, React定义方法的几种方式 获取数据 改变数据 执行方法传值.接下来我们将介绍 React 表单事件 键盘事件 事件对象以及 React中 的 re ...

  7. 原生js实现一个侧滑删除取消组件(item slide)

    组件,本质上是解决某个问题封装的类,在此记录原生js实现侧滑删除 先上效果图 实现思路 1. 确定渲染的数据结构 2. 思考划分布局,总的有两个主要的模块:内容区域和按钮区域 2.1 内容区域保持宽度 ...

  8. [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)

    题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...

  9. python矩阵相加

    举个栗子: # 两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵: # 使用 for 迭代并取出 X 和 Y 矩阵中对应位置的值,相加后放到新矩阵的对应位置中. import n ...

  10. ue4 杂记

    c++获取GameMode if(GetWorld()) { auto gamemode = (ASomeGameMode*)GetWorld()->GetAuthGameMode(); } 或 ...