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

思路1:

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

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

实现1:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N = ;
  5. ll a[N], dp[N][N];
  6. int main()
  7. {
  8. int n;
  9. while (cin >> n)
  10. {
  11. for (int i = ; i <= n; i++) { cin >> a[i]; a[i] -= i; }
  12. vector<ll> v(a + , a + n + );
  13. sort(v.begin(), v.end());
  14. v.erase(unique(v.begin(), v.end()), v.end());
  15. int m = v.size();
  16. for (int i = ; i <= n; i++)
  17. {
  18. for (int j = ; j < m; j++)
  19. {
  20. dp[i][j] = dp[i - ][j] + abs(a[i] - v[j]);
  21. if (j) dp[i][j] = min(dp[i][j], dp[i][j - ]);
  22. }
  23. }
  24. cout << dp[n][m - ] << endl;
  25. }
  26. return ;
  27. }

思路2:

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

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

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

实现2:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N = ;
  5. int a[N];
  6. ll solve(int n)
  7. {
  8. ll ans = ;
  9. priority_queue<int> pq;
  10. for (int i = ; i <= n; i++)
  11. {
  12. pq.push(a[i]);
  13. if (a[i] < pq.top())
  14. {
  15. ans += (ll)pq.top() - a[i];
  16. pq.pop();
  17. pq.push(a[i]);
  18. }
  19. }
  20. return ans;
  21. }
  22. int main()
  23. {
  24. int n, x;
  25. while (cin >> n)
  26. {
  27. for (int i = ; i <= n; i++) cin >> a[i];
  28. ll ans = solve(n);
  29. reverse(a + , a + n + );
  30. ans = min(ans, solve(n));
  31. cout << ans << endl;
  32. }
  33. return ;
  34. }

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. Mesos的quorum配置引发的问题

    Mesos安装完毕后,发现agent无法和master关联(通过WebUI的agent页面无法看到agent信息),查看日志显示: Elected as the leading master! sta ...

  2. BZOJ1926:[SDOI2010]粟粟的书架

    浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...

  3. WPF DelegateCommand CanExecute

     private DelegateCommand searchCommand;         public DelegateCommand SearchCommand         {       ...

  4. maven+eclpse+jmeter+jenkis

    1.maven配置:http://www.cnblogs.com/AlanLee/p/6133189.html 2.在eclipse中创建maven项目:https://jingyan.baidu.c ...

  5. JAVA 编程思想二

    1: java  单根继承的优点? 方便垃圾回收: 垃圾回收的设计会方便实现.   多重继承的函数重名的问题. 2: 向下转型和向上转型?    向下转型不安全,向上转型安全. 3: system.g ...

  6. Logstash-安装logstash-filter-multiline插件(解决logstash匹配多行日志)

    ELK-logstash在搬运日志的时候会出现多行日志,普通的搬运会造成保存到ES中日志一条一条的保存,很丑,而且不方便读取,logstash-filter-multiline可以解决该问题. 接下来 ...

  7. linux&nbsp;ip地址自动获取,ip地址…

    linux ip地址自动获取,ip地址手动设置(图文解释) 2011-04-19 16:19:31| 分类: 服务器(appache/n | 标签: |字号大中小 订阅 linux ip地址自动获取( ...

  8. prototype for '类名::函数名'does not match any in class'类名'

    函数声明和定义参数类型必须相同. 前置声明一定要放到名称空间内,代表该名称空间内的类.

  9. 初步使用redis

    1.导入jar包 2.新建类: public class JedisAdapter { private static final Logger logger = LoggerFactory.getLo ...

  10. 如何部署JavaWeb应用

    准备 公网主机一台(推荐云服务器) 数据库安装包 JDK安装包 Tomcat安装包 WAR包(web应用包) 部署 安装所需软件,并测试基本环境是否可用 将WAR包解压至Tomcat目录下的webap ...