답안 #647814

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
647814 2022-10-04T08:05:02 Z Oepeling 중앙값 배열 (balkan11_medians) C++14
100 / 100
51 ms 5024 KB
#include <iostream>
#include <vector>
#include <cassert>

using namespace std;

struct list {
    vector<bool> taken;
    vector<int> next;
    vector<int> prev;

    int min;
    int max;

    explicit list(int n) {
        next.resize(2 * n);
        prev.resize(2 * n);
        taken.resize(2 * n);
        min = 1;
        max = 2 * n - 1;
    }

    bool exists(int x) {
        return taken[x];
    }

    int insert_after(int where, int x) {
        assert(x > 0 && x < taken.size());
        assert(!taken[x]);
        assert(where == 0 || taken[where]);

        next[x] = next[where];
        prev[x] = where;
        prev[next[x]] = x;
        next[where] = x;
        taken[x] = true;
        return x;
    }

    int insert_before(int where, int x) {
        assert(x > 0 && x < taken.size());
        assert(!taken[x]);
        assert(where == 0 || taken[where]);

        prev[x] = prev[where];
        next[x] = where;
        next[prev[x]] = x;
        prev[where] = x;
        taken[x] = true;
        return x;
    }

    int insert_min() {
        while (min < taken.size() && taken[min]) { min++; }
        return insert_after(min - 1, min);
    }

    int insert_max() {
        while (max > 0 && taken[max]) { max--; }
//        cerr << max << endl;
        return insert_before((max + 1) % taken.size(), max);
    }
};

int main() {
    int n; cin >> n;
    vector<int> input(n);
    for (int i = 0; i < n; i++) {
        cin >> input[i];
    }

    list list(n);
    int median = input[0];
    vector<int> ans;
    ans.push_back(list.insert_after(0, median));

    for (int i = 1; i < n; i++) {
        if (input[i] == median) {
            ans.push_back(list.insert_min());
            ans.push_back(list.insert_max());
        } else if (input[i] > median) {
            if (list.exists(input[i])) {
                ans.push_back(list.insert_max());
            } else {
                ans.push_back(list.insert_after(median, input[i]));
            }
            ans.push_back(list.insert_max());
//            cerr << input[i] << " " << ans.back() << endl;
        } else {
            if (list.exists(input[i])) {
                ans.push_back(list.insert_min());
            } else {
                ans.push_back(list.insert_before(median, input[i]));
            }
            ans.push_back(list.insert_min());
        }
        median = input[i];
    }

    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << " ";
    }
    cout << "\n";

    return 0;
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from medians.cpp:3:
medians.cpp: In member function 'int list::insert_after(int, int)':
medians.cpp:28:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<bool>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |         assert(x > 0 && x < taken.size());
      |                         ~~^~~~~~~~~~~~~~
medians.cpp: In member function 'int list::insert_before(int, int)':
medians.cpp:41:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<bool>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |         assert(x > 0 && x < taken.size());
      |                         ~~^~~~~~~~~~~~~~
medians.cpp: In member function 'int list::insert_min()':
medians.cpp:54:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<bool>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |         while (min < taken.size() && taken[min]) { min++; }
      |                ~~~~^~~~~~~~~~~~~~
medians.cpp: In function 'int main()':
medians.cpp:100:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |     for (int i = 0; i < ans.size(); i++) {
      |                     ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 304 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 300 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 4 ms 628 KB Output is correct
4 Correct 8 ms 1108 KB Output is correct
5 Correct 18 ms 1768 KB Output is correct
6 Correct 34 ms 3348 KB Output is correct
7 Correct 51 ms 5024 KB Output is correct