-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayoff_sched.cpp
175 lines (146 loc) · 4.3 KB
/
payoff_sched.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Yuxiang (Ethan) Wang UCSB CS130B PROJ2 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>
using namespace std;
/*---------------------------interval structs--------------------------------*/
/* A interval has start time, finish time and payoff */
struct Interval
{
int start, finish, payoff;
};
/* A utility function prints interval */
void printinterval(Interval j)
{
cout<<j.start<<" ";
cout<<j.finish<<" ";
cout<<j.payoff<<endl;
}
/* A utility function that is used for sorting events
according to finish time */
bool IntervalComparator(Interval s1, Interval s2)
{
return (s1.finish < s2.finish);
}
/*---------------------------actual algorithms--------------------------------*/
/* This binary search function finds the lastest interval
that doesnt confict with current interval
-1 is returned for failing to find one */
int binarySearch(Interval Intervals[], int index)
{
/* Initialize 'lo' and 'hi' for Binary Search */
int lo = 0, hi = index - 1;
/* Perform binary Search iteratively */
while (lo <= hi)
{
int mid = (lo + hi) / 2;
if (Intervals[mid].finish <= Intervals[index].start)
{
if (Intervals[mid + 1].finish <= Intervals[index].start)
lo = mid + 1;
else
return mid;
}
else
hi = mid - 1;
}
return -1;
}
/* reverse the algorithm to get the actual results */
void BacktrackForSolution(Interval arr[], int resultsIndex[], int table[], int j, int &n)
{
if(j>=0)
{
int pj = binarySearch(arr, j);
if (arr[j].payoff+table[pj]>=table[j-1])
{
printinterval(arr[j]);
n++;
resultsIndex[n]=j;
cout<<"was here 1"<<endl;
BacktrackForSolution(arr, resultsIndex, table, pj, n);
}
else
{
BacktrackForSolution(arr, resultsIndex, table, j-1, n);
}
}
else return;
}
/* The main function to find the Max payoff */
void findMaxpayoff(Interval arr[], int n)
{
/* Sort intervals according to finish time */
sort(arr, arr+n, IntervalComparator);
/* Create an array to store solutions of subproblems */
int *table = new int[n];
table[0] = arr[0].payoff;
/* Fill entries in table[] using recursive property */
for (int i=1; i<n; i++)
{
/* Find payoff including the current interval */
int inclPay = arr[i].payoff;
int pjl = binarySearch(arr, i);
if (pjl != -1)
inclPay += table[pjl];
/* compare to the payoff without the current interval and then
store the maximum payoff in the table*/
table[i] = max(inclPay, table[i-1]);
}
/* output maxmium payoff */
cout<<"Max Payoff: "<<table[n-1]<<endl;
/* back track of the intervals and store index in resultsIndex */
int *resultsIndex = new int[n];
int resultNum = -1;
int j=n-1;
while(j>=0)
{
int pj = binarySearch(arr, j);
if (arr[j].payoff+table[pj]>=table[j-1])
{
//printinterval(arr[j]);
resultNum++;
resultsIndex[resultNum]=j;
//cout<<"was here 1"<<endl;
j=pj;
}
else
{
j--;
}
}
//BacktrackForSolution(arr, resultsIndex, table, n-1, resultNum);
//resultNum--;
/* print out intervals in reverse order */
//cout<<"reverse"<<endl;
for (; resultNum>=0; resultNum--) printinterval(arr[resultsIndex[resultNum]]);
delete[] table;
}
/*---------------------------driver program--------------------------------*/
int main()
{
/* assume maximum data size */
int size = 1000000;
Interval* arr = new Interval[size];
std::string input;
int n = 0;
while(std::getline(std::cin, input))
{
/* parse into tokens */
vector<string> tokens;
istringstream iss(input);
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter(tokens));
int start = std::stoi(tokens[0]);
int finish = std::stoi(tokens[1]);
int payoff = std::stoi(tokens[2]);
arr[n]={start,finish,payoff};
n++;
}
findMaxpayoff(arr, n);
return 0;
}