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. [算法]动态规划 ...
随机推荐
- HDU1828:Picture
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:http://acm.hdu.edu.cn/showproblem.php? ...
- 洛谷 1344 [USACO4.4]追查坏牛奶Pollutant Control——最大流
题目:https://www.luogu.org/problemnew/show/P1344 那个边数的限制,只要把边权乘1001再+1即可.乘1001是因为有1000条边,这样流量小的不会因为边数多 ...
- POJ3928(树状数组:统计数字出现个数)
Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2641 Accepted: 978 Descript ...
- python find()用法
案例: ### 1 ### str = "01213456" if str.find("23"): print "YES!" else: p ...
- Selenium实战脚本集—新浪微博发送QQ每日焦点
Selenium实战脚本集-新浪微博发送QQ每日焦点 http://www.spasvo.com/ceshi/open/kygncsgj/Selenium/201549150822.html 背景 很 ...
- Eclipse中一个开发AspectJ的插件安…
eclipse4.2 EE版本插件安装 Eclipse最新版本Juno版本发布.部分插件版本跟不上. 选择自己需要的插件安装. eclipse http://www.eclipse.org/downl ...
- glib 库 hash table 使用
glib库提供了 hashtable 的实现 1. 常用函数: 创建一个 GHashTable 函数: hash_func 是创建value的key值的函数,key_equal_func 是比较两个k ...
- 给Fitnesse添加json格式报告
需求:fitnesse自带xml.junit.html格式报告,现在需要添加json格式的报告,且报告中只展示执行错误的用例信息 修改文件: fitnesse.http.Response.java f ...
- CentOS 下部署Nginx+Gunicorn+Supervisor部署Flask项目
原本之前有一部分东西是在Windows Server,但是由于Gunicorn不支持Windows部署起来颇为麻烦.最近转战CentOS,折腾一段时间,终于简单部署成功.CentOS新手,作为一个总结 ...
- SQL中SUM函数和CASE WHEN联合使用
SELECT SUM(case WHEN sex=1 then 1 else 0 end )as '男生', SUM(case when sex =2 then 1 else 0 end )'女生'F ...