This repository was archived by the owner on Nov 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMinimum.cpp
136 lines (126 loc) · 3.11 KB
/
Minimum.cpp
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
//
// algorithm - some algorithms in "Introduction to Algorithms", third edition
// Copyright (C) 2018 lxylxy123456
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef MAIN
#define MAIN
#define MAIN_Minimum
#endif
#ifndef FUNC_Minimum
#define FUNC_Minimum
#include "utils.h"
class Value {
public:
Value(void) {}
Value(int v): val(v) {}
friend std::ostream& operator<<(std::ostream& os, const Value& rhs) {
return os << rhs.val;
}
bool operator==(const Value& rhs) const {
return val == rhs.val;
}
bool operator>(const Value& rhs) const {
cmp_num++;
return val > rhs.val;
}
bool operator<(const Value& rhs) const {
cmp_num++;
return val < rhs.val;
}
static int get_cmp_num(void) {
int ans = cmp_num;
cmp_num = 0;
return ans;
}
private:
int val;
static int cmp_num;
};
int Value::cmp_num = 0;
template <typename T>
T Minimum(std::vector<T>& A) {
typename std::vector<T>::iterator i = A.begin();
T min = *i;
for (i++; i != A.end(); i++)
if (min > *i)
min = *i;
return min;
}
template <typename T>
T Maximum(std::vector<T>& A) {
typename std::vector<T>::iterator i = A.begin();
T max = *i;
for (i++; i != A.end(); i++)
if (max < *i)
max = *i;
return max;
}
template <typename T>
void MinMax(std::vector<T>& A, T& min, T& max) {
typename std::vector<T>::iterator i = A.begin(), j;
if (A.size() % 2) {
min = max = *(i++);
} else {
j = i + 1;
if (*i < *j) {
min = *i;
max = *j;
} else {
min = *j;
max = *i;
}
i = j + 1;
}
while (i != A.end()) {
j = i + 1;
if (*i < *j) {
if (*i < min)
min = *i;
if (*j > max)
max = *j;
} else {
if (*j < min)
min = *j;
if (*i > max)
max = *i;
}
i = j + 1;
}
}
#endif
#ifdef MAIN_Minimum
int main(int argc, char *argv[]) {
std::vector<int> a;
int n = get_argv(argc, argv, 1, 10);
random_integers(a, 0, n - 1, n);
output_integers(a);
std::vector<Value> A;
A.reserve(n);
for (std::vector<int>::iterator i = a.begin(); i != a.end(); i++)
A.push_back(*i);
Value min1, max1, min2, max2;
min1 = Minimum(A);
std::cout << Value::get_cmp_num() << std::endl;
max1 = Maximum(A);
std::cout << Value::get_cmp_num() << std::endl;
MinMax(A, min2, max2);
std::cout << Value::get_cmp_num() << std::endl;
std::cout << min1 << '\t' << max1 << std::endl;
std::cout << min2 << '\t' << max2 << std::endl;
std::cout << std::boolalpha << (min1 == min2 && max1 == max2) << std::endl;
return 0;
}
#endif