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 ...
随机推荐
- 「日常训练」 Genghis Khan the Conqueror(HDU-4126)
题意 给定\(n\)个点和\(m\)条无向边(\(n\le 3000\)),需要将这\(n\)个点连通.但是有\(Q\)次(\(Q\le 10^4\))等概率的破坏,每次破坏会把\(m\)条边中的某条 ...
- ObjectMapper的使用
Jackson ObjectMapper类 ObjectMapper类是Jackson库的主要类它提供一些功能将Java对象转换成JSON结构,反之亦然它使用JsonParser和JsonGenera ...
- 初涉 JavaScript
网页是什么 网页 = Html+CSS+JavaScriptHtml:网页元素内容CSS:控制网页样式JavaScript:操作网页内容,实现功能或者效果 JavaScirpt 发展历史 参考 使用 ...
- linux c语言 fork() 和 exec 函数的简介和用法
linux c语言 fork() 和 exec 函数的简介和用法 假如我们在编写1个c程序时想调用1个shell脚本或者执行1段 bash shell命令, 应该如何实现呢? 其实在<std ...
- python常用命令—‘\r’
# \r 默认表示将输出的内容返回到第一个指针,这样的话,后面的内容会覆盖前面的内容 如常用的显示程序完成进度!!
- 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 -*- 注意:加在其他的位置可能没用,我就是这样的
- js经典试题之数据类型
js经典试题之数据类型 1:输出"B" + "a" + + "B" + "a"的值: 答案:BaNaNa. 分析:因为+ ...
- java知乎爬虫
好久没写博客了,前阵子项目忙着上线,现在有点空闲,就把最近写的一个爬虫和大家分享下,统计结果放在了自己买的阿里云服务器上(点此查看效果),效果如下: 程序是在工作之余写的,用了java 的webmgi ...
- 无法启动mysql服务 错误1067:进程意外中止
这个错误在前些周遇到过,没有解决,直接粗暴的卸载重装了,自己用的是wampserver集成环境,重装的后果是mysql里面的一些已有的数据库就没有了,有点小悲剧,不过幸好都是一些测试用的数据库,后面直 ...
- Alpha冲刺——第二天
Alpha第二天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...