First, best and worst fit

Where a new process goes among the free holes changes how much memory is left for the next ones. First fit, best fit and worst fit can place the same list of processes differently, and one of them may leave a process waiting where another does not.

What to build

Place the same list of processes with each of the three strategies, and say which one places the most.

Requirements

  • Each process goes in a hole that is big enough, and uses only what it needs: the rest of the hole stays as a smaller hole in the same position. A process that fits nowhere waits and is not placed (later processes may still fit).
  • First fit uses the lowest-numbered big enough hole, best fit the smallest big enough hole, worst fit the largest hole (if it is big enough). Equal sizes: the lowest-numbered hole. Each strategy starts from the original holes.
  • Print three lines first: ..., best: ..., worst: ..., each with one entry for every process: the hole number (from 1) or - if it waited. Then print winner: NAME, the strategy that placed the most processes; on a tie the first of first, best, worst.

Input

The first line has h and p (1 ≤ h, p ≤ 10). The second line has the h hole sizes, the third line the p process sizes (each 1 to 1000).

Output

Three strategy lines and the winner: line.

Constraints

Processes are placed in the order given.

Example

Input:

5 4
100 500 200 300 600
212 417 112 426

Output:

first: 2 5 2 -
best: 4 2 3 5
worst: 5 2 5 -
winner: best

Why it works

The three strategies differ only in how they choose among the holes that fit. With the textbook sizes, first fit and worst fit leave the last process waiting while best fit places all four. Neither choice is best for every input, which is why real allocators measure instead of guessing.

Examples

Example 1 · Input
5 4
100 500 200 300 600
212 417 112 426
Expected output
first: 2 5 2 -
best: 4 2 3 5
worst: 5 2 5 -
winner: best
Example 2 · Input
2 2
10 10
5 5
Expected output
first: 1 1
best: 1 1
worst: 1 2
winner: first

Hints

Hint 1

Write one function that takes the strategy (0 first, 1 best, 2 worst) and a copy of the holes, and fills an array with the hole chosen for each process.

Hint 2

First fit takes the lowest-numbered hole that is big enough. Best fit takes the smallest hole that is big enough, worst fit the largest, and a tie goes to the lowest number.

Hint 3

Count the processes placed by each strategy. The winner has the most; on a tie the first of first, best, worst.

Solution

Show one correct solution (try it yourself first)
#include <stdio.h>

int h, p, original[10], proc[10];

int place(int strategy, int *where) {
    int hole[10], placed = 0;
    for (int i = 0; i < h; i++) hole[i] = original[i];
    for (int i = 0; i < p; i++) {
        int pick = -1;
        for (int j = 0; j < h; j++) {
            if (hole[j] < proc[i]) continue;
            if (pick < 0) pick = j;
            else if (strategy == 0) break;
            else if (strategy == 1 && hole[j] < hole[pick]) pick = j;
            else if (strategy == 2 && hole[j] > hole[pick]) pick = j;
            if (strategy == 0) break;
        }
        if (pick >= 0) {
            hole[pick] -= proc[i];
            placed++;
        }
        where[i] = pick;
    }
    return placed;
}

int main(void) {
    scanf("%d %d", &h, &p);
    for (int i = 0; i < h; i++) scanf("%d", &original[i]);
    for (int i = 0; i < p; i++) scanf("%d", &proc[i]);
    const char *names[3] = {"first", "best", "worst"};
    int best_count = -1, winner = 0;
    for (int s = 0; s < 3; s++) {
        int where[10];
        int count = place(s, where);
        printf("%s:", names[s]);
        for (int i = 0; i < p; i++) {
            if (where[i] < 0) printf(" -");
            else printf(" %d", where[i] + 1);
        }
        printf("\n");
        if (count > best_count) {
            best_count = count;
            winner = s;
        }
    }
    printf("winner: %s\n", names[winner]);
    return 0;
}

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Memory: addresses and allocation →