tinycodinglm / misc /misc-0364.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame
4.14 kB
opencode trace
System You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code. When solving problems, explain your reasoning step by step before providing...

You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code. When solving problems, explain your reasoning step by step before providing the final solution.

User

Problem Description:
A broken seven-segment clock suddenly started displaying erroneous information. Each segment of each digit could potentially stop working. Given the number of segments that stopped working in total and the current state of the digits, determine the minimum possible number that could be displayed by turning off exactly k more segments.

Input Format:

  • The first line contains integers n (1 ≤ n ≤ 2000) and k (0 ≤ k ≤ 2000).
  • The next n lines each contain a binary string of length 7 representing the glowing segments of each digit.

Output Format:
Output a single number consisting of n digits — the minimum number that can be obtained by turning off exactly k more segments or -1 if it is impossible.

Examples
Input:

markdown
1 3
1111111

Output:

markdown
1

Input:

markdown
2 5
1011011
1101011

Output:

markdown
02
Assistant

import sys

def main():
n, k = map(int, sys.stdin.readline().split())
digits = [sys.stdin.readline().strip() for _ in range(n)]

markdown
# Define the masks for each digit (0-9) as binary strings
digit_masks = [
    '1111110',  # 0
    '0110000',  # 1
    '1101101',  # 2
    '1111001',  # 3
    '0110011',  # 4
    '1011011',  # 5
    '1011111',  # 6
    '1110000',  # 7
    '1111111',  # 8
    '1111011',  # 9
]

# Precompute for each digit the possible deltas and minimum digit
digit_data = []
for s in digits:
    count_ones_S = s.count('1')
    possible = []
    for d in range(10):
        mask = digit_masks[d]
        valid = True
        for i in range(7):
            if mask[i] == '1' and s[i] != '1':
                valid = False
                break
        if valid:
            count_d = mask.count('1')
            delta = count_ones_S - count_d
            possible.append( (delta, d) )
    # Create a dictionary of delta to minimum digit
    delta_to_min = {}
    for delta, d in possible:
        if delta not in delta_to_min or d < delta_to_min[delta]:
            delta_to_min[delta] = d
    # Compute max delta for this digit
    max_delta_i = max(delta_to_min.keys()) if delta_to_min else 0
    digit_data.append( (delta_to_min, max_delta_i) )

# Precompute the max_delta array for the remaining digits
max_delta = [0]*(n+1)
for i in range(n-1, -1, -1):
    current_max = digit_data[i][1]
    max_delta[i] = current_max + max_delta[i+1]

# Greedily choose the smallest digit at each step
current_remaining = k
result = []
for i in range(n):
    s = digits[i]
    delta_to_min, _ = digit_data[i]
    candidates = []
    for delta in delta_to_min:
        digit_val = delta_to_min[delta]
        rem_needed = current_remaining - delta
        if rem_needed < 0:
            continue
        max_remain = max_delta[i+1]
        if rem_needed <= max_remain and rem_needed >=0:
            candidates.append( (digit_val, delta) )
    if not candidates:
        print(-1)
        return
    # Sort candidates by digit first, then by delta
    candidates.sort()
    chosen_digit, chosen_delta = candidates[0]
    result.append(str(chosen_digit))
    current_remaining -= chosen_delta

if current_remaining != 0:
    print(-1)
else:
    print(''.join(result))

if name == 'main':
main()