summaryrefslogtreecommitdiff
path: root/Year_2/Algorithms/max-heap.cc
blob: e81485eef2e2a500b3e442008573995847c7c754 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <fstream>
#include <iostream>

using namespace std;

template <class H>
class max_heap {
public:
    inline int
    compare(unsigned a, unsigned b)
    {
        return arr_[b] - arr_[a];
    }

    void
    swap(unsigned i, unsigned j)
    {
        auto t = arr_[i];
        arr_[i] = arr_[j];
        arr_[j] = t;
    }

    max_heap<H>*
    enqueue(H key)
    {
        if (heapsize_ == length_)
            return this;

        arr_[++heapsize_] = key;

        unsigned i = heapsize_;

        while (i > 1 && compare(i >> 1, i) > 0) {
            swap(i, i >> 1);
            i >>= 1;
        }

        return this;
    }

    void
    show(ostream& out)
    {
        out << count_ << ' ';
        for (unsigned i = 1; i <= heapsize_; ++i) {
            out << arr_[i] << ' ';
        }
        out << endl;
    }

    void
    heapify(unsigned i)
    {
        if (heapsize_ < 1)
            return;

        count_++;
        auto l = i << 1;
        auto r = l | 1;

        auto max = i;

        if (l <= heapsize_ && compare(l, max) < 0)
            max = l;
        if (r <= heapsize_ && compare(r, max) < 0)
            max = r;

        if (max != i) {
            swap(i, max);
            heapify(max);
        }
    }

    H
    extract()
    {
        if (heapsize_ < 1)
            return 0;

        swap(1, heapsize_);
        --heapsize_;
        heapify(1);
        return arr_[heapsize_ + 1];
    }

    ~max_heap()
    {
        delete arr_;
    }

    max_heap()
        : heapsize_ { 0 }
    {
        length_ = 200;
        arr_ = new H[length_];
    }

private:
    H* arr_;
    unsigned length_ { 0 };
    unsigned heapsize_ { 0 };
    unsigned count_ { 0 };
};

template <typename H>
void
parse(max_heap<H>* heap, char typ, ifstream& fin, short act_nums)
{
    string s;
    string v;
    for (unsigned i = 0; i < act_nums; ++i) {
        fin >> s;
        if (s.at(1) == 'x') {
            heap->extract();
        } else {
            v = s.substr(2, s.length());

            if (typ == 'i')
                heap->enqueue(stoi(v));
            else if (typ == 'd')
                heap->enqueue(stod(v));
            else if (typ == 'b')
                heap->enqueue(v == "1" ? true : false);
            else if (typ == 'c')
                heap->enqueue(v[0]);
        }
    }
}

int
main(int argc, char** argv)
{
    int n = (argc > 1) ? atoi(argv[1]) : 100;
    ifstream fin("input.txt");
    ofstream fout("output.txt");
    string typ;
    short actions;

    for (int i = 0; i < n; ++i) {
        fin >> typ;
        fin >> actions;

        if (typ == "int") {
            max_heap<int>* mh = new max_heap<int>();

            parse(mh, 'i', fin, actions);

            mh->show(fout);
            delete mh;
        } else if (typ == "double") {
            max_heap<double>* mh = new max_heap<double>();

            parse(mh, 'd', fin, actions);

            mh->show(fout);
            delete mh;
        } else if (typ == "bool") {
            max_heap<bool>* mh = new max_heap<bool>();

            parse(mh, 'b', fin, actions);

            mh->show(fout);
            delete mh;
        } else if (typ == "char") {
            max_heap<char>* mh = new max_heap<char>();

            parse(mh, 'c', fin, actions);

            mh->show(fout);
            delete mh;
        }
    }

    fin.close();
    fout.close();

    return 0;
}