POJ3666:Making the Grade——题解
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——题解的更多相关文章
- POJ3666 Making the Grade
POJ3666 Making the Grade 题意: 给定一个长度为n的序列A,构造一个长度为n的序列B,满足b非严格单调,并且最小化S=∑i=1N |Ai-Bi|,求出这个最小值S,1<= ...
- 【DP】+【贪心】【前缀和】洛谷P2893 [USACO08FEB]修路Making the Grade 题解
正常的没想到的DP和玄学贪心. 题目描述 A straight dirt road connects two fields on FJ's farm, but it changes eleva ...
- poj3666 Making the grade【线性dp】
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions:10187 Accepted: 4724 ...
- POJ3666 Making the Grade [DP,离散化]
题目传送门 Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9090 Accepted: ...
- poj3666 Making the Grade(基础dp + 离散化)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- 「POJ 3666」Making the Grade 题解(两种做法)
0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...
- [poj3666]Making the Grade(DP/左偏树)
题目大意:给你一个序列a[1....n],让你求一个序列b[1....n],满足 bi =a && bc,则最小的调整可以是把b变成c. 所以归纳可知上面结论成立. dp[i][j] ...
- 常规DP专题练习
POJ2279 Mr. Young's Picture Permutations 题意 Language:Default Mr. Young's Picture Permutations Time L ...
- 「kuangbin带你飞」专题十二 基础DP
layout: post title: 「kuangbin带你飞」专题十二 基础DP author: "luowentaoaa" catalog: true tags: mathj ...
随机推荐
- vim 安装
Ubuntu 16.04 下 Vim安装及配置 默认已经安装了VIM-tiny linuxidc@linuxidc:~$ locate vi | grep 'vi$' |xargs ls -al lr ...
- python 水仙花
#简单def narcissus(): for n in range(100, 1000, 1): a, b, c = n//100, (n//10)%10, (n%100)%10 if a ** 3 ...
- Object里面的方法
object里面有12个方法,没写完,写一些部分代表 toString():输出对象的地址字符串(hashcode码) equals():用的是==,比较的是引用,在有些类里面是重写了这个方法的,重写 ...
- jQuery实现“回到顶部”按钮功能
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【view】 视图组件说明
view 是视图容器,可用于包裹其它组件或文本内容. 原型: <view hover-class="[String]" hover-stop-propagation=&quo ...
- 关于javascript的一个小问题,请问有人看出啥问题吗?
最近学习javascript,有一个问题挺奇怪的,先贴出代码: function binarySearch(){ var arr = [0,1,2,3]; var res = actbinarySea ...
- 爬虫2.1-scrapy框架-两种爬虫对比
目录 scrapy框架-两种爬虫对比和大概流程 1. 传统spider爬虫 2. crawl型爬虫 3. 循环页面请求 4. scrapy框架爬虫的大致流程 scrapy框架-两种爬虫对比和大概流程 ...
- Machine Learning笔记整理 ------ (五)决策树、随机森林
1. 决策树 一般的,一棵决策树包含一个根结点.若干内部结点和若干叶子结点,叶子节点对应决策结果,其他每个结点对应一个属性测试,每个结点包含的样本集合根据属性测试结果被划分到子结点中,而根结点包含样本 ...
- [C++] Fucntions
Statements A break statements terminate the nearest wile, do while, for or switch statement. A break ...
- Centos6更新yum repo
163开源镜像站是国内比较老的一个网站.很多人都在使用. step 1/3 备份原镜像文件: cd /etc/yum.repos.d mv CentOS-Base.repo CentOS-Base.r ...