Submission #711505

#TimeUsernameProblemLanguageResultExecution timeMemory
711505CyanmondBubble Sort 2 (JOI18_bubblesort2)C++17
38 / 100
9068 ms2640 KiB
#include "bubblesort2.h"
#include <bits/stdc++.h>

constexpr int inf = 1 << 30;

std::vector<int> countScans(std::vector<int> A, std::vector<int> X, std::vector<int> V) {
    const int N = (int)A.size(), Q = (int)X.size();
    {
        std::vector<int> vals;
        std::copy(A.begin(), A.end(), std::back_inserter(vals));
        std::copy(V.begin(), V.end(), std::back_inserter(vals));
        std::sort(vals.begin(), vals.end());
        for (auto &e : A) e = (int)(std::lower_bound(vals.begin(), vals.end(), e) - vals.begin());
        for (auto &e : V) e = (int)(std::lower_bound(vals.begin(), vals.end(), e) - vals.begin());
    }
    // subtask 1, 2
    const int M =
        std::max(*std::max_element(A.begin(), A.end()), *std::max_element(V.begin(), V.end())) + 1;
    std::vector<int> countL(N);
    for (int i = 0; i < N; ++i) {
        for (int j = i + 1; j < N; ++j) {
            if (A[i] > A[j]) {
                ++countL[j];
            }
        }
    }

    std::vector<int> answer(Q);
    for (int q = 0; q < Q; ++q) {
        const int x = X[q], newVal = V[q];
        const int preVal = A[x];
        for (int i = x + 1; i < N; ++i) {
            if (preVal > A[i]) {
                --countL[i];
            }
        }
        A[x] = newVal;
        for (int i = x + 1; i < N; ++i) {
            if (newVal > A[i]) {
                ++countL[i];
            }
        }
        countL[x] = 0;
        for (int i = 0; i < x; ++i) {
            if (A[i] > newVal) {
                ++countL[x];
            }
        }
        answer[q] = *std::max_element(countL.begin(), countL.end());
    }
    return answer;
}

Compilation message (stderr)

bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:17:15: warning: unused variable 'M' [-Wunused-variable]
   17 |     const int M =
      |               ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...