https://ac.nowcoder.com/acm/contest/5086/A
Generally meaning of problems: there is an array a [i], is calculated as Σa [i] * i, the subject requires a [i + k] Previous switched to position i, and a [i] to a [i + k -1] next move a unit, and recalculated, looking for the maximum sum.
Ideas: In the beginning of the direct violence, but each time plus it will time out. (And violence) and so the use of a prefix, the prefix, and to use and demand interval, that is, from a [i] to a [i + k-1] interval and, Σa and the beginning of [i] * i, then and changed and stored calculation t = max (t, ans + a [i + k] * ia [i + k] * (i + k) + sum [i + k-1] -sum [i-1])
In summary, it is a prefix and numerous visits to optimize the use, can effectively solve the complex problem of too large.
The following is the AC codes:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; long long sum[100100],a[100100]; int main(){ int t; scanf("%d",&t); while(t--){ long long n,k,ans=0; memset(sum,0,sizeof(sum)); scanf(" %lld%lld",&n,&k); for(int i=1;i<=n;i++){ scanf(" %lld",&a[i]); sum[i]+=sum[i-1]+a[i]; ans+=a[i]*i; } long long t=-1; for(int i=1;i<=n-k;i++){ t=max(t,ans+a[i+k]*i-a[i+k]*(i+k)+sum[i+k-1]-sum[i-1]); } printf("%lld\n",t); } return 0; }