当前位置:网站首页>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
边栏推荐
- Discuss | frankly, why is it difficult to implement industrial AR applications?
- PHP面试题 foreach($arr as &$value)与foreach($arr as $value)的用法
- AntiSamy:防 XSS 攻击的一种解决方案使用教程
- Redis集群与扩展
- Kirk borne's selection of learning resources this week [click the title to download directly]
- 【Unity Shader】插入Pass实现模型遮挡X光透视效果
- Thread factory in thread pool
- 清华、剑桥、UIC联合推出首个中文事实核查数据集:基于证据、涵盖医疗社会等多个领域
- 如何给“不卖笔”的晨光估值?
- 【塔望方法论】塔望3W消费战略 - U&A研究法
猜你喜欢

Discuss | frankly, why is it difficult to implement industrial AR applications?

Redis

Basic concepts and properties of binary tree

我感觉被骗了,微信内测 “大小号” 功能,同一手机号可注册两个微信

50亿,福建又诞生一只母基金

AntiSamy:防 XSS 攻击的一种解决方案使用教程

磁盘存储链式的B树与B+树

The live broadcast reservation channel is open! Unlock the secret of fast launching of audio and video applications

Differences between rip and OSPF and configuration commands

持续测试(CT)实战经验分享
随机推荐
微信网页调试8.0.19换掉X5内核,改用xweb,所以x5调试方式已经不能用了,现在有了解决方案
Continuous test (CT) practical experience sharing
磁盘存储链式的B树与B+树
基于图像和激光的多模态点云融合与视觉定位
Complete e-commerce system
现货白银分析中的一些要点
小程序中实现付款功能
Discuss | frankly, why is it difficult to implement industrial AR applications?
Cloud security daily 220707: Cisco Expressway series and telepresence video communication server have found remote attack vulnerabilities and need to be upgraded as soon as possible
Embedded interview questions (algorithm part)
Will domestic software testing be biased
行业案例|数字化经营底座助力寿险行业转型
云景网络科技面试题【杭州多测师】【杭州多测师_王sir】
来了!GaussDB(for Cassandra)新特性亮相
如何给“不卖笔”的晨光估值?
小试牛刀之NunJucks模板引擎
【demo】循环队列及条件锁实现goroutine间的通信
App capture of charles+drony
Reuse of data validation framework Apache bval
The live broadcast reservation channel is open! Unlock the secret of fast launching of audio and video applications