Question

There is an old fairy tale where some elves secretly help an old shoemaker finish his work for the day. In this version, the

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <algorithm>
using namespace std;

struct job
{
int id;
int time;
int fine;
};

bool compFunction(job a, job b){ return (a.fine < b.fine); }

void jobSequence( job* arr, int n){
// Sort all jobs according to increasing order of fine
sort(arr, arr+n, compFunction);

int result[n]; // To store final sequence
bool slot[n]; // To check if slot is free

for(int i=0; i<n; i++){ slot[i] = false; }

for(int i=0; i<n; i++){
int from = n;
if( arr[i].time < from ){ from = arr[i].time; }
for (int j = from-1; j >= 0; j--){
if( slot[j]==false ){
result[j] = i;
slot[j] = true;
break;
}
}
}
for (int i=0; i<n; i++){
if( slot[i] ){ cout << arr[result[i]].id << " "; }}
cout << endl;
}

// Driver program to test methods
int main()
{
int totalJobs;
job* arr;
cin >> totalJobs;
arr = new job[totalJobs];
for(int i = 0; i < totalJobs; i++){
cin >> arr[i].time >> arr[i].fine;
arr[i].id = i+1;
}
jobSequence(arr, totalJobs);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
There is an old fairy tale where some elves secretly help an old shoemaker finish his...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1) Write a Reaction equation using 4- aminobenzenesulfulfonic acid (aniline derivative) and 8-ani...

    1) Write a Reaction equation using 4- aminobenzenesulfulfonic acid (aniline derivative) and 8-anilino-1-naphthalenesulfonic acid (coupling agent). Write the mechanism. SYNTHESIS OF AZO DYES INTRODUCTION Humans' love of color dates back almost to prehistoric times, yet it is not actually until the modern era that the full range of the rainbow has been accessible to the majority of people irn their clothes and other textiles. Ancient or medieval times have often been described as being quite splendid with their "tyrian purple"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT