求助

Gavin 2021-07-17 19:01:13 6

求助!!!求答案!!!

{{ vote && vote.total.up }}

共 4 条回复

yehongchen

#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; }

root 站长

👍

Yang_

使用前缀和,然后枚举差分,求差分的最大值.sum[i + 2 * R + 1] - sum[i]

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int sum[1000010];

int main() {
    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;
}
furuozhan

#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]; }

for(int i=1;i<=n;i++){
	ans=max(ans,f(max(1,i-r),min(n,i+r)));
}
cout<<ans;
return 0;

}