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 pathTaskSchedule.cpp
99 lines (89 loc) · 2.67 KB
/
TaskSchedule.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
//
// 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_TaskSchedule
#endif
#ifndef FUNC_TaskSchedule
#define FUNC_TaskSchedule
#include "utils.h"
#include "Greedy.cpp"
template <typename T>
class Task {
public:
Task(size_t i, size_t ddl, T pun): id(i), d(ddl), w(pun) {}
bool operator<(const Task<T> rhs) const {
if (d != rhs.d)
return d < rhs.d;
if (w != rhs.w)
return w > rhs.w;
return id < rhs.id;
}
bool operator==(const Task<T> rhs) const { return id == rhs.id; }
friend std::ostream& operator<<(std::ostream& os, const Task<T> rhs) {
return os << rhs.id << ',' << rhs.d << ',' << rhs.w;
}
static T get_p(Task<T> x) { return x.w; }
size_t id;
size_t d;
T w;
};
template <typename T>
class TaskMatroid: public Matroid<Task<T>> {
public:
TaskMatroid(std::set<Task<T>> s): Matroid<Task<T>>(s) {}
virtual bool I(const std::set<Task<T>>& A) const {
size_t t = 0;
for (auto i = A.begin(); i != A.end(); i++)
if (i->d <= t++)
return false;
return true;
}
};
template <typename T>
void TaskSchedule(const TaskMatroid<T>& M, std::vector<Task<T>>& ans) {
std::set<Task<T>> A;
Greedy(M, Task<T>::get_p, A);
for (auto i = A.begin(); i != A.end(); i++)
ans.push_back(*i);
for (auto i = M.S.begin(); i != M.S.end(); i++)
if (A.find(*i) == A.end())
ans.push_back(*i);
}
#endif
#ifdef MAIN_TaskSchedule
template <typename T>
T f(Task<T> x) { return x.p; }
int main(int argc, char *argv[]) {
const size_t n = get_argv(argc, argv, 1, 7);
// prepare data
std::set<Task<size_t>> S;
std::vector<size_t> d, w;
random_integers<size_t>(d, 1, n, n);
random_integers<size_t>(w, 1, n, n);
for (size_t i = 0; i < n; i++)
S.insert(Task<size_t>(i, d[i], w[i]));
TaskMatroid<size_t> M(S);
output_integers(M.S, "\t");
// call function
std::vector<Task<size_t>> ans;
TaskSchedule(M, ans);
output_integers(ans, "\t");
return 0;
}
#endif