Cave

大致题意:

一个洞穴,已经i位置地面高度和顶的高度,要求在这个洞穴里面储蓄尽可能多的燃料,而且任何位置燃料不能碰到顶点

思路:

先从左往右扫描一下得出每一个点燃料能达到的最大高度,然后右边一样扫一遍,两个取最小值,然后累加

看起来很简单,可是实践起来很麻烦,需要讨论好不同条件下的最大高度!!!!

首先我们假设一个位置左端的最大高度为h

那么i+1位置地面高度p,顶点高度是s.

如果p>h,说明燃料被阻断!!!i+1位置上最大高度为自己!!!

如果s<h,说明当前最大高度还是h

如果p<h,最大高度和之前相同

反正扫描一下,就好了,这样充分保证了一个点左右均合法了!!!!

这个和以前遇到的最大长方形面积很相似





#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector> using namespace std;
const int maxn = 1e6+100;
#define pr(x) cout << #x << " = " << x << " ";
#define prln(x) cout << #x << " = " << x <<endl;
#define ll long long
int p[maxn], s[maxn], h[maxn], lev, ans, n;
int main(){
#ifdef LOCAL
freopen("C:\\Users\\User Soft\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\User Soft\\Desktop\\out.txt","w",stdout);
#endif
int t; cin >> t;
while(t--) {
cin >> n;
ans = 0;
for(int i = 0; i < n; ++i) scanf("%d", p +i);
for(int i = 0; i < n; ++i) scanf("%d", s +i);
lev = s[0];
for(int i = 0; i < n; ++i) {
if(lev < p[i]) lev = p[i];
if(lev > s[i]) lev = s[i];
h[i] = lev;
}
lev = s[n-1];
for(int i = n-1; i >= 0; --i) {
if(lev < p[i]) lev = p[i];
if(lev > s[i]) lev = s[i];
ans += min(lev, h[i]) - p[i];
}
cout << ans << endl;
}
return 0;
}

UVA1442_Cave的更多相关文章

随机推荐

  1. [fw]Die 為什麼不能用現在完成式?

    have PP是表示"從以前到現在"都直在做的事情 Mr. Chen has taught English for 30 years.---表示teach的動作持續了30年,但Mr ...

  2. 攻防世界--getit

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/8ef2f7ef55c240418f84b3c514a7a28a 准备 得知 64位文件 ...

  3. upx压缩notepad.exe(运行时压缩)

    PEView:https://www.lanzous.com/i5k9vbg UPX:https://www.lanzous.com/i5k9vch notepad.exe:https://www.l ...

  4. jq中的ajax传参

        一.   jq中的Ajax传参有两种           1.通过url地址来传参    2.通过data来传递参数 1. url来传递参数 function GetQuery(id) { | ...

  5. webpack的理解、总结

    weabpck的基础应用 https://blog.zhangjd.me/2016/06/19/webpack-your-bags/ https://juejin.im/post/5cc26dfef2 ...

  6. route - 显示 / 操作IP选路表

    总览 SYNOPSIS route [-CFvnee] route [-v] [-A family] add [-net|-host] target [netmask Nm] [gw Gw] [met ...

  7. 98-基于FPGA Spartan6 的双路光纤PCIe采集卡(2路光纤卡)

    基于FPGA Spartan6 的双路光纤PCIe采集卡(2路光纤卡) 1.板卡概述  板卡采用xilinx Spartan6系列芯片,支持 PCI Express Base Specificatio ...

  8. 服务器处理 json 数据

    今天做小程序后端,需要处理 json 数据,我用的 express 框架,无法直接处理,需要进行 json 提取,网上找了一堆,发现json 四种解析格式,在此记录一下 www-form-urlenc ...

  9. shortcut to add throws declaration in Intellij Idea

    When a piece of code needs error handling, IntelliJ underlines it with red. Set your pointer on that ...

  10. 三、Angular项目,app.module.ts解析

    1. 项目主要文件存放的路径 2.app.module.ts模块解析 3.模块和组件关系 |--app.module.ts(模块) |--app.component.ts(组件)  |--app.co ...