Submission #37172703


Source Code Expand

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll,int>;
#define chmax(x,y) x = max(x,y);
#define chmin(x,y) x = min(x,y);
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const double PI = acos(-1);



int main() {
    ll n, m, k;
    cin >> n >> m >> k;
    vector<ll> a(n);
    rep(i,n) cin >> a[i];
    priority_queue<P> q1;
    priority_queue<P, vector<P>, greater<P>> q2;
    map<P, bool> inq1, valid; 
    ll nowsum = 0;
    rep(i,m) {
        valid[P(a[i], -i)] = true;
        if (i < k) {
            q1.push(P(a[i], -i));
            inq1[P(a[i], -i)] = true;
            nowsum += a[i];
        } else {
            P nowmax = q1.top();
            if (P(a[i], -i) < nowmax) {

                q1.pop();
                inq1[nowmax] = false;
                nowsum -= nowmax.first;
                q2.push(nowmax);

                q1.push(P(a[i], -i));
                inq1[P(a[i], -i)] = true;
                nowsum += a[i];

            } else {
                q2.push(P(a[i], -i));
            }
        }
    }
    cout << nowsum << " ";

    rep(i,n-m) {
        // a[i] を抜く
        P pi = P(a[i], -i);
        // cout << q1.size() << " " << q2.size() << endl;
        if (inq1[pi]) {
            nowsum -= pi.first;
            inq1[pi] = false;

            while (1) {
                if (q2.empty()) break;
                P p = q2.top();
                q2.pop();
                if (!valid[p]) continue;
                q1.push(p);
                inq1[p] = true;
                nowsum += p.first;
                break;
            }
        } 
        valid[pi] = false;

        // cout << 1 << endl;

        // a[i+m] を入れる
        P pim = P(a[i+m], -(i+m));
        bool okin = false;
        while (1) {
            if (q1.empty()) {
                q1.push(pim);
                inq1[pim] = true;
                nowsum += pim.first;
                break;
            }
            P p = q1.top();
            q1.pop();
            if (!valid[p]) continue;
            if (pim < p) {

                inq1[p] = false;
                nowsum -= p.first;
                q2.push(p);

                q1.push(pim);
                inq1[pim] = true;
                nowsum += pim.first;

            } else {
                q2.push(pim);
                q1.push(p);
            }
            break;
        }
        valid[pim] = true;

        cout << nowsum << " ";
    }
    cout << endl;
    return 0;
}

Submission Info

Submission Time
Task E - Least Elements
User taki0711
Language C++ (GCC 9.2.1)
Score 500
Code Size 2775 Byte
Status AC
Exec Time 338 ms
Memory 32760 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:78:14: warning: unused variable ‘okin’ [-Wunused-variable]
   78 |         bool okin = false;
      |              ^~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 21
Set Name Test Cases
Sample 00_example_00.txt, 00_example_01.txt
All 00_example_00.txt, 00_example_01.txt, 01_max_00.txt, 01_max_01.txt, 02_min_00.txt, 03_m_small_00.txt, 04_random_00.txt, 04_random_01.txt, 04_random_02.txt, 04_random_03.txt, 04_random_04.txt, 04_random_05.txt, 04_random_06.txt, 04_random_07.txt, 04_random_08.txt, 04_random_09.txt, 04_random_10.txt, 04_random_11.txt, 04_random_12.txt, 04_random_13.txt, 04_random_14.txt
Case Name Status Exec Time Memory
00_example_00.txt AC 8 ms 3608 KiB
00_example_01.txt AC 4 ms 3552 KiB
01_max_00.txt AC 167 ms 32760 KiB
01_max_01.txt AC 318 ms 29832 KiB
02_min_00.txt AC 7 ms 3484 KiB
03_m_small_00.txt AC 338 ms 29896 KiB
04_random_00.txt AC 199 ms 19980 KiB
04_random_01.txt AC 252 ms 26928 KiB
04_random_02.txt AC 142 ms 16312 KiB
04_random_03.txt AC 310 ms 27444 KiB
04_random_04.txt AC 116 ms 15620 KiB
04_random_05.txt AC 207 ms 21512 KiB
04_random_06.txt AC 97 ms 14316 KiB
04_random_07.txt AC 244 ms 28208 KiB
04_random_08.txt AC 40 ms 7320 KiB
04_random_09.txt AC 21 ms 5428 KiB
04_random_10.txt AC 139 ms 19960 KiB
04_random_11.txt AC 86 ms 11920 KiB
04_random_12.txt AC 114 ms 12384 KiB
04_random_13.txt AC 112 ms 13256 KiB
04_random_14.txt AC 14 ms 4448 KiB