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 pathHireAssistant.cpp
75 lines (68 loc) · 2.27 KB
/
HireAssistant.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
//
// 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_HireAssistant
#endif
#ifndef FUNC_HireAssistant
#define FUNC_HireAssistant
#include <cassert>
#include "utils.h"
template <typename T>
class Candidate {
public:
Candidate(T a, T c_i, T c_h, bool i_ed): abl(a),ci(c_i),ch(c_h),ied(i_ed) {}
T ability(void) const { assert(ied); return abl; }
void interview(T& tc) { ied = true; tc += ci; }
void hire(T& tc) const { tc += ch; }
private:
T abl, ci, ch;
bool ied;
};
template <typename T>
T HireAssistant(std::vector<Candidate<T>*> candidates) {
typename std::vector<Candidate<T>*>::iterator i = candidates.begin();
Candidate<T>* best = *i;
T total_cost = 0;
for (i++; i != candidates.end(); i++) {
(*i)->interview(total_cost);
if ((*i)->ability() > best->ability()) {
best = *i;
best->hire(total_cost);
}
}
return total_cost;
}
#endif
#ifdef MAIN_HireAssistant
int main(int argc, char *argv[]) {
int n = get_argv(argc, argv, 1, 10);
int ci = get_argv(argc, argv, 2, 1);
int ch = get_argv(argc, argv, 3, 10);
std::vector<int> abilities;
Candidate<int>* dummy_candidate = new Candidate<int>(-1, ci, ch, true);
std::vector<Candidate<int>*> candidates(1, dummy_candidate);
random_integers(abilities, 0, n - 1, n);
candidates.reserve(n);
for (auto i = abilities.begin(); i != abilities.end(); i++)
candidates.push_back(new Candidate<int>(*i, ci, ch, false));
output_integers(abilities);
std::cout << HireAssistant(candidates) << std::endl;
return 0;
}
#endif