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. HTMLTestRunner带饼图

    # -*- coding: utf-8 -*- """ A TestRunner for use with the Python unit testing framewo ...

  2. 在页面使用echarts的地图(解决地图不完整)

    测试环境:IDEA+Tomcat7 谷歌浏览器 创建好web工程,编写jsp页面,在自己编写的JSP页面上导包 现在echarts停止了在其网站上下载地图脚本,直接通过src引用网站上的china.j ...

  3. Laxcus大数据管理系统2.0(6)- 第四章 数据计算

    第四章 数据计算 Laxcus所有数据计算工作都是通过网络实施.相较于集中计算,在网络间进行的数据计算更适合处理那些数据量大.复杂的.耗时长的计算任务.能够实施网络计算的前提是数据可以被分割,就是把一 ...

  4. OpenPAI大规模人工智能平台安装部署文档

    环境要求: 如果需要图形界面,需要在Ubuntu系统安装,否则centos系统安装时是没有问题的(web端和命令行进行任务提交) 安装过程需要有另外一台控制端机器(注意:区别于集群所在的任何一台服务器 ...

  5. Linux 静态和动态添加路由

    (一)静态添加 1/5 首先让我们查看当前机器的路由表,执行如下命令:route -n [root@vnode33 network-scripts]# route -n Kernel IP routi ...

  6. 拷贝构造函数 & 拷贝赋值运算符

    一.拷贝构造函数 1. 形式 class A { public: // ... A(const A &); // 拷贝构造函数 }; 2. 合成拷贝构造函数 编译器总会为我们合成一个拷贝构造函 ...

  7. 数组的引用——用作形参&返回类型时

    一.数组的引用 切入:可以将一个变量定义成数组的引用(这个变量和数组的类型要相同) 形式: int odd[5] = {1, 3, 5, 7, 9}; int (&arr)[5] = odd; ...

  8. Mishka and Contest(模拟水题)

    Mishka started participating in a programming contest. There are nn problems in the contest. Mishka' ...

  9. coding.net 版本控制

    这是版本测试的所有内容,其中用到了  git 和coding的远程连接. 代码及版本控制 git地址:https://git.coding.net/tianjiping/11111.git

  10. BluetoothClass详解

    一. BluetoothClass简介 1. 继承关系 public final class BluetoothClass extends Object implements Parcelable 该 ...