|
| 1 | +(* |
| 2 | + This is an implementation of my solution for the first exercise in |
| 3 | + the first coding exercises set for the Algorithms course during the |
| 4 | + 2024-2025 winter Semester @ ECE/NTUA. The full description of the |
| 5 | + problem can be found in the web page of the course in Helios. |
| 6 | + |
| 7 | + Our goal is to place N stores in M non-overlapping integer intervals[s_i, x_i]. |
| 8 | + A store can be placed in any integer number that belongs to an interval. |
| 9 | + We want our program to calculate the smallest possible distance between |
| 10 | + two consecutive stores such as that distance is maximized. |
| 11 | + |
| 12 | + Input: The program reads from the standard input two positive integers, N amount of stores and M amount of spaces(intervals) to place them. |
| 13 | + After that for the next M lines it reads from the standard input two non-negative integers defining the beginning and the end of each space(interval). |
| 14 | + We assume the spaces are given in a random order and their total length is not shorter than N. |
| 15 | + |
| 16 | + Output: Our program prints in the standard output a positive integer, |
| 17 | + the minimum required distance between two consecutive stores in a |
| 18 | + placement of all stores such as that minimum distance is maximized. |
| 19 | + |
| 20 | + Example Input: 6 3 |
| 21 | + 10 12 |
| 22 | + 0 4 |
| 23 | + 5 8 |
| 24 | + |
| 25 | + Example Output: 2 |
| 26 | + |
| 27 | +*) |
| 28 | + |
| 29 | +def main |
| 30 | + def bsort: n as int, x as int[] |
| 31 | + def swap: x y as ref int |
| 32 | + var t is int |
| 33 | + t := x |
| 34 | + x := y |
| 35 | + y := t |
| 36 | + |
| 37 | + var changed is byte |
| 38 | + var i is int |
| 39 | + |
| 40 | + loop: |
| 41 | + changed := false |
| 42 | + i := 0 |
| 43 | + loop: |
| 44 | + if i < n-1: |
| 45 | + if x[i] > x[i+1]: |
| 46 | + swap x[i], x[i+1] |
| 47 | + changed := true |
| 48 | + i := i + 1 |
| 49 | + else: break |
| 50 | + if not changed: break |
| 51 | + |
| 52 | + def possible_store is byte: arr as int[], mid spaces stores as int: |
| 53 | + var last_position placed_stores i is int |
| 54 | + last_position := -1000000000 |
| 55 | + placed_stores := 0 |
| 56 | + i := 0 |
| 57 | + loop: |
| 58 | + if i >= (spaces/2): break |
| 59 | + else: |
| 60 | + var j is int |
| 61 | + j := 2*i |
| 62 | + var position is int |
| 63 | + if (last_position + mid) > arr[j]: position := last_position + mid |
| 64 | + else: position := arr[j] |
| 65 | + loop: |
| 66 | + if position > arr[j + 1]: break |
| 67 | + else: |
| 68 | + placed_stores := placed_stores + 1 |
| 69 | + last_position := position |
| 70 | + position := position + mid |
| 71 | + if placed_stores >= stores: return true |
| 72 | + i := i + 1 |
| 73 | + return false |
| 74 | + |
| 75 | + def largest_minimum_distance is int: arr as int[], spaces stores as int: |
| 76 | + bsort(arr, spaces) |
| 77 | + var result left right is int |
| 78 | + result := 0 |
| 79 | + left := 0 |
| 80 | + right := arr[spaces - 1] - arr[0] |
| 81 | + |
| 82 | + #Simple Binary Search for finding the sweet spot |
| 83 | + loop: |
| 84 | + if left > right: break |
| 85 | + else: |
| 86 | + var distance is int |
| 87 | + distance := (left + right) / 2 |
| 88 | + if possible_store(arr, distance, spaces, stores): |
| 89 | + result := distance |
| 90 | + left := distance + 1 |
| 91 | + else: |
| 92 | + right = distance + 1 |
| 93 | + return result |
| 94 | + |
| 95 | + var stores size s f N tail is int |
| 96 | + tail := 0 |
| 97 | + stores := readInteger() |
| 98 | + N := readInteger() |
| 99 | + |
| 100 | + var MAX_SIZE MAX_PLACES is int |
| 101 | + |
| 102 | + MAX_STORES := 1000000 #Change accordingly for larger sizes |
| 103 | + MAX_PLACES := 100000 #Change accordingly for larger sizes |
| 104 | + |
| 105 | + (* |
| 106 | + The program handles out of bound inputs by |
| 107 | + asking the user again for a correct one. |
| 108 | + Maybe the two "if" statements can be combined into one. |
| 109 | + *) |
| 110 | + |
| 111 | + if stores > MAX_STORES or stores < 1: |
| 112 | + writeString "Wrong starting input, let's try again!" |
| 113 | + stores := readInteger() |
| 114 | + |
| 115 | + if N > MAX_PLACES or N < 1: |
| 116 | + writeString "Wrong starting input, let's try again!" |
| 117 | + N := readInteger() |
| 118 | + |
| 119 | + var arr is int[2*N] |
| 120 | + |
| 121 | + var i is int |
| 122 | + i := 0 |
| 123 | + |
| 124 | + loop: |
| 125 | + if i >= N: break |
| 126 | + else: |
| 127 | + s := readInteger() #We assume correct values for the limits of the spaces |
| 128 | + f := readInteger() |
| 129 | + |
| 130 | + arr[i*2] := s |
| 131 | + arr[i*2 + 1] := f |
| 132 | + tail := tail + 2 |
| 133 | + i := i + 1 |
| 134 | + |
| 135 | + writeInteger: largest_minimum_distance(arr, tail, stores) |
| 136 | + writeString: "\n" |
0 commit comments