当前位置:网站首页>牛客月赛-环上食虫
牛客月赛-环上食虫
2022-06-21 20:47:00 【山中一扶苏】
原题链接
题目描述
牛牛参加了牛妹的派对。
牛牛面前有一个圆桌,圆桌边缘按顺序摆上了 n 个蛋糕(第一个蛋糕和第 n 个蛋糕相邻)。
每个蛋糕都有一个饱腹值和奶油含量。
牛牛不喜欢吃奶油,所以他想要在保证自己能吃饱(所吃蛋糕的饱腹度的和大于等于 s)的情况下,所选择的蛋糕中奶油含量最大的那一个的奶油含量越低越好。我们知道,牛牛一直都是个绅士。所以他选择的蛋糕应该是相邻的(也就是对应圆上的一段弧(也可以是整个圆))。
现在它想请你帮它计算在能够吃饱的情况下,他吃到蛋糕中奶油含量最高的那一个最低会是多少?
输入样例
5 9
4 3 7 6 1
1 3 9 2 5
输出样例
5
算法
(双指针 + 环问题 + 求区间最大值)
本题由于每次枚举的是一个区间的左边界 l 与右边界 r,而从左边界到右边界的整个区间是只要满足饱腹度之和大于等于 s 即可且r 必定在 l 之后,所以每次随着 l 的移动 r 的枚举是不断递增的,不用重新从l + 1 开始枚举,所以主要应用双指针算法。
双指针:注意其实饱腹度>=s的情况下,区间越短越好(注意边界r的问题)。
环问题:复制原数组,判断区间确实对应环上的一段。
求区间最大值: 用multiset(注意multiset.end()指向的是最后一位的下一位地址)。
C++ 代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;
const int N = 4e5 + 10;
ll n,s;
ll w[N],val[N];
int main()
{
cin >> n >> s;
for (int i = 1;i <= n;i ++ ) {
cin >> w[i];
w[i + n] = w[i];
}
for (int i = 1;i <= n;i ++ ) {
cin >> val[i];
val[i + n] = val[i];
}
multiset<ll> st;
ll res =1e10,sum = 0;
bool ok = false;
for (ll l = 1,r = 0;l <= n;l ++ ) {//注意r加到哪里放到哪里
while (sum < s && r < 2 * n) {
r ++;
sum += w[r];
st.insert(val[r]);//也可以st.insert(-val[r]);
}
if (sum >= s && r - l + 1 <= n) {
ok = true;
res = min(res,*(--st.end()));//也可以res = min(res,-(*st.begin()))
}
sum -= w[l];
st.erase(st.find(val[l]));//也可以st.earse(st.find(-val[l]))
}
if (!ok) puts("-1");
else cout << res << '\n';
return 0;
}
边栏推荐
- Sampler collection
- 利用while循环,分别计算1-100中奇数的和、偶数的和【方法一】
- 刷题笔记(十七)--二叉搜索树:关于属性问题
- WPF thread manipulation UI problem
- HIC Pro | HIC data processing tool
- Summary of Li Kou brush questions 4 (MySQL version)
- KVM虚拟机救援模式修改root密码 —— 筑梦之路
- FPGA设计中RAM和ROM初始化的方法
- GDB debugging practice (7) signal processing
- promise错误捕获处理——Promisifying技术
猜你喜欢
![[leetcode] 8. String conversion integer (ATOI)](/img/e8/08986237d8945685888817f214d7a9.png)
[leetcode] 8. String conversion integer (ATOI)

Nacos安装指南

Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading

Hiclotter|hic data visualization tool

Notes on question brushing (17) -- binary search tree: about attribute problems

JMter测试命令【笔记】

Contact five heart matchmaker to take off the order

Summary of Li Kou brush questions 4 (MySQL version)

WPF 依赖属性
![[deeply understand tcapulusdb technology] tmonitor background one click installation](/img/f6/d2a287aac4ef3dfa8c75f7130202a4.png)
[deeply understand tcapulusdb technology] tmonitor background one click installation
随机推荐
关于eureka启动成功但是访问404问题
Contact five heart matchmaker to take off the order
[deeply understand tcapulusdb technology] tmonitor background one click installation
Qt滚动区域QScrollArea
[deeply understand tcapulusdb technology] one click installation of tmonitor background
电脑屏幕分辨率怎么调?电脑屏幕修改分辨率SwitchResX
Paml| Shengxin software for calculating dn/ds value
Technology sharing | a clustering incremental statistical SQL requirement in MySQL
cpu指令重排导致错误的一个例子
Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
利用do while循环,分别计算1-100中奇数的和、偶数的和【方法一】
How to uninstall a package installed with the CONDA command
Pal2nal| how to run pal2nal from the command line
UWP 阴影效果
JMter测试命令【笔记】
WPF ListBox虚拟化
更好的管理各种音乐,专业的DJ音乐管理软件Pioneer DJ rekordbox
UWP 手写板InkCanvas
Pycharm下可以正常运行,Pyinstaller打包软件报出Fatal error
利用for循环,分别计算1-100中奇数的和、偶数的和【方法一】