当前位置:网站首页>B. Luke is a Foodie(贪心/模拟)
B. Luke is a Foodie(贪心/模拟)
2022-08-06 09:18:00 【罗gkv】
题意
给定n堆食物,每堆食物有美味度a[i],吃第i堆食物,要求当前的品尝者的品尝度v,与它的a[i]绝对值不能超过x,即|a[i]-v|<=x。
品尝者从左到右去吃这些食物,问他最少需要调整多少次v,才能吃完所有食物。
第一设定的v可以取任意值,且不做为调整的次数。
思路
从左到右,模拟去取每个食物可以接受美味度的左右边界。
- 如果吃不了的,就只能新开一个v,次数+1。
- 如果吃得了的,则更新,当前食物集合的左右边界的 交集。
详见代码。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int n, x;
int a[maxn];
void solve() {
scanf("%d%d", &n, &x);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
int count = 0, l = a[0] - x, r = a[0] + x;
for (int i = 1; i < n; ++i) {
int tmpl = a[i] - x, tmpr = a[i] + x;
if (r < tmpl || tmpr < l) {
++count;
l = tmpl;
r = tmpr;
} else {
l = max(l, tmpl);
r = min(r, tmpr);
}
}
printf("%d\n", count);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
}
最后
weixin gongzhonghao搜 对方正在debug,关注下,一起快乐刷题吧~
边栏推荐
- Day 17 (16 day bpdus related knowledge and STP configuration)
- 虹科分享|如何保障医疗数据安全?移动目标防御技术给你满意的答案
- ROS报错[rospack] Error: package ‘.....‘ not found
- [Mathematical Modeling] Linear Programming
- LeetCode - 1047. Remove all adjacent duplicates in a string
- 交换综合实验(待补充)
- 【保姆级教程】腾讯云如何获取secretId和secretKey,以及开通人脸服务
- 亚马逊员工教你打造DIY版Echo
- HCIP第十八天笔记
- rain cloud animation
猜你喜欢

prometheus学习笔记(1)

石原子科技正式加入openGauss社区

DO280管理和监控OpenShift平台--使用probes监视应用

软件测试八款优秀的API安全测试工具,会用三款工作效率能提升50%

创建一个 Dapp,为什么要选择波卡?

nuxt页面访问速度优化

View the Linux log on the web side, and view the Linux log on the web side

【保姆级教程】腾讯云如何获取secretId和secretKey,以及开通人脸服务

【机器学习】决策树

Exchange comprehensive experiment (to be supplemented)
随机推荐
卡片懸停毛玻璃效果
pytorch中 torch.utils.data的用法 ----加载数据篇
BigEvent Demo
go去除字符串的换行符与空格
ACM常用头文件
剑指 Offer 15. 二进制中1的个数,位运算,与运算
From "prairie cattle" to "digital cattle": Mengniu's digital transformation!
Two important self-learning functions in pytorch dir(); help()
prometheus学习笔记(1)
【Untitled】
第十九章 自动化理论
Timed task appears A component required a bean named ‘xxx‘ that could not be found
数组里的值放到另一个数组中,并转大写
Day 17 (16 day bpdus related knowledge and STP configuration)
虹科分享|如何保障医疗数据安全?移动目标防御技术给你满意的答案
交换综合实验(待补充)
【FirmAE论文节译】FirmAE: Towards Large-Scale Emulation of IoT Firmware for Dynamic Analysis
Folyd
读书笔记(二)——《被讨厌的勇气》
ELT.zip 】 【 OpenHarmony chew club - the methodology of academic research paper precipitation series