【每日一题】数学考试
头一次遇到这样的前缀和,(看来是太菜了),自己一开始觉得是类似小根堆,然后发现是自己读题没读明白, 然后看了看,别人的写法, 学了学
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define iinf 0x3f3f3f3f #define linf (1ll<<60) #define eps 1e-8 #define maxn 1000010 #define maxe 1000010 #define cl(x) memset(x,0,sizeof(x)) #define rep(i,a,b) for(i=a;i<=b;i++) #define drep(i,a,b) for(i=a;i>=b;i--) #define em(x) emplace(x) #define emb(x) emplace_back(x) #define emf(x) emplace_front(x) #define fi first #define se second #define pb push_back #define de(x) cerr<<#x<<" = "<<x<<endl #define __i __int128 using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; ll read(ll x=0) { ll c, f(1); for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f; for(;isdigit(c);c=getchar())x=x*10+c-0x30; return f*x; } int n , k, m ; int cmp ( pll a, pll b){ return a.first< b.first; } ll a[200010], sum[200010], f1[200010],f2[200010]; priority_queue<ll , vector<ll > , greater<ll > > qe; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> m; while(m--){ cin >>n>>k; ll ans =-1e18; for(int i=1;i<=n;i++){ cin>>a[i]; sum[i]=a[i]+sum[i-1]; } memset(f1,-0x3f3f3f3f,sizeof f1);memset(f2,-0x3f3f3f3f,sizeof f2); for(int i=k;i<=n-k;i++) f1[i]=max(sum[i]-sum[i-k],f1[i-1]); for(int i=n-k+1;i>=k+1;i--) f2[i]=max(sum[i+k-1]-sum[i-1],f2[i+1]); for(int i=k;i<=n-k;i++) ans=max(ans,f1[i]+f2[i+1]); cout<<ans<<endl; } return 0; }