#include <bits/stdc++.h>
#include
using namespace std;
long long int a[501000], b[501000];
int main() {
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
int n, r, man = 0, h = 0;
cin >> n >> r;
for (int i = 1; i <= n; i++) {
cin >> a[i];
b[i] = b[i - 1] + a[i];
}
for (int i = r + 1; i <= n - r; i++) {
int t = b[i + r] - b[i - r - 1];
if (t > man) {
man = t;
}
}
cout << man;
return 0;
}
#include<algorithm>#include<cstdio>#include<iostream>usingnamespace std;int sum[1000010];intmain(){int n, m, R;freopen("A.in","r", stdin);freopen("A.out","w", stdout);scanf("%d%d",&n,&R);scanf("%d",&sum[0]);for(int i =1; i < n; i++){scanf("%d",&m);
sum[i]= sum[i -1]+ m;}int max_num =0;for(int i =0; i < n -2* R -1; i++){int cha = sum[i +2* R +1]- sum[i];
max_num =max(cha, max_num);}
cout << max_num;}
#include <bits/stdc++.h>
using namespace std;
int n,r,ans;
int sum[500001],a[500001];
int f(int l,int r){
return sum[r]-sum[l-1];
}
int main(){
cin>>n>>r;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
共 4 条回复
#include <bits/stdc++.h> #include using namespace std; long long int a[501000], b[501000]; int main() { freopen("A.in", "r", stdin); freopen("A.out", "w", stdout); int n, r, man = 0, h = 0; cin >> n >> r; for (int i = 1; i <= n; i++) { cin >> a[i]; b[i] = b[i - 1] + a[i]; } for (int i = r + 1; i <= n - r; i++) { int t = b[i + r] - b[i - r - 1]; if (t > man) { man = t; } } cout << man; return 0; }
👍
使用前缀和,然后枚举差分,求差分的最大值.
sum[i + 2 * R + 1] - sum[i]
#include <bits/stdc++.h> using namespace std; int n,r,ans; int sum[500001],a[500001]; int f(int l,int r){ return sum[r]-sum[l-1]; } int main(){ cin>>n>>r; for(int i=1;i<=n;i++){ scanf("%d",&a[i]); sum[i]=sum[i-1]+a[i]; }
}