-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfroshweek2.cpp
More file actions
127 lines (87 loc) · 2.47 KB
/
Copy pathfroshweek2.cpp
File metadata and controls
127 lines (87 loc) · 2.47 KB
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
#include <bits/stdc++.h>
using namespace std;
//USE OF MERGE SORT
void mergeSort(vector<int> &left, vector<int> &right, vector<int> &number){
int nL = left.size();
int nR = right.size();
int i = 0, j = 0, k = 0;
while (j < nL && k < nR){
// if left number smaller than right, input left into number vector first
if (left[j] < right[k]) {
number[i] = left[j];
j++;
}
else {
// input right number into number vector first
number[i] = right[k];
k++;
}
i++;
}
//This while loop is if the left side has remaining values
while (j < nL) {
number[i] = left[j];
j++;
i++;
}
//This while loop is if the right side has remaining values
while (k < nR) {
number[i] = right[k];
k++;
i++;
}
}
void sort(vector<int> &number) {
if (number.size() <= 1)
return;
// 1. Obtain the midpt
int mid = number.size() / 2;
vector<int> left;
vector<int> right;
for (int j = 0; j < mid;j++)
left.push_back(number[j]);
for (int j = 0; j < (number.size()) - mid; j++)
right.push_back(number[mid + j]);
// 2. Recursively divide the vector into single elements
sort(left);
sort(right);
// 3. sort and merge the sub vectors
mergeSort(left, right, number);
}
int main(void){
int task;
int time;
vector <int> work;
vector <int> duration;
cin >> task >> time;
//for task length
for(int i=0; i<task; i++){
int value;
cin >> value;
work.push_back(value);
}
//for time length
for(int i=0; i<time; i++){
int value;
cin >> value;
duration.push_back(value);
}
cout << endl;
sort(work); //use merge sort
sort(duration); //use merge sort
// CAN USE stable_sort ALSO (either both ascending or both descending)
// stable_sort(work.begin(), work.end()); //in decreasing order
// stable_sort(duration.begin(), duration.end()); // in ascending order
int count = 0;
int job = 0;
int interval = 0;
while(job < work.size() && interval < duration.size()){ //SAVE TIME COMPLEXITY.
if(work[job] <= duration[interval]){
count++;
job++;
}
interval++; // else continue with the interval until while loop exhausted or all job satisfied with intervals
}
cout << count;
return 0;
}