/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 11ms 28.625 MiB
#2 Accepted 23ms 29.207 MiB
#3 Accepted 31ms 29.445 MiB
#4 Accepted 30ms 29.02 MiB
#5 Accepted 32ms 29.355 MiB
#6 Accepted 46ms 29.059 MiB
#7 Wrong Answer 58ms 30.602 MiB
#8 Wrong Answer 62ms 30.27 MiB

Code

/**
 *    author:   Binoy Barman
 *    created:  2024-12-10 15:28:18
**/

#include<bits/stdc++.h>
using namespace std;
#define nl '\n'
#define all(v) v.begin(), v.end()
#define Too_Many_Jobs int tts, tc = 1; cin >> tts; hell: while(tts--)
#define Dark_Lord_Binoy ios_base::sync_with_stdio(false); cin.tie(NULL);

#ifdef LOCAL
#include "debug/whereisit.hpp"
#else
#define dbg(...) 42
#endif
#define int long long

const int N = 1e5 + 9, inf = 1e9;

int a[N];
struct SparseTable {
    int t[N][18][2], logs[N];
    SparseTable() {
        memset(t, 0, sizeof t);
        memset(logs, 0, sizeof logs);
        for (int i = 2; i < N; i++) logs[i] = logs[i >> 1] + 1;
    }
    void build(int n) { // O(nlogn)
        for(int i = 1; i <= n; i++) t[i][0][0] = t[i][0][1] = a[i];
        for(int k = 1; k < 18; k++) {
            for(int i = 1; i + (1 << k) - 1 <= n; i++) {
                t[i][k][0] = min(t[i][k - 1][0], t[i + (1 << (k - 1))][k - 1][0]);
                t[i][k][1] = max(t[i][k - 1][1], t[i + (1 << (k - 1))][k - 1][1]);
            }
        }
    }
    int minQuery(int l, int r) { // O(1)
        // int k = 31 - __builtin_clz(r - l + 1);
        if(l > r) return inf;
        int k = logs[r - l + 1];
        return min(t[l][k][0], t[r - (1 << k) + 1][k][0]);
    }
    int maxQuery(int l, int r) { // O(1)
        if(l > r) return -inf;
        int k = logs[r - l + 1];
        return max(t[l][k][1], t[r - (1 << k) + 1][k][1]);
    }
}st;

int32_t main() {
Dark_Lord_Binoy
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    Too_Many_Jobs {
        int n, k;
        cin >> n >> k;
        vector<int> ps(n + 1);
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            ps[i] = ps[i - 1] + a[i];
        }
        st.build(n);
        int ans = inf;
        for (int i = 1; i + k - 1 <= n; i++) {
            int sum = ps[i + k - 1] - ps[i - 1];
            int mn = st.minQuery(1, i - 1);
            mn = min(mn, st.minQuery(i + k, n));
            int mx = st.maxQuery(i, i + k - 1);
            sum = min(sum, sum - mx + mn);
            ans = min(sum, ans);
        }
        cout << ans << nl;
    }
    
    return 0;
}

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:37:57
Judged At
2024-12-10 09:37:57
Judged By
Score
50
Total Time
62ms
Peak Memory
30.602 MiB