CF713C Sonya and Problem Wihtout a Legend & hihocoder1942 单调序列
这两个题是一样的,不过数据范围不同。
思路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 单调序列的更多相关文章
- CF713C Sonya and Problem Wihtout a Legend
考虑我们直接选择一个暴力\(dp\). \(f_{i,j} = min_{k<=j}\ (f_{i - 1,k}) + |a_i - j|\) 我们考虑到我们直接维护在整个数域上\(min(f_ ...
- Codeforces 713C Sonya and Problem Wihtout a Legend(单调DP)
[题目链接] http://codeforces.com/problemset/problem/713/C [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上 ...
- CodeForces 714E Sonya and Problem Wihtout a Legend(单调数列和DP的小研究)
题意:给你n个数字,每个数字可以加减任何数字,付出变化差值的代价,求最后整个序列是严格单调递增的最小的代价. 首先我们要将这个题目进行转化,因为严格单调下是无法用下面这个dp的方法的,因此我们转化成非 ...
- 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 ...
- 把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend
//把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend //dp[i][j]:把第i个数转成第j小的数,最小花费 //此题与po ...
- 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 ...
- 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 ...
- 【CodeForces】713 C. Sonya and Problem Wihtout a Legend
[题目]C. Sonya and Problem Wihtout a Legend [题意]给定n个数字,每次操作可以对一个数字±1,求最少操作次数使数列递增.n<=10^5. [算法]动态规划 ...
随机推荐
- CF 438 E & bzoj 3625 小朋友和二叉树 —— 多项式开方
题目:http://codeforces.com/contest/438/problem/E https://www.lydsy.com/JudgeOnline/problem.php?id=3625 ...
- poj3208启示录——数位DP
题目:http://poj.org/problem?id=3208 数位DP,首先按位数预处理出每一种位数的情况,包括有多少个魔鬼数和有多少个以6开头的非魔鬼数,以便递推.累加等等: 然后先找出第X个 ...
- mac下nginx的安装
新手初学,按照网上的教程,一步一步来进行安装.把自己的安装流程贴出来. 1 安装nginx需要三方的lib库pcre.因此先下载pcre. 在这里,需要注意的是安装的pcre的版本要与nginx对应. ...
- navicat 关于orcale新建表空间,用户和权限分配
图文教程,直观, 上面连接数据库 下面创建表空间 建表空间的设置 表空间名的设置 新建用户 填写用户名,选择默认表空间 成员属性德设置,这里因为是自己用,所以选择最大权限,其他的权限这是需要专业的了 ...
- 11_listview入门
listview是在安卓开发当中很常用的API. 以垂直滚动的列表的方式展示条目的控件. ListAdapter是一个桥梁,给ListView提供数据的.数据是由适配器来进行提供的.Adapter是数 ...
- apache配置中的小细节
configuration error: couldn’t perform authentication错误的解决办法 configuration error: couldn’t perform au ...
- [yii]Trying to get property of non-object
今天接触gridview的时候,发现总是报错,如图. array( 'name'=>'user_info.userinfo', 'value'=>'$data->user_info- ...
- minihttp安装配置ssl和c语言实现cgi
概述:参考了大牛们的方法,结合自己的环境做了修改,主要是讲:minihttp安装配置ssl和c语言实现cgi接收字符串并且保存系统环境:centos6.5 开发版 依赖软件包: mini_httpd- ...
- 模板 - SPFA
SPFA可以用来判断负环或者计算带负权的最短路. 其实带负权的最短路可以用带势Dijkstra计算-- 所以SPFA基本就拿来判负环了-- #include<bits/stdc++.h> ...
- js 正则表达式学习笔记
正则表达式正则表达式是由一个字符序列形成的搜索模型 语法new RegExp("[abc]")/[abc]//正则表达式主体/修饰符(可选) 1.修饰符i 忽略大小写g 执行全局匹 ...