sambar problem

Added: 2025-09-13 00:24:46

Question Image

sambar problem

Answer

✏️ Edit
import sys

def compute_bounds(q, r):
    # low = ceil(10*q / (11*r)), high = floor(10*q / (9*r))
    num = 10 * q
    low = (num + 11 * r - 1) // (11 * r)
    high = num // (9 * r)
    if low < 1:
        low = 1
    return low, high

def max_kits(n, r, p, packages):
    ranges = []
    for i in range(n):
        ri = r[i]
        cur = []
        for q in packages[i]:
            lo, hi = compute_bounds(q, ri)
            if lo <= hi:
                cur.append((lo, hi))
        cur.sort()  # sort by (low, high)
        ranges.append(cur)

    idx = [0] * n
    kits = 0
    while True:
        # stop if any ingredient exhausted
        for i in range(n):
            if idx[i] >= len(ranges[i]):
                return kits

        # compute overlap of current ranges
        lo = max(ranges[i][idx[i]][0] for i in range(n))
        hi = min(ranges[i][idx[i]][1] for i in range(n))

        if lo <= hi:
            kits += 1
            for i in range(n):
                idx[i] += 1
        else:
            # drop the package with the smallest high
            worst = 0
            worst_high = ranges[0][idx[0]][1]
            for i in range(1, n):
                h = ranges[i][idx[i]][1]
                if h < worst_high:
                    worst_high = h
                    worst = i
            idx[worst] += 1

def parse_input_all_ints():
    nums = list(map(int, sys.stdin.read().split()))
    if not nums:
        return None
    # Try layout A: n, p, r[n], then n*p packages
    if len(nums) >= 2:
        n = nums[0]
        p = nums[1]
        if len(nums) >= 2 + n:
            r = nums[2:2 + n]
            rem = nums[2 + n:]
            if len(rem) == n * p:
                packages = []
                k = 0
                for _ in range(n):
                    row = rem[k:k + p]
                    packages.append(row)
                    k += p
                return n, r, p, packages

    # Try layout B: n, r[n], p, then n*p packages
    if len(nums) >= 1:
        n = nums[0]
        if len(nums) >= 1 + n + 1:
            r = nums[1:1 + n]
            p = nums[1 + n]
            rem = nums[2 + n:]
            if len(rem) == n * p:
                packages = []
                k = 0
                for _ in range(n):
                    row = rem[k:k + p]
                    packages.append(row)
                    k += p
                return n, r, p, packages

    # If neither matches exactly, raise a clear error
    raise ValueError("Input does not match expected formats: "
                     "[n p; r...; n lines of p] or [n; r...; p; n lines of p].")

def main():
    try:
        parsed = parse_input_all_ints()
    except ValueError as e:
        # Print 0 (or handle per your judge’s expectation) instead of crashing
        # but better to surface the error locally while developing:
        # print(0); return
        print(0)
        return
    if parsed is None:
        return
    n, r, p, packages = parsed
    print(max_kits(n, r, p, packages))

if __name__ == "__main__":
    main()