当前位置:网站首页>牛客小Bai月赛52 D 环上食虫(尺取+st表)
牛客小Bai月赛52 D 环上食虫(尺取+st表)
2022-06-29 17:47:00 【eva_can(not)survive】
登录—专业IT笔试面试备考平台_牛客网牛客网是互联网求职神器,C++、Java、前端、产品、运营技能学习/备考/求职题库,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的技术能力
https://ac.nowcoder.com/acm/contest/11229/D很明显题目说明是一个环,所以数组开两倍,然后通过双指针尺取区间,用st表预处理掉奶油的区间最大,尺取的时候需要注意边界,r-l需要小于等于n,要不然就会出现吃空气的情况从而使答案出现错误。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#define lowbit(x) ((x)&(-x))
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
const int MAXN=1e6+5;
const int INF=0x3f3f3f3f;
const ll NNF=0x3f3f3f3f3f3f3f3f;
int n;
ll s;
ll a[MAXN];
ll st[MAXN][24];
ll query(int x,int y){
int s=log2(y-x+1);
return max(st[x][s],st[y-(1<<s)+1][s]);
}
void solve(){
scanf("%d %lld",&n,&s);
for(int i=1;i<=n;i++) scanf("%lld",a+i);
for(int i=1;i<=n;i++) scanf("%lld",&st[i][0]);
for(int i=n+1;i<=2*n;i++){
a[i]=a[i-n];
// b[i]=b[i-n];
st[i][0]=st[i-n][0];
}
for(int i=1;i<=23;i++){
for(int j=1;j+(1<<i)-1<=2*n;j++){
st[j][i]=max(st[j][i-1],st[j+(1<<(i-1))][i-1]);
}
}
int l,r;
l=r=1;
ll sum=0;
// int mx=0;
ll ans=INF;
while(l<=r){
while(r<=2*n&&sum<s){
// mx=max(mx,a[r]);
sum+=a[r++];
}
while(r-l>n){
sum-=a[l++];
}
if(sum>=s){
ll mx=query(l,r-1);
ans=min(ans,mx);
}else if(r>2*n) break;
if(l<=r){
sum-=a[l++];
}
}
if(ans==INF) printf("-1");
else printf("%lld",ans);
}
int main(){
solve();
return 0;
}边栏推荐
- 0 basic self-study STM32 (wildfire) -- use register to light LED -- Explanation of GPIO function block diagram
- Opencv+yolo-v3 for target tracking
- Professor of Cambridge University: eating breakfast often is harmful and dangerous. - you know what
- Detailed introduction and Simulation of bitmap
- Cross border independent station language Unicode to Hebrew
- Test dble split function execution + import time-consuming shell script reference
- Opencv+YOLO-V3实现目标跟踪
- Mongotemplate - distinct use
- 基于STM32F103ZET6库函数串口实验
- What technology is an applet container? Can it help Internet of things enterprises break through the red sea?
猜你喜欢
随机推荐
Have you grasped the most frequently asked question in the interview about massive data processing?
分布式 | 几步快速拥有读写分离
R语言将距离矩阵输入给hclust函数进行层次聚类分析,method参数指定两个组合数据点间的距离计算方式、plot函数可视化层次聚类的树状图(dendrogram)
Custom handlerinterceptor interceptor for user authentication
Cross border independent station language Unicode to Hebrew
Visual studio plug-in coderush officially released v22.1 -- visual tool for optimizing debugging
Createstore for Redux source code analysis
Graduation season | Huawei experts teach interview tips: how to get a high salary offer from a large factory?
Parental delegation mechanism
Premature end of script headers 或 End of script output before headers
第42期:MySQL 是否有必要多列分区
跨境独立站语言unicode转希伯来语
3h精通OpenCV(八)-形状检测
The R language uses the KAP function (kap.2.raters function) of epidisplay package to calculate the value of kappa statistics (total consistency, expected consistency), analyze the consistency of the
2022春夏系列 KOREANO ESSENTIAL重塑时装生命力
Visio标注、批注位置
MaxCompute字符串替换函数-replace
What is the MySQL query view command
面试中问最常问的海量数据处理你拿捏了没?
What technology is an applet container? Can it help Internet of things enterprises break through the red sea?









