-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnum_7.cpp
More file actions
138 lines (125 loc) · 2.11 KB
/
num_7.cpp
File metadata and controls
138 lines (125 loc) · 2.11 KB
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
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
class Heap
{
public:
Heap()=default;
~Heap()=default;
bool deal_min(T &result);
bool add_ele(const T& to_add);
private:
vector<T> hp;
void up_change();
void down_change(int position);
};
template<typename T>
void Heap<T>::up_change()
{
int current = hp.size()-1;
int father = 0;
int tempt = 0;
bool first_time_root = true;
do
{
father = (int)(current / 2);
if (hp.at(father) > hp.at(current))
{
tempt = hp.at(father);
hp.at(father) = hp.at(current);
hp.at(current) = tempt;
}
if (father == 0)
{
first_time_root = false;
}
current = father;
} while ((int)(current / 2) >= 0 && first_time_root);
}
template<typename T>
void Heap<T>::down_change(int position)
{
int where_now = position;
int left = where_now * 2 + 1;
int right = where_now * 2 + 2;
int smaller_son = 0;
int tempt = 0;
while (left<hp.size())
{
if (right < hp.size())
{
if (hp.at(left) < hp.at(right))
{
smaller_son = left;
}
else
{
smaller_son = right;
}
}
else
{
smaller_son = left;
}
if (hp.at(smaller_son) < hp.at(where_now))
{
tempt = hp.at(where_now);
hp.at(where_now) = hp.at(smaller_son);
hp.at(smaller_son) = tempt;//swap
}
where_now = smaller_son;
left = where_now * 2 + 1;
right = where_now * 2 + 2;
}
}
template<typename T>
bool Heap<T>::add_ele(const T& to_add)
{
hp.push_back(to_add);
up_change();
return true;
}
template<typename T>
bool Heap<T>::deal_min(T &result)
{
if (hp.empty())
{
return false;
}
else
{
result = hp.front();
hp.front() = hp.back();
hp.pop_back();
for (int i = 0; 2 * i + 1 < hp.size(); i++)
{
down_change(i);
}
return true;
}
}
int main()
{
Heap<int> target;
int num;
cin >> num;
for (int i = 0; i < num; i++)
{
int tempt;
cin >> tempt;
target.add_ele(tempt);
}
int cur_min;
int next_min;
int sum = 0;
int current_sum = 0;
while (target.deal_min(cur_min)&&target.deal_min(next_min))
{
current_sum = cur_min + next_min;
target.add_ele(current_sum);
sum += current_sum;
}
cout << sum;
return 0;
}