当前位置:网站首页>UVALive – 4621 Cav 贪心 + 分析「建议收藏」
UVALive – 4621 Cav 贪心 + 分析「建议收藏」
2022-07-07 16:53:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
题目大意:有一张洞穴地图,要在这个洞穴里面存放水,要求水不能碰到洞穴顶部。如今给出每一个位置的顶部位置和地面高度。问最多能够放多少水
解题思路:根据物理定理,每一段有水的连续区间,水位高度必须相等 所以我们能够求出在同一段连续区间內的水位高度,该水位高度等于最低洞穴顶部的高度。以此为根据,从左到右更新,再从右到左更新,就能够得到每一个位置的水位高度了
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 1000010;
const int INF = 0x3f3f3f3f;
int s[N], p[N], n;
void init() {
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &p[i]);
}
for(int i = 0; i < n; i++) {
scanf("%d", &s[i]);
}
}
int solve() {
int t = INF;
for(int i = 0; i < n; i++) {
t = min(t, s[i]);
t = max(t, p[i]);
s[i] = t;
}
t = INF;
for(int i = n - 1; i >= 0; i--) {
t = min(t, s[i]);
t = max(t, p[i]);
s[i] = t;
}
int ans = 0;
for(int i = 0; i < n; i++)
ans += s[i] - p[i];
return ans;
}
int main() {
int test;
scanf("%d", &test);
while(test--) {
init();
printf("%d\n", solve());
}
return 0;
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116603.html原文链接:https://javaforall.cn
边栏推荐
- 二叉树的基本概念和性质
- Redis cluster and expansion
- CVPR 2022 - learning non target knowledge for semantic segmentation of small samples
- 2022-07-04 matlab reads video frames and saves them
- 數據驗證框架 Apache BVal 再使用
- 卖空、加印、保库存,东方甄选居然一个月在抖音卖了266万单书
- sqlite sql 异常 near “with“: syntax error
- Datasimba launched wechat applet, and datanuza accepted the test of the whole scene| StartDT Hackathon
- 虚拟数字人里的生意经
- 企业展厅设计中常用的三种多媒体技术形式
猜你喜欢

标准ACL与扩展ACL

How to choose the appropriate automated testing tools?

CVPR 2022丨学习用于小样本语义分割的非目标知识

Save the memory of the model! Meta & UC Berkeley proposed memvit. The modeling time support is 30 times longer than the existing model, and the calculation amount is only increased by 4.5%

3.关于cookie

Differences between rip and OSPF and configuration commands

Kirk Borne的本周学习资源精选【点击标题直接下载】

Cadre de validation des données Apache bval réutilisé

Learn to make dynamic line chart in 3 minutes!

Improve application security through nonce field of play integrity API
随机推荐
Differences between rip and OSPF and configuration commands
DataSimba推出微信小程序,DataNuza接受全场景考验? | StartDT Hackathon
gsap动画库
虚拟数字人里的生意经
嵌入式C语言程序调试和宏使用的技巧
低代码助力企业数字化转型会让程序员失业?
Interview vipshop internship testing post, Tiktok internship testing post [true submission]
RIP和OSPF的区别和配置命令
我感觉被骗了,微信内测 “大小号” 功能,同一手机号可注册两个微信
Reuse of data validation framework Apache bval
Discuss | what preparations should be made before ar application is launched?
3. About cookies
简单几步教你如何看k线图图解
【C语言】字符串函数
学习open62541 --- [67] 添加自定义Enum并显示名字
I feel cheated. Wechat tests the function of "size number" internally, and two wechat can be registered with the same mobile number
PHP面试题 foreach($arr as &$value)与foreach($arr as $value)的用法
CVPR 2022 - learning non target knowledge for semantic segmentation of small samples
Hutool - lightweight DB operation solution
Some key points in the analysis of spot Silver