当前位置:网站首页>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
边栏推荐
- Complete e-commerce system
- 云景网络科技面试题【杭州多测师】【杭州多测师_王sir】
- 面试唯品会实习测试岗、抖音实习测试岗【真实投稿】
- I feel cheated. Wechat tests the function of "size number" internally, and two wechat can be registered with the same mobile number
- 磁盘存储链式的B树与B+树
- CVPR 2022 - learning non target knowledge for semantic segmentation of small samples
- 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%
- How to choose the appropriate automated testing tools?
- Redis的发布与订阅
- 持续测试(CT)实战经验分享
猜你喜欢
磁盘存储链式的B树与B+树
Industry case | digital operation base helps the transformation of life insurance industry
清华、剑桥、UIC联合推出首个中文事实核查数据集:基于证据、涵盖医疗社会等多个领域
伺服力矩控制模式下的力矩目标值(fTorque)计算
Short selling, overprinting and stock keeping, Oriental selection actually sold 2.66 million books in Tiktok in one month
Will low code help enterprises' digital transformation make programmers unemployed?
AntiSamy:防 XSS 攻击的一种解决方案使用教程
Comparison and selection of kubernetes Devops CD Tools
Calculation of torque target value (ftorque) in servo torque control mode
How to clean when win11 C disk is full? Win11 method of cleaning C disk
随机推荐
静态路由配置
Comparison and selection of kubernetes Devops CD Tools
A few simple steps to teach you how to see the K-line diagram
基于图像和激光的多模态点云融合与视觉定位
低代码助力企业数字化转型会让程序员失业?
虚拟数字人里的生意经
海量数据去重的hash,bitmap与布隆过滤器Bloom Filter
IP netns command (memo)
3. About cookies
What is the general yield of financial products in 2022?
NAT地址转换
行业案例|数字化经营底座助力寿险行业转型
Three forms of multimedia technology commonly used in enterprise exhibition hall design
线程池和单例模式以及文件操作
2022-07-04 matlab读取视频帧并保存
Reject policy of thread pool
PTA 1101 B是A的多少倍
Wireshark analyzes packet capture data * cap
Desci: is decentralized science the new trend of Web3.0?
线程池中的线程工厂