結果

問題 No.3099 Parentheses Decomposition
ユーザー qwewe
提出日時 2025-05-14 12:59:49
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 4,941 bytes
コンパイル時間 138 ms
コンパイル使用メモリ 82,512 KB
実行使用メモリ 67,488 KB
最終ジャッジ日時 2025-05-14 13:01:15
合計ジャッジ時間 2,751 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 4
other RE * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
import sys

# Function to calculate accumulated night hours from time 0 up to time t.
# Night time is defined as the intervals [18 + 24k, 24 + 24k) and [0 + 24k, 6 + 24k) for integer k.
# This function computes the length of the intersection of [0, t) with the union of all night intervals.
def calculate_night_hours(t):
    """
    Calculates the total duration of night time within the interval [0, t) hours.
    Night is defined as 18:00 to 6:00 (next day), which corresponds to intervals
    [18, 24) and [0, 6) within each 24-hour cycle.
    
    Args:
        t (float): The time point (in hours from time 0) up to which the accumulated
                   night hours need to be calculated.

    Returns:
        float: The total accumulated night hours in the interval [0, t).
    """
    # Use a small tolerance for floating point comparisons, especially comparing with 0.
    epsilon = 1e-9
    
    # If t is effectively zero or negative, accumulated night time is 0.
    if t < epsilon:
        return 0.0

    # Calculate the number of full 24-hour days completed.
    # math.floor ensures correct handling even if t/24.0 is slightly less than an integer
    # due to floating point representation.
    days = math.floor(t / 24.0)
    
    # Calculate the remaining time within the last (possibly partial) day.
    time_in_day = t - days * 24.0

    # Adjust for potential floating point inaccuracies where t is extremely close
    # to a multiple of 24. If time_in_day is almost 24.0, it means we've completed
    # another full day, so we treat it as the start of the next day (time 0.0)
    # and increment the number of full days.
    if abs(time_in_day - 24.0) < epsilon:
         days += 1
         time_in_day = 0.0
    # Adjust if time_in_day calculation results in a tiny negative number close to zero,
    # which can happen due to precision limits in the subtraction t - days * 24.0.
    elif time_in_day < 0 and abs(time_in_day) < epsilon:
         time_in_day = 0.0
         
    # Ensure time_in_day is non-negative after potential adjustments
    # This helps prevent issues in the subsequent logic if time_in_day was unexpectedly negative.
    time_in_day = max(0.0, time_in_day)


    # Each full day contributes 12 hours of night time.
    # This is 6 hours from the evening interval [18, 24) and 6 hours from the morning interval [0, 6).
    night_hours_full_days = days * 12.0
    
    # Calculate the night hours within the last partial day interval [0, time_in_day).
    night_hours_last_day = 0.0
    
    # The calculation depends on where time_in_day falls relative to the night intervals [0, 6) and [18, 24).
    if time_in_day <= 6.0:
        # If time_in_day is within [0, 6], the entire duration [0, time_in_day) is night time.
        night_hours_last_day = time_in_day
    elif time_in_day <= 18.0:
        # If time_in_day is within (6, 18], the night portion is only the [0, 6) interval.
        # The interval (6, time_in_day] is daytime.
        night_hours_last_day = 6.0
    else: # time_in_day > 18.0 (and time_in_day < 24.0 due to the earlier adjustments)
        # If time_in_day is within (18, 24), the night portion includes the full [0, 6) interval
        # and the interval [18, time_in_day).
        night_hours_last_day = 6.0 + (time_in_day - 18.0)

    # Total accumulated night hours is the sum from the full days and the last partial day.
    return night_hours_full_days + night_hours_last_day

def solve():
    """
    Reads input, calculates the total night driving time, and prints the result.
    """
    # Read input values for start time T, speed S, and distance D from standard input.
    T, S, D = map(int, sys.stdin.readline().split())

    # Calculate the start time in hours (absolute time from 00:00 on day 0).
    start_time_abs = float(T)
    
    # Calculate the total travel time in hours.
    # Use floating point division to handle potential fractional results.
    total_travel_time = float(D) / float(S)
    
    # Calculate the end time in hours (absolute time).
    end_time_abs = start_time_abs + total_travel_time

    # The total night driving time during the journey interval [start_time_abs, end_time_abs)
    # is the difference in accumulated night hours between the end time and the start time.
    # This relies on the property that calculate_night_hours(t) represents the integral
    # of a function that is 1 during night time and 0 during day time.
    result = calculate_night_hours(end_time_abs) - calculate_night_hours(start_time_abs)

    # Print the result to standard output.
    # Use f-string formatting with a high number of decimal places (e.g., 17)
    # to ensure the output meets the required precision (absolute or relative error <= 10^-5).
    print(f"{result:.17f}")

# Execute the solve function when the script is run.
# This is standard practice in competitive programming.
if __name__ == '__main__':
    solve()
0