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

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

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

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

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

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

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cctype>
#include<stack>
using namespace std;
const int N=;
typedef long long ll;
inline ll read(){
ll X=,w=;char ch=;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
inline ll abs(ll a){
if(a<)a=-a;
return a;
}
inline ll min(ll a,ll b){
if(a<b)return a;
return b;
}
ll a[N],b[N],n;
struct tree{
int l,r;
ll dis,val;
}tr[N];
int merge(int x,int y){
if(x==||y==)return x+y;
if(tr[x].val<tr[y].val)
swap(x,y);
tr[x].r=merge(tr[x].r,y);
if(tr[tr[x].l].dis<tr[tr[x].r].dis)
swap(tr[x].l,tr[x].r);
tr[x].dis=tr[tr[x].r].dis+;
return x;
}
stack<int>q,l;
ll solve(){
ll ans=;
for(int i=;i<=n;i++){
int id=i,ct=;
while(!q.empty()&&tr[q.top()].val>tr[id].val){
id=merge(q.top(),id);
q.pop();
if((l.top()+)/+(ct+)/>(l.top()+ct+)/){
id=merge(tr[id].l,tr[id].r);
}
ct+=l.top();
l.pop();
}
l.push(ct);
q.push(id);
}
int i=n+;
while(!q.empty()){
ll k=tr[q.top()].val;
int len=l.top();
l.pop();q.pop();
while(len--){
i--;
ans+=abs(tr[i].val-k);
}
}
return ans;
}
int main(){
n=read();
tr[].dis=-;
for(int i=;i<=n;i++){
a[i]=b[n-i+]=read();
}
for(int i=;i<=n;i++){
tr[i].l=tr[i].r=tr[i].dis=;
tr[i].val=a[i];
}
ll ans=solve();
for(int i=;i<=n;i++){
tr[i].l=tr[i].r=tr[i].dis=;
tr[i].val=b[i];
}
ans=min(ans,solve());
printf("%lld\n",ans);
return ;
}

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. CSS3 子节点选择器

    CSS3中新增了几个子元素选择器,大大提高了开发者的开发效率.之前有些要通过为一个个子元素添加class,或者js实现才能实现的效果.现在可以很方便的用选择器实现. 这些新的样式已被现代浏览器及IE9 ...

  2. DSP5509的GPIO学习-第5篇

    1. 使用CCS V6.1版本,目前已经不局限于仅仅把实验搞清楚了,要深入去探究内部的原理,本章看下GPIO实验 2. 在CCS启动的时候,提示,这个问题是什么,XDAIS是什么?XDAIS (eXp ...

  3. lesson 14 A noble gangster

    lesson 14 A noble gangster there was a ++time++ 时期 times 时期/年代 in times of peace a sum of + money 一笔 ...

  4. ubuntu 执行Python脚本出现: /usr/bin/env: ‘python\r’: No such file or directory

    原因: #!/usr/bin/env python 在ubuntu会变成 #!/usr/bin/env python\r 而\r 会被shell 当成参数 所以出现:  /usr/bin/env: ‘ ...

  5. TW实习日记:第18天

    今天的bug没有那么多了,都是些小bug,一下就改好了.或者是接口那边数据返回的有问题,通知一下同事就ok了.主要今天是在赶功能进度,然而有一个功能模块需求里并没有写,实在是不知道要做成什么样子,真的 ...

  6. 【sessionInfo】使用说明

    对象:sessionInfo 说明:会话类型操作,此对象是session与cookies的完善版,解决了session异常丢失及cookies文件大小的问题. 注意: 1)  访客的IP地址发生变化时 ...

  7. Thunder团队第二周 - Scrum会议4

    Scrum会议4 小组名称:Thunder 项目名称:爱阅app Scrum Master:邹双黛 工作照片: 宋雨同学在拍照,所以不再照片中. 参会成员: 王航:http://www.cnblogs ...

  8. c# 调取 c++ dll____c#调用dll

    1.以海康摄像头dll为例.(文章转载https://www.cnblogs.com/smartsensor/p/4343744.html) 海康SDK编程指南 目前使用的海康SDK包括IPC_SDK ...

  9. Java常用类之Math类

    Java 的常用类Math类: java.lang.Math 提供了系列的静态方法用于科学计算,其方法的参数和返回值类型一般为 double 类型. 如: 1. public static final ...

  10. PCA算法理解及代码实现

    github:PCA代码实现.PCA应用 本文算法均使用python3实现 1. 数据降维   在实际生产生活中,我们所获得的数据集在特征上往往具有很高的维度,对高维度的数据进行处理时消耗的时间很大, ...