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 ...
随机推荐
- EF Core注意事项
流程:https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db 1.Both Entity Framework 6. ...
- python3 BeautifulSoup模块使用
BeautifulSoup就是Python的一个HTML或XML的解析库,可以用它来方便地从网页中提取数据.官方解释如下: Beautiful Soup提供一些简单的.Python式的函数来处理导航. ...
- 接口测试工具postman(七)下载文件接口
按照一般请求接口,配置好接口地址以及参数,点击Send and Download 按钮,执行请求的同时会下载文件
- 记录---Testin上新手测试用例设计实战---碎乐3.2.0
平台上给的版本是碎乐3.12版的,但是平台上给的安装包下载不了,所以加群咨询之后给出了直接去手机应用商店下载搜索到的版本的对策.所以就那应用商店中找到的3.2.0版本来设计测试用例.因为任务中没有给出 ...
- html简约风用户登录界面网页制作html5-css-jquary-学习模版
2018--12-12 喜迎双十二,咳咳,,,,我不是打广告哈,购物的节日也不要忘记学习. 大家好,我又来了. 今天抽出来空把自己的学习心得给大家分享,这是一个可开发可扩展的用户登录界面,用于开发学习 ...
- 【progress】 进度条组件说明
progress 进度条组件 原型: <progress percent="[Float(0-100)]" show-info="[Boolean]" b ...
- [转载]CENTOS 6.0 iptables 开放端口80 3306 22端口
原文地址:6.0 iptables 开放端口80 3306 22端口">CENTOS 6.0 iptables 开放端口80 3306 22端口作者:云淡风轻 #/sbin/iptab ...
- Machine Learning分类:监督/无监督学习
从宏观方面,机器学习可以从不同角度来分类 是否在人类的干预/监督下训练.(supervised,unsupervised,semisupervised 以及 Reinforcement Learnin ...
- Windows环境下的TensorFlow安装过程
安装环境 Windows8.1 python3.5.x(TensorFlow only supports version 3.5.x of Python on Windows) pip 9.0.1 t ...
- erlang+thrift配合开发
I think, thrift is a tcp/ip based Client-Server architecture multi-languages supported RPC framewo ...