/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 15ms 472.0 KiB
#3 Wrong Answer 23ms 800.0 KiB

Code

#include<bits/stdc++.h>
#define ll long long int
#define endl '\n'
#define print(v) for(auto data : v) cout << data << " "; cout << endl;
using namespace std;
void solve()
{
    int n, k; cin >> n >> k;
    vector<ll> v(n); for(auto &data : v) cin >> data;
    // print(v);
    int l = 0, r = 0;
    ll sum = 0, mn = 1e9;
    int L, R;
    
    while (r < n)
    {
        sum += v[r];
        if(r-l+1 == k)
        {
            if(sum < mn)
            {
                mn = min(mn, sum);
                L = l, R = r;
            }
            sum -= v[l]; l++;
        } 
        r++;
    }
    ll mx = -1e9;
    for (int i = L; i <= R; i++)
    {
        mx = max(mx, v[i]);
    }
    ll min_val = mx;
    for (int i = 0; i < n; i++)
    {
        if(i >= L && i <= R) continue;
        min_val = min(min_val, v[i]);
    }
    if(mx > min_val) 
    {
        mn -= mx;
        mn += min_val;
    }
    cout << mn << endl;
    // cout << L << " " << R  << " -> " << mn << endl;
}
int main ()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int t; cin >> t;
    // int t = 1;
    while (t--) solve();
    
    return  0;
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Contest
LU IUJPC : Sylhet Division 2024
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-09 05:55:14
Judged At
2024-12-09 05:55:14
Judged By
Score
1
Total Time
23ms
Peak Memory
800.0 KiB