当前位置:网站首页>牛客小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;
}边栏推荐
- 位图的详细介绍及模拟实现
- R language uses user-defined functions to write deep learning leaky relu activation functions and visualize leaky relu activation functions
- 与爱同行,育润走进贫困家庭,助推公益事业
- R语言dplyr包filter函数通过组合逻辑(与逻辑)过滤dataframe数据中的数据、其中一个字段的内容等于指定向量中的其中一个,并且另外一个字段值大于某一阈值
- Sword finger offer 13 Robot range of motion (BFS)
- Timer interrupt experiment based on stm32f103zet6 library function
- Visio标注、批注位置
- Force deduction daily question 06.29 add two numbers
- /usr/bin/ld: warning: **libmysqlclient.so.20**, needed by //usr/
- Let's start with a bug that was cheated by the app store
猜你喜欢
![[webdriver] upload files using AutoIT](/img/69/8c27626d515976b47f1df4831d09c8.png)
[webdriver] upload files using AutoIT

2022 spring summer collection koreano essential reshapes the vitality of fashion

Openfeign use step polling strategy and weight log4j configuration of openfeign interceptor

0 basic self-study STM32 (wildfire) - register lit LED

分布式 | 几步快速拥有读写分离

Let's start with a bug that was cheated by the app store

2022春夏系列 KOREANO ESSENTIAL重塑时装生命力

Face recognition 4- research on Baidu commercial solutions

Maidong Internet won the bid of Dajia Insurance Group

SRM supplier collaborative management system function introduction
随机推荐
phpunit骚操作之静态类的部分mock
The dplyr package filter function of R language filters the data in dataframe data through combinatorial logic (and logic). The content of one field is equal to one of the specified vectors, and the v
Let's start with a bug that was cheated by the app store
On adding and subtracting dates
R语言将距离矩阵输入给hclust函数进行层次聚类分析,method参数指定两个组合数据点间的距离计算方式、plot函数可视化层次聚类的树状图(dendrogram)
VB.Net读写NFC Ntag标签源码
Bottom level internal skill cultivation
ISO 32000-2 international standard 7.7
Opencv+YOLO-V3实现目标跟踪
剑桥大学教授:经常吃早餐害处多,很危险 - 知乎
What is the MySQL query view command
Have you grasped the most frequently asked question in the interview about massive data processing?
Parental delegation mechanism
reflex
Uploading files using AutoIT
Self taught structure (small turtle C language)
VB. Net read / write NFC ntag tag source code
Opencv+yolo-v3 for target tracking
selenium上传文件
Mongotemplate - distinct use