当前位置:网站首页>牛客小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;
}边栏推荐
- Visual Studio插件CodeRush正式发布v22.1——优化调试可视化工具
- Top 30 open source software
- Basic operations such as MySQL startup under Windows platform
- 让 Google 搜索到自己的博客
- mac安装php7.2
- Bottom level internal skill cultivation
- SRM系统是什么系统?如何应用SRM系统?
- Have you grasped the most frequently asked question in the interview about massive data processing?
- 关于日期相加减问题
- 设置双击运行 jar 文件
猜你喜欢

Selenium file upload method

Detailed introduction and Simulation of bitmap

ISO 32000-2 国际标准7.7

0 basic self-study STM32 (wildfire) -- use register to light LED -- Explanation of GPIO function block diagram

双亲委派机制
![Fill in the next right node pointer of each node [make good use of each point - > reduce the space-time complexity as much as possible]](/img/33/bda0a898bfe3503197026d1f62e851.png)
Fill in the next right node pointer of each node [make good use of each point - > reduce the space-time complexity as much as possible]

Maidong Internet won the bid of Dajia Insurance Group

Prevent form resubmission based on annotations and interceptors

Parental delegation mechanism

小程序容器是什么技术?能助力物联网企业红海突围?
随机推荐
2022 spring summer collection koreano essential reshapes the vitality of fashion
The soft youth under the blessing of devcloud makes education "smart" in the cloud
phpunit骚操作之静态类的部分mock
基于注解和拦截器防止表单重复提交
Does rapid software delivery really need to be at the cost of security?
最受欢迎的30款开源软件
小程序容器是什么技术?能助力物联网企业红海突围?
Analyze the implementation principle of zero copy mechanism, applicable scenarios and code implementation
How to solve MySQL 1045 error in Linux
Visual studio plug-in coderush officially released v22.1 -- visual tool for optimizing debugging
力扣每日一题 06.29 两数相加
SRM系统是什么系统?如何应用SRM系统?
Web Scraping with Beautiful Soup for Data Scientist
Does MySQL support foreign keys
Younger sister Juan takes you to learn JDBC - 2-day dash Day1
剑桥大学教授:经常吃早餐害处多,很危险 - 知乎
3h精通OpenCV(六)-图像堆叠
Sword finger offer 13 Robot range of motion (BFS)
Opencv+yolo-v3 for target tracking
上班可以做副业