题目描述

春春幼儿园举办了一年一度的“积木大赛”。今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi。

在搭建开始之前,没有任何积木(可以看成n块高度为 0 的积木)。接下来每次操作,小朋友们可以选择一段连续区间[l, r],然后将第第 L 块到第 R 块之间(含第 L 块和第 R 块)所有积木的高度分别增加1。

小 M 是个聪明的小朋友,她很快想出了建造大厦的最佳策略,使得建造所需的操作次数最少。但她不是一个勤于动手的孩子,所以想请你帮忙实现这个策略,并求出最少的操作次数。

输入输出格式

输入格式:

输入文件为 block.in

输入包含两行,第一行包含一个整数n,表示大厦的宽度。

第二行包含n个整数,第i个整数为hi 。

输出格式:

输出文件为 block.out

仅一行,即建造所需的最少操作数。

输入输出样例

5
2 3 4 1 2
输出样例#1:

5

说明

【样例解释】

其中一种可行的最佳方案,依次选择

[1,5] [1,3] [2,3] [3,3] [5,5]

【数据范围】

对于 30%的数据,有1 ≤ n ≤ 10;

对于 70%的数据,有1 ≤ n ≤ 1000;

对于 100%的数据,有1 ≤ n ≤ 100000,0 ≤ hi≤ 10000。

思路:

  这道题很深,你可以做的很麻烦,也可以做的很简单,你都认为那是正解。

  我就想了很多思路,然后用了最麻烦的方法。。但至少练习了一下倍增

  1递归去做  

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<vector>
#include<cmath>
#include<ctime>
using namespace std;
const int N=;
int a[N];
int n;
int f1[N][],F;//zhi
int f2[N][];//where
long long ans;
inline void work(int L,int R,int tot)
{
if(L==R)
{
ans+=a[L]-tot;
return ;
}
if(L>R) return ;
int F=(log2(R-L+)/log2());
int minn,minw;
if(f1[L][F]<=f1[R-(<<F)+][F])
minn=f1[L][F],minw=f2[L][F];
else
minn=f1[R-(<<F)+][F],minw=f2[R-(<<F)+][F];
ans+=minn-tot;
work(L,minw-,minn);work(minw+,R,minn);
return ;
}
int main()
{ scanf("%d",&n);
F=(log2(n)/log2());
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
f1[i][]=a[i];
f2[i][]=i;
}
for(int j=;j<=F;j++)
for(int i=;i<=n;i++)
{
if(f1[i][j-]<=f1[i+(<<(j-))][j-])
f1[i][j]=f1[i][j-],f2[i][j]=f2[i][j-];
else
f1[i][j]=f1[i+(<<(j-))][j-],f2[i][j]=f2[i+(<<(j-))][j-];
}
work(,n,);
cout<<ans<<endl;
return ;
}

倍增,递归

  2有点贪心吧,我也说不清

  就是如果后一个比前个,大就要加它们的差。

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<vector>
#include<cmath>
#include<ctime>
using namespace std;
const int N=;
int x,last,n;
long long ans;
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&x);
if(x>last) ans+=x-last;
last=x;
}
cout<<ans;
return ;
}

贪心

最新文章

  1. php实验四
  2. JAVA 设计模式 解释器模式
  3. 织梦DedeCMS调用二级子栏目或者多级栏目解决方法
  4. 【转】android中最好的瀑布流控件PinterestLikeAdapterView
  5. [转]bat批处理实现TXT文本合并
  6. 【无聊放个模板系列】BZOJ 3172 (AC自动机)
  7. PC10303/UVA10252
  8. CodeForces 111B - Petya and Divisors 统计..想法题
  9. PHP发送短信如何实现?
  10. Dynamic Performance Tables not accessible Automatic Statistics disabled for this session
  11. Promise和setTimeout执行顺序 面试题
  12. git遇到的问题-- Another git process seems to be running in this repository
  13. restfull环境搭建-helloword(二)
  14. &lt;转&gt;如何用C++实现自动微分
  15. 04_web基础(八)之车票实现增删改查初级版本
  16. 转载:$(function() {}),即$(document).ready(function(),什么时候执行?以此为准,真理
  17. 【GOF23设计模式】--工厂模式
  18. Centos设置开机启动Apache和Mysql[总结]
  19. 分享Memcached shell启动停止脚本
  20. Linux下的lds链接脚本详解【转】

热门文章

  1. Codeforces617E XOR and Favorite Number(分块 异或)
  2. jQuery bootstrap框架下重置下拉框选择
  3. ls命令还能这么玩
  4. 洛谷 P2285 [HNOI2004]打鼹鼠
  5. BZOJ1500:[NOI2005]维修数列
  6. .Net 学习资源整理
  7. zk 01之 ZooKeeper概述
  8. MangoDB篇章(1)
  9. hdu 4123 Bob’s Race (dfs树上最远距离+RMQ)
  10. 使用VSTO写的一个工作证打印软件