/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 24ms 828.0 KiB
#3 Accepted 49ms 844.0 KiB
#4 Accepted 91ms 564.0 KiB
#5 Accepted 507ms 796.0 KiB
#6 Time Exceeded ≥2093ms ≥1.258 MiB
#7 Wrong Answer 20ms 1.305 MiB

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+1); 
    for (int i = 1; i <= n; i++)
    {
        cin >> v[i];
    }
    
    int l = 1, r = 1;
    
    ll sum = 0;
    ll ans = 2e6 + 5;
    while (r <= n)
    {
        sum += v[r];
        if(r-l+1 == k)
        {
            vector<ll> lu;
            ll mx = v[l];
            ll mn = mx;
            for (int i = 1; i <= n; i++)
            {
                if(i >= l && i <= r) 
                {
                    mx = max(mx, v[i]);
                }
                else 
                {
                    lu.push_back(v[i]);
                }
            }
            ll min_ele;
            if(!lu.empty()) min_ele = *min_element(lu.begin(), lu.end());
            ll tmp = sum;
            if(!lu.empty() && mx > min_ele)
            {
                tmp -= mx; 
                tmp += min_ele;
            }
            ans = min(ans, tmp);

            sum -= v[l]; l++;
        } 
        r++;
    }
    cout << ans << endl;
}
int main ()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int t; cin >> t;
    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 09:28:37
Judged At
2024-12-09 09:28:37
Judged By
Score
40
Total Time
≥2093ms
Peak Memory
≥1.305 MiB