SCAN, the elevator algorithm, moves the arm in one direction serving every request on the way, goes all the way to the end of the disk, then reverses and serves the rest.
What to build
Serve requests with SCAN and print the order and the total head movement.
Requirements
- The disk has cylinders 0 to
size − 1. The head starts athand moves in the given direction (upfor larger numbers,downfor smaller). It serves the requests in its way, in order, including any athitself. - At the end of the disk the head turns round. Requests still waiting on the other side are served on the way back, nearest first, until the farthest one. If none are left, the head stops at the end.
- Print the cylinders in the order served on the first line, and
total N(total head movement, including the trip to the disk end) on the second.
Input
The first line has h, size and the direction word, then n (1 ≤ n ≤ 30, 0 ≤ h < size ≤ 1000). The second line has the n requested cylinders (0 to size − 1).
Output
The served cylinders on one line, then total N.
Constraints
Several requests may be for the same cylinder; each is served in turn, without extra movement.
Example
Input:
53 200 up 8
98 183 37 122 14 124 65 67
Output:
65 67 98 122 124 183 37 14
total 331
Why it works
SCAN always goes to the disk end, even when the last request is much closer, which costs some movement. In return, no request waits more than two sweeps, so nothing starves, unlike SSTF. The sorted order makes the program simple: two sorted runs, one on each side of the head.
Examples
53 200 up 8 98 183 37 122 14 124 65 67
65 67 98 122 124 183 37 14 total 331
53 200 down 8 98 183 37 122 14 124 65 67
37 14 65 67 98 122 124 183 total 236
Hints
Hint 1
Sort the requests. In direction up, serve the requests at or above the head from the lowest up; in direction down, serve those at or below the head from the highest down.
Hint 2
Then move on to the disk end (size - 1 going up, 0 going down). If requests are left on the other side, go back to the farthest one, serving the rest in order on the way.
Hint 3
Total = distance to the disk end, plus the distance from the end to the farthest remaining request (only if there are some).
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
#include <string.h>
int main(void) {
int h, size, n, req[30];
char dir[8];
scanf("%d %d %7s %d", &h, &size, dir, &n);
for (int i = 0; i < n; i++) scanf("%d", &req[i]);
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (req[j] < req[i]) {
int t = req[i];
req[i] = req[j];
req[j] = t;
}
int total = 0;
int up = strcmp(dir, "up") == 0;
int order[30], count = 0;
if (up) {
for (int i = 0; i < n; i++)
if (req[i] >= h) order[count++] = req[i];
total += (size - 1) - h;
int far = -1;
for (int i = n - 1; i >= 0; i--)
if (req[i] < h) {
order[count++] = req[i];
far = req[i];
}
if (far >= 0) total += (size - 1) - far;
} else {
for (int i = n - 1; i >= 0; i--)
if (req[i] <= h) order[count++] = req[i];
total += h;
int far = -1;
for (int i = 0; i < n; i++)
if (req[i] > h) {
order[count++] = req[i];
far = req[i];
}
if (far >= 0) total += far;
}
for (int i = 0; i < count; i++) printf("%d%s", order[i], i + 1 < count ? " " : "");
printf("\ntotal %d\n", total);
return 0;
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step