-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttendingWorkshops.cpp
65 lines (61 loc) · 1.67 KB
/
AttendingWorkshops.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
#include<bits/stdc++.h>
using namespace std;
struct Workshop{
int starttime;
int duration;
int endtime;
};
struct Available_Workshops{
int n;
Workshop *arr;
};
Available_Workshops* initialize (int start_time[], int duration[], int n){
Available_Workshops *ob=new Available_Workshops;
ob->n=n;
ob->arr= new Workshop[n];
for (int i=0; i<n; i++) {
(ob->arr+i)->starttime=start_time[i];
(ob->arr+i)->duration=duration[i];
(ob->arr+i)->endtime=(ob->arr+i)->starttime+(ob->arr+i)->duration;
}
return ob;
}
bool compare( Workshop a, Workshop b){
if(a.endtime < b.endtime)
return 1;
else
return 0;
}
//Define the structs Workshops and Available_Workshops.
//Implement the functions initialize and CalculateMaxWorkshops
int CalculateMaxWorkshops(Available_Workshops* ptr){
int count=1;
sort(ptr->arr,ptr->arr+ptr->n,&compare);
for (int i=0; i<(ptr->n)-1; i++) {
if(ptr->arr[i].starttime+ptr->arr[i].duration<=ptr->arr[i+1].starttime){
count++;
}
else{
ptr->arr[i+1].starttime=ptr->arr[i].starttime;
ptr->arr[i+1].duration=ptr->arr[i].duration;
}
}
return count;
}
int main(int argc, char *argv[]) {
int n; // number of workshops
cin >> n;
// create arrays of unknown size n
int* start_time = new int[n];
int* duration = new int[n];
for(int i=0; i < n; i++){
cin >> start_time[i];
}
for(int i = 0; i < n; i++){
cin >> duration[i];
}
Available_Workshops * ptr;
ptr = initialize(start_time,duration, n);
cout << CalculateMaxWorkshops(ptr) << endl;
return 0;
}