Skip to content

Commit d504388

Browse files
Merge pull request matthewsamuel95#552 from CodeLogist/patch-3
Patch 3
2 parents f277326 + ac39e70 commit d504388

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
#include <queue>
5+
6+
using std::vector;
7+
using std::cin;
8+
using std::cout;
9+
using std::priority_queue;
10+
11+
12+
class worker
13+
{
14+
public:
15+
long long int id;
16+
long long int next_free_time;
17+
worker(long long int id)
18+
{
19+
this->id=id;
20+
next_free_time=0;
21+
}
22+
};
23+
24+
struct compare{
25+
bool operator()(const worker &w1, const worker &w2) const {
26+
if(w1.next_free_time == w2.next_free_time)
27+
return w1.id > w2.id;
28+
else
29+
return w1.next_free_time > w2.next_free_time;
30+
}
31+
};
32+
33+
class JobQueue {
34+
private:
35+
long long int num_workers_;
36+
vector<long long int> jobs_;
37+
38+
vector<long long int> assigned_workers_;
39+
vector<long long int> start_times_;
40+
41+
void WriteResponse() const {
42+
for (long long int i = 0; i < jobs_.size(); ++i) {
43+
cout << assigned_workers_[i] << " " << start_times_[i] << "\n";
44+
}
45+
}
46+
47+
void ReadData() {
48+
long long int m;
49+
cin >> num_workers_ >> m;
50+
jobs_.resize(m);
51+
for(long long int i = 0; i < m; ++i)
52+
cin >> jobs_[i];
53+
}
54+
55+
void AssignJobs() {
56+
// TODO: replace this code with a faster algorithm.
57+
assigned_workers_.resize(jobs_.size());
58+
start_times_.resize(jobs_.size());
59+
priority_queue <worker, vector<worker>, compare> pq;
60+
long long int i=0;
61+
for(i=0;i<num_workers_;i++)
62+
{
63+
pq.push(worker(i));
64+
}
65+
66+
for (i = 0; i < jobs_.size(); ++i) {
67+
worker free_thread = pq.top();
68+
pq.pop();
69+
assigned_workers_[i] = free_thread.id;
70+
start_times_[i] = free_thread.next_free_time;
71+
free_thread.next_free_time += jobs_[i];
72+
pq.push(free_thread);
73+
}
74+
}
75+
76+
public:
77+
void Solve() {
78+
ReadData();
79+
AssignJobs();
80+
WriteResponse();
81+
}
82+
};
83+
84+
85+
86+
int main() {
87+
std::ios_base::sync_with_stdio(false);
88+
JobQueue job_queue;
89+
job_queue.Solve();
90+
return 0;
91+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
********Problem Description:********
2+
3+
******Task:******
4+
5+
You have a program which is parallelized and uses 𝑛 independent threads to process the given list
6+
of 𝑚 jobs. Threads take jobs in the order they are given in the input. If there is a free thread,
7+
it immediately takes the next job from the list. If a thread has started processing a job, it doesn’t
8+
interrupt or stop until it finishes processing the job. If several threads try to take jobs from the list
9+
simultaneously, the thread with smaller index takes the job. For each job you know exactly how long
10+
will it take any thread to process this job, and this time is the same for all the threads. You need to
11+
determine for each job which thread will process it and when will it start processing.
12+
13+
******Input Format:******
14+
15+
The first line of the input contains integers 𝑛 and 𝑚.
16+
The second line contains 𝑚 integers 𝑡𝑖 — the times in seconds it takes any thread to process 𝑖-th job.
17+
The times are given in the same order as they are in the list from which threads take jobs.
18+
Threads are indexed starting from 0.
19+
Constraints. 1 ≤ 𝑛 ≤ 105
20+
; 1 ≤ 𝑚 ≤ 105
21+
; 0 ≤ 𝑡𝑖 ≤ 109
22+
.
23+
******Output Format:******
24+
25+
Output exactly 𝑚 lines. 𝑖-th line (0-based index is used) should contain two spaceseparated
26+
integers — the 0-based index of the thread which will process the 𝑖-th job and the time in
27+
seconds when it will start processing that job.

0 commit comments

Comments
 (0)