Skip to content

Commit f277326

Browse files
Merge pull request matthewsamuel95#550 from CodeLogist/patch-1
Patch 1
2 parents 510d117 + d89495b commit f277326

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//Code By CodeLogist
2+
//Problem Also Present in Stanford University's Data Structure Course on Coursera.
3+
4+
#include <iostream>
5+
#include <vector>
6+
#include <algorithm>
7+
8+
using std::vector;
9+
using std::cin;
10+
using std::cout;
11+
using std::swap;
12+
using std::pair;
13+
using std::make_pair;
14+
15+
class HeapBuilder {
16+
private:
17+
vector<int> data_;
18+
vector< pair<int, int> > swaps_;
19+
20+
void WriteResponse() const {
21+
cout << swaps_.size() << "\n";
22+
for (int i = 0; i < swaps_.size(); ++i) {
23+
cout << swaps_[i].first << " " << swaps_[i].second << "\n";
24+
}
25+
}
26+
27+
void ReadData() {
28+
int n;
29+
cin >> n;
30+
data_.resize(n);
31+
for(int i = 0; i < n; ++i)
32+
cin >> data_[i];
33+
}
34+
35+
void GenerateSwaps() {
36+
swaps_.clear();
37+
for (int i = data_.size()/2; i >= 0; i--) {
38+
ShiftDown(i);
39+
}
40+
}
41+
42+
void ShiftDown(int i){
43+
int minindex=i;
44+
int l=2*i+1;
45+
if(l < data_.size() && data_[l] < data_[minindex])
46+
{
47+
minindex=l;
48+
}
49+
int r=2*i+2;
50+
if(r < data_.size() && data_[r] < data_[minindex])
51+
minindex=r;
52+
if(minindex != i){
53+
swap(data_[i], data_[minindex]);
54+
swaps_.push_back(make_pair(i, minindex));
55+
ShiftDown(minindex);
56+
}
57+
}
58+
59+
60+
public:
61+
void Solve() {
62+
ReadData();
63+
GenerateSwaps();
64+
WriteResponse();
65+
}
66+
};
67+
68+
int main() {
69+
std::ios_base::sync_with_stdio(false);
70+
HeapBuilder heap_builder;
71+
heap_builder.Solve();
72+
return 0;
73+
}

Data Structures/Heap/Readme.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
24+
******Output Format:******
25+
26+
Output exactly 𝑚 lines. 𝑖-th line (0-based index is used) should contain two spaceseparated
27+
integers — the 0-based index of the thread which will process the 𝑖-th job and the time in
28+
seconds when it will start processing that job.

0 commit comments

Comments
 (0)