diff --git a/apartment_allocation.pyt b/apartment_allocation.pyt new file mode 100644 index 0000000..c99f398 --- /dev/null +++ b/apartment_allocation.pyt @@ -0,0 +1,21 @@ +def apartment_allocation(n, m, k, applicants, apartments): + applicants.sort() + apartments.sort() + i = j = count = 0 + + while i < n and j < m: + if abs(applicants[i] - apartments[j]) <= k: + count += 1 + i += 1 + j += 1 + elif apartments[j] < applicants[i] - k: + j += 1 + else: + i += 1 + return count + +# Contoh penggunaan +n, m, k = 4, 3, 5 +applicants = [60, 45, 80, 60] +apartments = [30, 60, 75] +print(apartment_allocation(n, m, k, applicants, apartments)) # Output: 2 diff --git a/palindrome.pyt b/palindrome.pyt new file mode 100644 index 0000000..9e74481 --- /dev/null +++ b/palindrome.pyt @@ -0,0 +1,30 @@ +def process_palindrome_queries(n, m, s, operations): + from bisect import bisect_left + + s = list(s) + + result = [] + for op in operations: + parts = op.split() + if parts[0] == '1': + _, k, x = parts + s[int(k) - 1] = x + elif parts[0] == '2': + _, a, b = parts + a, b = int(a) - 1, int(b) - 1 + substring = s[a:b+1] + result.append('1' if substring == substring[::-1] else '0') + return result + +# Contoh penggunaan +n, m = 7, 5 +s = "aybabtu" +operations = [ + "2 3 5", + "1 3 x", + "2 3 5", + "1 5 x", + "2 3 5" +] +results = process_palindrome_queries(n, m, s, operations) +print("\n".join(results)) # Output: 1 0 1 diff --git a/tower.pyt b/tower.pyt new file mode 100644 index 0000000..27b71fc --- /dev/null +++ b/tower.pyt @@ -0,0 +1,17 @@ +def tower_of_hanoi(n, source=1, target=3, auxiliary=2, moves=None): + if moves is None: + moves = [] + if n == 1: + moves.append((source, target)) + else: + tower_of_hanoi(n-1, source, auxiliary, target, moves) + moves.append((source, target)) + tower_of_hanoi(n-1, auxiliary, target, source, moves) + return moves + +# Contoh penggunaan +n = 2 +moves = tower_of_hanoi(n) +print(len(moves)) +for move in moves: + print(move[0], move[1]) diff --git a/tsa_php/src/ApartmentAllocation.php b/tsa_php/src/ApartmentAllocation.php index 7d95f4a..585bd03 100644 --- a/tsa_php/src/ApartmentAllocation.php +++ b/tsa_php/src/ApartmentAllocation.php @@ -11,6 +11,48 @@ function solveApartment($A, $B, $N, $M, $K) { */ // YOUR CODE HERE + n; + $this->tree[$index] = $value; + while ($index > 1) { + $index >>= 1; + $this->tree[$index] = ($this->tree[$index * 2] + $this->tree[$index * 2 + 1]) % MOD; + } } /** @@ -32,7 +37,15 @@ public function update($index, $value) { * @return int The sum of values in the range [l, r]. */ public function query($l, $r) { - // YOUR CODE HERE + $l += $this->n; + $r += $this->n + 1; + $result = 0; + while ($l < $r) { + if ($l % 2 == 1) $result = ($result + $this->tree[$l++]) % MOD; + if ($r % 2 == 1) $result = ($result + $this->tree[--$r]) % MOD; + $l >>= 1; + $r >>= 1; + } return $result; } } @@ -43,7 +56,10 @@ public function query($l, $r) { * @return array An array where hashPower[i] = HASH^i % MOD. */ function initializeHashPowers($length) { - // YOUR CODE HERE + $hashPower = [1]; + for ($i = 1; $i <= $length; $i++) { + $hashPower[$i] = ($hashPower[$i - 1] * HASH) % MOD; + } return $hashPower; } @@ -55,7 +71,17 @@ function initializeHashPowers($length) { * @return array An associative array containing the forward and backward hash segment trees. */ function initializeHashTables($n, $s, $hashPower) { - // YOUR CODE HERE + $fwdHash = new HashSegmentTree($n); + $bckHash = new HashSegmentTree($n); + + for ($i = 0; $i < $n; $i++) { + $val = (ord($s[$i]) * $hashPower[$i]) % MOD; + $fwdHash->update($i, $val); + + $revVal = (ord($s[$i]) * $hashPower[$n - 1 - $i]) % MOD; + $bckHash->update($n - 1 - $i, $revVal); + } + return ['fwdHash' => $fwdHash, 'bckHash' => $bckHash]; } @@ -67,9 +93,59 @@ function initializeHashTables($n, $s, $hashPower) { * @return array An array of results for palindrome queries ("YES" or "NO"). */ function processOperations($n, $operations, $s) { - // YOUR CODE HERE + $hashPower = initializeHashPowers($n); + $trees = initializeHashTables($n, $s, $hashPower); + $fwd = $trees['fwdHash']; + $bck = $trees['bckHash']; + $sArr = str_split($s); + $results = []; + + foreach ($operations as $op) { + if ($op[0] == 1) { + // Update operation + $k = $op[1] - 1; + $x = $op[2]; + $sArr[$k] = $x; + $fwd->update($k, (ord($x) * $hashPower[$k]) % MOD); + $bck->update($n - 1 - $k, (ord($x) * $hashPower[$n - 1 - $k]) % MOD); + } else if ($op[0] == 2) { + // Query operation + $a = $op[1] - 1; + $b = $op[2] - 1; + $fwdHash = $fwd->query($a, $b); + $bckHash = $bck->query($n - 1 - $b, $n - 1 - $a); + + if ($a <= $b) { + $leftShift = $n - 1 - $b - $a; + $fwdHash = ($fwdHash * $hashPower[$leftShift]) % MOD; + } + + $results[] = ($fwdHash == $bckHash) ? "YES" : "NO"; + } + } + return $results; } +// Read input +fscanf(STDIN, "%d %d", $n, $m); +$s = trim(fgets(STDIN)); +// Read m operations +$operations = []; +for ($i = 0; $i < $m; $i++) { + $line = explode(' ', trim(fgets(STDIN))); + $type = (int)$line[0]; + if ($type == 1) { + $operations[] = [1, (int)$line[1], $line[2]]; + } else { + $operations[] = [2, (int)$line[1], (int)$line[2]]; + } +} + +// Process and print results +$results = processOperations($n, $operations, $s); +foreach ($results as $res) { + echo $res . "\n"; +} ?> \ No newline at end of file diff --git a/tsa_php/src/TowerOfHanoi.php b/tsa_php/src/TowerOfHanoi.php index d937dac..7171c78 100644 --- a/tsa_php/src/TowerOfHanoi.php +++ b/tsa_php/src/TowerOfHanoi.php @@ -13,6 +13,14 @@ function moveDisk($diskNumber, &$moves, $sourceStack, $destinationStack, $auxiliaryStack) { // YOUR CODE HERE + if ($diskNumber == 1) { + $moves[] = [$sourceStack, $destinationStack]; + return; + } + + moveDisk($diskNumber - 1, $moves, $sourceStack, $auxiliaryStack, $destinationStack); + $moves[] = [$sourceStack, $destinationStack]; + moveDisk($diskNumber - 1, $moves, $auxiliaryStack, $destinationStack, $sourceStack); } /** @@ -23,6 +31,13 @@ function moveDisk($diskNumber, &$moves, $sourceStack, $destinationStack, $auxili function towerOfHanoi($numberOfDisks) { // YOUR CODE HERE + $moves = []; + moveDisk($numberOfDisks, $moves, 1, 3, 2); + + echo count($moves) . "\n"; + foreach ($moves as [$from, $to]) { + echo "$from $to\n"; + } } -?> +?> \ No newline at end of file