社区讨论
求助,环形最大子段和
学术版参与者 4已保存回复 8
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 8 条
- 当前快照
- 1 份
- 快照标识符
- @lobk841x
- 此快照首次捕获于
- 2023/10/29 22:22 2 年前
- 此快照最后确认于
- 2023/11/04 03:21 2 年前
rt,给的数组是环形的,wa了一个点,n,a[i]<=1e6
CPP#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1000006;
ll n,a[N*2],f[N*2],sum,ans;
deque<pair<int,int> >q;
int main(){
freopen("subsegment.in","r",stdin);
freopen("subsegment.out","w",stdout);
cin>>n;
for (int i=1;i<=n;i++)
cin>>a[i],a[i+n]=a[i];
n*=2;
for (int i=1;i<=n;i++){
while(!q.empty()&&q.front().first<=i-(n/2)) sum-=q.front().second,q.pop_front();
if (sum+a[i]<=0){
f[i]=0;
sum=0;
q.clear();
}
else{
sum+=a[i];
q.push_back(make_pair(i,a[i]));
f[i]=sum;
}
}
for (int i=1;i<=n;i++)
ans=max(f[i],ans);
cout<<ans<<endl;
return 0;
}
回复
共 8 条回复,欢迎继续交流。
正在加载回复...