/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 4.527 MiB
#2 Accepted 16ms 4.812 MiB
#3 Accepted 26ms 4.602 MiB
#4 Accepted 24ms 4.57 MiB
#5 Accepted 27ms 4.527 MiB
#6 Accepted 47ms 8.871 MiB
#7 Accepted 69ms 39.895 MiB
#8 Accepted 75ms 39.52 MiB
#9 Accepted 79ms 40.141 MiB
#10 Accepted 74ms 41.961 MiB
#11 Accepted 78ms 39.785 MiB
#12 Accepted 77ms 39.703 MiB

Code

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
const int N =3e5+9;
ll t[N][22],t1[N][22],a[N];
void build(ll n) {
  for(ll i=1;i<=n;i++) t[i][0]=a[i];
  for(ll k=1;k<22;k++) {
    for(ll i=1;i+(1<<k)-1<=n;i++) {
      t[i][k]=min(t[i][k-1],t[i+(1<<(k-1))][k-1]);
    }
  }
}
ll qmn(ll l,ll r) {
  ll k=31-__builtin_clz(r-l+1);
  return min(t[l][k],t[r-(1<<k)+1][k]);
}
void build1(ll n) {
  for(ll i=1;i<=n;i++) t1[i][0]=a[i];
  for(ll k=1;k<22;k++) {
    for(ll i=1;i+(1<<k)-1<=n;i++) {
      t1[i][k]=max(t1[i][k-1],t1[i+(1<<(k-1))][k-1]);
    }
  }
}
ll qmx(ll l,ll r) {
  ll k=31-__builtin_clz(r-l+1);
  return max(t1[l][k],t1[r-(1<<k)+1][k]);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tc=1,tc1=1;
    cin>>tc;
    up:
    while(tc--){
      ll n,k;
      cin>>n>>k;
      for(ll i=1;i<=n;i++){
        cin>>a[i];
      }
      build(n);
      build1(n);
      vector<ll>pre(n+2);
      for(ll i=1;i<=n;i++){
        pre[i]=pre[i-1]+a[i];
      }
      ll mn=1e18;
      for(ll i=k;i<=n;i++){
        ll here=pre[i]-pre[i-k];
        ll hmx=qmx(i-k+1,i),hmn=1e18;
        if(i!=k){
          hmn=min(hmn,qmn(1,i-k));
        }
        if(i!=n){
          hmn=min(hmn,qmn(i+1,n));
        }
        if(hmn<hmx){
          here-=hmx,here+=hmn;
        }
        mn=min(mn,here);
      }
      cout<<mn<<'\n';
    }
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Contest
LU IUJPC : Sylhet Division 2024 Replay Contest
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-10 09:47:38
Judged At
2024-12-10 09:47:38
Judged By
Score
100
Total Time
79ms
Peak Memory
41.961 MiB