洛谷 P1969 积木大赛
2024-09-02 03:56:52
题目描述
春春幼儿园举办了一年一度的“积木大赛”。今年比赛的内容是搭建一座宽度为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 ;
}
贪心
最新文章
- php实验四
- JAVA 设计模式 解释器模式
- 织梦DedeCMS调用二级子栏目或者多级栏目解决方法
- 【转】android中最好的瀑布流控件PinterestLikeAdapterView
- [转]bat批处理实现TXT文本合并
- 【无聊放个模板系列】BZOJ 3172 (AC自动机)
- PC10303/UVA10252
- CodeForces 111B - Petya and Divisors 统计..想法题
- PHP发送短信如何实现?
- Dynamic Performance Tables not accessible Automatic Statistics disabled for this session
- Promise和setTimeout执行顺序 面试题
- git遇到的问题-- Another git process seems to be running in this repository
- restfull环境搭建-helloword(二)
- <;转>;如何用C++实现自动微分
- 04_web基础(八)之车票实现增删改查初级版本
- 转载:$(function() {}),即$(document).ready(function(),什么时候执行?以此为准,真理
- 【GOF23设计模式】--工厂模式
- Centos设置开机启动Apache和Mysql[总结]
- 分享Memcached shell启动停止脚本
- Linux下的lds链接脚本详解【转】