1 solutions
-
1
40pts
#include<bits/stdc++.h> using namespace std; const int N=4e5+10; typedef long long LL; LL a[N],s[N]; int main() { int n; cin>>n; for(int i=1;i<=n;i++) { cin>>a[i]; a[i+n]=a[i]; } for(int i=1;i<=2*n;i++) { s[i]=s[i-1]+a[i]; } LL ans=-2e18; for(int i=1;i<=n;i++) { for(int j=i;j<=2*n;j++) { int len=j-i+1; if(len>n) continue; ans=max(ans,s[j]-s[i-1]); } } cout<<ans; }100pts
#include<bits/stdc++.h> using namespace std; typedef long long LL; const int N=4e5+10; LL w[N],s[N]; int q[N]; int main() { int n; cin>>n; for(int i=1;i<=n;i++) { cin>>w[i]; w[i+n]=w[i]; } for(int i=1;i<=2*n;i++) { s[i]=s[i-1]+w[i]; } LL ans=-1e18; int hh=0,tt=-1; //队头,队尾 for(int i=1;i<=2*n;i++) { while(hh<=tt&&i-q[hh]+1>n+1) hh++; //当前区间元素过多 ans=max(ans,s[i]-s[q[hh]]);//更新区间最值 while(hh<=tt&&s[i]<s[q[tt]]) tt--; //去掉较大值 q[++tt]=i; } cout<<ans; return 0; }
- 1
Information
- ID
- 2570
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 5
- Tags
- # Submissions
- 35
- Accepted
- 3
- Uploaded By