http://poj.org/problem?id=3666

题目大意:给n个数,每次操作可使一个数+1或-1,求最小操作数使得序列不下降或不上升。

——————————————————————

思路:http://blog.csdn.net/luovilonia/article/details/44004041

因为我再讲什么也没什么好讲的了。

但是这人的代码是错的……请注意查找最长不上升和不下降所要减的值是不一样的。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<cctype>
  5. #include<stack>
  6. using namespace std;
  7. const int N=;
  8. typedef long long ll;
  9. inline ll read(){
  10. ll X=,w=;char ch=;
  11. while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
  12. while(isdigit(ch))X=(X<<)+(X<<)+(ch^),ch=getchar();
  13. return w?-X:X;
  14. }
  15. inline ll abs(ll a){
  16. if(a<)a=-a;
  17. return a;
  18. }
  19. inline ll min(ll a,ll b){
  20. if(a<b)return a;
  21. return b;
  22. }
  23. ll a[N],b[N],n;
  24. struct tree{
  25. int l,r;
  26. ll dis,val;
  27. }tr[N];
  28. int merge(int x,int y){
  29. if(x==||y==)return x+y;
  30. if(tr[x].val<tr[y].val)
  31. swap(x,y);
  32. tr[x].r=merge(tr[x].r,y);
  33. if(tr[tr[x].l].dis<tr[tr[x].r].dis)
  34. swap(tr[x].l,tr[x].r);
  35. tr[x].dis=tr[tr[x].r].dis+;
  36. return x;
  37. }
  38. stack<int>q,l;
  39. ll solve(){
  40. ll ans=;
  41. for(int i=;i<=n;i++){
  42. int id=i,ct=;
  43. while(!q.empty()&&tr[q.top()].val>tr[id].val){
  44. id=merge(q.top(),id);
  45. q.pop();
  46. if((l.top()+)/+(ct+)/>(l.top()+ct+)/){
  47. id=merge(tr[id].l,tr[id].r);
  48. }
  49. ct+=l.top();
  50. l.pop();
  51. }
  52. l.push(ct);
  53. q.push(id);
  54. }
  55. int i=n+;
  56. while(!q.empty()){
  57. ll k=tr[q.top()].val;
  58. int len=l.top();
  59. l.pop();q.pop();
  60. while(len--){
  61. i--;
  62. ans+=abs(tr[i].val-k);
  63. }
  64. }
  65. return ans;
  66. }
  67. int main(){
  68. n=read();
  69. tr[].dis=-;
  70. for(int i=;i<=n;i++){
  71. a[i]=b[n-i+]=read();
  72. }
  73. for(int i=;i<=n;i++){
  74. tr[i].l=tr[i].r=tr[i].dis=;
  75. tr[i].val=a[i];
  76. }
  77. ll ans=solve();
  78. for(int i=;i<=n;i++){
  79. tr[i].l=tr[i].r=tr[i].dis=;
  80. tr[i].val=b[i];
  81. }
  82. ans=min(ans,solve());
  83. printf("%lld\n",ans);
  84. return ;
  85. }

POJ3666:Making the Grade——题解的更多相关文章

  1. POJ3666 Making the Grade

    POJ3666 Making the Grade 题意: 给定一个长度为n的序列A,构造一个长度为n的序列B,满足b非严格单调,并且最小化S=∑i=1N |Ai-Bi|,求出这个最小值S,1<= ...

  2. 【DP】+【贪心】【前缀和】洛谷P2893 [USACO08FEB]修路Making the Grade 题解

        正常的没想到的DP和玄学贪心. 题目描述 A straight dirt road connects two fields on FJ's farm, but it changes eleva ...

  3. poj3666 Making the grade【线性dp】

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:10187   Accepted: 4724 ...

  4. POJ3666 Making the Grade [DP,离散化]

    题目传送门 Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9090   Accepted: ...

  5. poj3666 Making the Grade(基础dp + 离散化)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  6. 「POJ 3666」Making the Grade 题解(两种做法)

    0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...

  7. [poj3666]Making the Grade(DP/左偏树)

    题目大意:给你一个序列a[1....n],让你求一个序列b[1....n],满足 bi =a && bc,则最小的调整可以是把b变成c. 所以归纳可知上面结论成立. dp[i][j] ...

  8. 常规DP专题练习

    POJ2279 Mr. Young's Picture Permutations 题意 Language:Default Mr. Young's Picture Permutations Time L ...

  9. 「kuangbin带你飞」专题十二 基础DP

    layout: post title: 「kuangbin带你飞」专题十二 基础DP author: "luowentaoaa" catalog: true tags: mathj ...

随机推荐

  1. 「日常训练」 Genghis Khan the Conqueror(HDU-4126)

    题意 给定\(n\)个点和\(m\)条无向边(\(n\le 3000\)),需要将这\(n\)个点连通.但是有\(Q\)次(\(Q\le 10^4\))等概率的破坏,每次破坏会把\(m\)条边中的某条 ...

  2. ObjectMapper的使用

    Jackson ObjectMapper类 ObjectMapper类是Jackson库的主要类它提供一些功能将Java对象转换成JSON结构,反之亦然它使用JsonParser和JsonGenera ...

  3. 初涉 JavaScript

    网页是什么 网页 = Html+CSS+JavaScriptHtml:网页元素内容CSS:控制网页样式JavaScript:操作网页内容,实现功能或者效果 JavaScirpt 发展历史 参考 使用 ...

  4. linux c语言 fork() 和 exec 函数的简介和用法

    linux c语言 fork() 和 exec 函数的简介和用法   假如我们在编写1个c程序时想调用1个shell脚本或者执行1段 bash shell命令, 应该如何实现呢? 其实在<std ...

  5. python常用命令—‘\r’

    # \r 默认表示将输出的内容返回到第一个指针,这样的话,后面的内容会覆盖前面的内容 如常用的显示程序完成进度!!

  6. python SyntaxError: Non-ASCII character '\xe8' in file C:\Users\nwpujun\PycharmProjects\projects\hrl1\hrlAgent\src\li_nn.py on line 52

    解决方法:在文件头部加上这样的一句话 # -*- coding: utf-8 -*- 注意:加在其他的位置可能没用,我就是这样的

  7. js经典试题之数据类型

    js经典试题之数据类型 1:输出"B" + "a" + + "B" + "a"的值: 答案:BaNaNa. 分析:因为+ ...

  8. java知乎爬虫

    好久没写博客了,前阵子项目忙着上线,现在有点空闲,就把最近写的一个爬虫和大家分享下,统计结果放在了自己买的阿里云服务器上(点此查看效果),效果如下: 程序是在工作之余写的,用了java 的webmgi ...

  9. 无法启动mysql服务 错误1067:进程意外中止

    这个错误在前些周遇到过,没有解决,直接粗暴的卸载重装了,自己用的是wampserver集成环境,重装的后果是mysql里面的一些已有的数据库就没有了,有点小悲剧,不过幸好都是一些测试用的数据库,后面直 ...

  10. Alpha冲刺——第二天

    Alpha第二天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...