-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwisp-dp.cpp
106 lines (80 loc) · 2.34 KB
/
wisp-dp.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
/*
Weighted Interval Schedule Problem
// Incio e Fim dos intervalos
1 <= begin <= end
Input: List of integers begin time task and end.
(Begin task, End Task),
...,
(Begin task, End Task)
Output: The longest interval of task such that them don't have any conflit in schedule.
tasks : Each tasks with your interval (begin, end)
p(j) : Greater indice i < j, such that the intervals i and j are disjunts.
memo() :
1. Sort intervals by ending time.
2. Calculate p
3. OPT(j) the optimal sums to {1, 2, ..., j }
if j is in the solution:
OPT(j) = v[j] + OPT(p(j))
else
OPT(j) = OPT(j-1)
source: http://www.lcad.icmc.usp.br/~jbatista/scc210/ProgDin1.pdf
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define MAX 100000
vector<pair<int, int>> tasks;
int p[MAX], memo[MAX];
int compute_opt(int j) {
if(j == 0)
return 0;
else if(memo[j])
return memo[j];
return memo[j] = max(tasks[j].second - tasks[j].first + compute_opt(p[j]), compute_opt(j - 1));
}
int iterative_opt(int numTasks) {
memo[0] = 0;
for(int i = 1; i < numTasks; ++i) {
memo[i] = max(tasks[i].second - tasks[i].first + memo[p[i]], memo[i - 1]);
}
}
bool mySort(pair<int, int> tasks1, pair<int, int> tasks2) {
if(tasks1.second < tasks2.second) {
return true;
} else if(tasks1.second > tasks2.second) {
return false;
} else if(tasks1.first <= tasks2.first) {
return true;
}
return false;
}
// O(n^n)
void calculate_p() {
for(int i = 1; i < (int)tasks.size(); ++i) {
int j = i - 1;
while(j-- >= 0) {
if(tasks[i].first > tasks[j].second) {
p[i] = j + 1;
break;
}
}
}
}
int main() {
int numTask, begin, end;
cin >> numTask;
tasks.push_back(make_pair(0, 0));
for(int i = 0; i < numTask; ++i) {
cin >> begin >> end;
tasks.push_back(make_pair(begin, end));
}
sort(tasks.begin(), tasks.end(), mySort);
calculate_p();
cout << compute_opt(numTask) << '\n';
cout << iterative_opt(numTask) << '\n';
for(int i = 0; i < (int)tasks.size(); ++i) {
cout << p[i] << ' ' << tasks[i].first << ' ' << tasks[i].second << endl;
}
return 0;
}