forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJob_Sequencing.cpp
43 lines (43 loc) · 835 Bytes
/
Job_Sequencing.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
#include<bits/stdc++.h>
using namespace std;
struct Job
{
int deadline;
int profit;
};
bool compare(struct Job a, struct Job b)
{
return a.profit>b.profit;
}
int jobSequencing(Job arr[], int n)
{
sort(arr,arr+n,compare); //array sorted according to profit
bool visit[n]={false};
int max_profit=0;
for(int i=0;i<n;i++)
{
for(int j=min(n,arr[i].deadline-1);j>=0;j--)
{
if(visit[j]==false)
{
visit[j]=true;
max_profit+=arr[i].profit;
break;
}
}
}
return max_profit;
}
int main()
{
int n,capacity;
//array of structures
cin>>n;
Job arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i].deadline>>arr[i].profit;
}
cout<<jobSequencing(arr,n);
return 0;
}