/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 584.0 KiB
#2 Accepted 70ms 604.0 KiB
#3 Accepted 127ms 828.0 KiB
#4 Accepted 124ms 540.0 KiB
#5 Accepted 143ms 588.0 KiB
#6 Accepted 214ms 1.469 MiB
#7 Accepted 149ms 5.387 MiB
#8 Accepted 204ms 5.406 MiB
#9 Accepted 313ms 5.414 MiB
#10 Accepted 279ms 5.469 MiB
#11 Accepted 317ms 5.41 MiB
#12 Accepted 310ms 5.656 MiB

Code

#ifndef COMMON_H
#define COMMON_H 1
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include <random>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define szx(x) (int)(x).size()
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
constexpr int LOG2(int x)
{
    return 32 - __builtin_clz(x) - 1;
}
#endif // COMMON_H
#ifndef DEBUG_H
#define DEBUG_H 1
#ifndef CLown1331
#define debug(...) 0
#define ASSERT(...) 0
#define dbg(...) 0
#endif
#endif // DEBUG_H
#ifndef solution_h
#define solution_h 1
namespace solution
{
const int sz = 2e5 + 105;
const int mod = 1e9 + 7;
const ll INF = 1e16;
void solve()
{
    int t;
    int n, k;
    cin >> t;
    assert(t >= 1 && t <= 10000);
    int total_n = 0;
    while (t--)
    {
        cin >> n >> k;
        assert(n >= 1 && n <= 100000);
        assert(k >= 1 && k <= n);
        total_n += n;
        vector<int> a(n);
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            assert(a[i] >= -100000 && a[i] <= 100000);
        }
        auto f = [&]() -> ll {
            multiset<int> sm, ot;
            ll sum = 0;
            ll ans = 1e16;
            for (int i = 0; i < n; i++)
            {
                sum += a[i];
                sm.insert(a[i]);
                if (i >= k)
                {
                    sum -= a[i - k];
                    sm.erase(sm.find(a[i - k]));
                    ot.insert(a[i - k]);
                    ans = min(ans, sum);
                }
                if (ot.size() > 0)
                {
                    ans = min(ans, sum + *ot.begin() - *sm.rbegin());
                }
            }
            ans = min(ans, sum);
            return ans;
        };
        ll ans = 1e16;
        ans = f();
        reverse(a.begin(), a.end());
        ans = min(ans, f());
        cout << ans << endl;
    }
    assert(total_n <= 200000);
}
} // namespace solution
#endif // solution_h
#define _CRT_SECURE_NO_WARNINGS
int main()
{
    solution::solve();
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Contest
LU Divisonal Contest Problem Testing Round
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-07 13:35:00
Judged At
2024-12-07 13:35:00
Judged By
Score
100
Total Time
317ms
Peak Memory
5.656 MiB