题目

Source

http://codeforces.com/problemset/problem/713/C

Description

Sonya was unable to think of a story for this problem, so here comes the formal description.

You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array.

Next line contains n integer ai (1 ≤ ai ≤ 109).

Output

Print the minimum number of operation required to make the array strictly increasing.

Sample Input

7
2 1 5 11 5 9 11
5
5 4 3 2 1

Sample Output

9
12

分析

题目打给说给一个序列,可以进行若干次操作,每次操作选择一个数让它+1或-1,问最少要几次操作使得序列变为严格单调递增序列。

首先考虑不是严格递减的情况,这其实是个经典问题的样子,太弱都没做过。。

  • 不严格递减最后修改完成后的各个数一定是原序列中的某一个数

这个大概可以这么理解:原序列,从左到右扫过去,如果左边的大于右边的,要嘛左边的减掉使其等于右边的,要嘛右边的加上使其等于左边的。

于是就可以考虑用dp来做了:

  • dp[i][j]表示序列前i个数都单调递增且第i个数更改为不大于原序列中第j个数的最少代价
  • 转移就是dp[i][j]=min(dp[i][j-1],dp[i-1][j]+abs(a[i],b[j])),a是原序列,b是原序列排序去重的序列

回到原问题,原问题是求严格单调递增,这个可以转化成不严格单调递增:

  • 让原序列每个数a[i]减去i

这样转化可行是因为:

这个转化感觉挺神奇的。。

代码

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. long long d[3333][3333],a[3333],b[3333];
  7. int main(){
  8. int n;
  9. scanf("%d",&n);
  10. for(int i=1; i<=n; ++i){
  11. scanf("%d",a+i);
  12. a[i]-=i;
  13. b[i]=a[i];
  14. }
  15. sort(b+1,b+1+n);
  16. int bn=unique(b+1,b+1+n)-b-1;
  17. memset(d,127,sizeof(d));
  18. for(int i=1; i<=bn; ++i){
  19. d[1][i]=min(d[1][i-1],abs(a[1]-b[i]));
  20. }
  21. for(int i=2; i<=n; ++i){
  22. for(int j=1; j<=bn; ++j){
  23. d[i][j]=min(d[i][j-1],d[i-1][j]+abs(a[i]-b[j]));
  24. }
  25. }
  26. printf("%lld",d[n][bn]);
  27. return 0;
  28. }

Codeforces713C Sonya and Problem Wihtout a Legend(DP)的更多相关文章

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

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

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

  3. codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)

    题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...

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

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

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

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

  6. 【CF713C】Sonya and Problem Wihtout a Legend(离散化,DP)

    题意:给你一个数列,对于每个数字你都可以++或者−− 然后花费就是你修改后和原数字的差值,然后问你修改成一个严格递增的,最小花费 思路:很久以前做过一道一模一样的 严格递增很难处理,就转化为非严格递增 ...

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

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

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

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

随机推荐

  1. URL Schemes

    APP 被唤醒离不开对URL Schemes的认知. 苹果选择沙盒来保障用户的隐私和安全,但沙盒也阻碍了应用间合理的信息共享,于是有了 URL Schemes 这个解决办法. URL Schemes ...

  2. 文件IO函数和标准IO库的区别

    摘自 http://blog.chinaunix.net/uid-26565142-id-3051729.html 1,文件IO函数,在Unix中,有如下5个:open,read,write,lsee ...

  3. 评价cnblogs.com的用户体验

    我们作为大学计算机科学与技术专业的本科生,使用博客园主要是用来搜索和阅读技术资料,并且使用它来编辑发表博客.我们偏向于搜索便捷,界面友好的网站风格.我们对cnblogs的期望是更便捷精准,更便于编辑. ...

  4. 墙裂推荐一本案例驱动的PhoneGap入门书,早看早收货

    清华大学出版社推出的<构建跨平台APP:PhoneGap移动应用实战> 零门槛学APP开发 从无到有 循序渐进 20余个示例APP 3个项目APP 全平台à跨终端à移动开发 完美生命周期: ...

  5. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  6. ReactiveCocoa源码拆分解析(五)

    (整个关于ReactiveCocoa的代码工程可以在https://github.com/qianhongqiang/QHQReactive下载) 好多天没写东西了,今天继续.主要讲解RAC如何于UI ...

  7. 在OSX和Windows版本Docker上运行GUI程序

    看到很多人在Docker问题区讨论:如何在OS X和Windows的Docker上运行GUI程序, 随手记录几个参考资料: https://github.com/docker/docker/issue ...

  8. Ubuntu 使用phpmyadmin,报错#1146 - Table ‘phpmyadmin.pma_table_uiprefs' doesn't exist

    cd /etc/phpmyadminsudo vim config.inc.php 修改phpmyadmin的配置文件config.inc.php $cfg['Servers'][$i]['table ...

  9. 百度地图SDK

      百度地图官方SDK文档 http://lbsyun.baidu.com/index.php?title=androidsdk   一.申请百度地图SDK 每一个app对应一个百度地图AK,百度地图 ...

  10. 解决Can't connect to MySQL server on 'localhost' (10048)

    解决Can't connect to MySQL server on 'localhost' (10048) 您使用的是Windows操作系统,此错误与一个注册表键值TcpTimedWaitDelay ...