結果

問題 No.3083 One Two
ユーザー qwewe
提出日時 2025-05-14 13:18:19
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 6,392 bytes
コンパイル時間 248 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 66,944 KB
最終ジャッジ日時 2025-05-14 13:19:12
合計ジャッジ時間 1,538 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 3
other RE * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

import datetime
import sys

# Function to check if a year is a leap year according to the Gregorian calendar rules.
# A year is a leap year if it is divisible by 4, unless it is divisible by 100 but not by 400.
def is_leap(year):
    """Checks if a year is a leap year."""
    return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)

def solve():
    # Read integer inputs Y, N, D from standard input.
    # Y: The year the students entered junior high school.
    # N: The number of students.
    # D: The number of days after April 1st, Y, 8 PM.
    Y, N, D = map(int, sys.stdin.readline().split())

    # Define the reference start date: April 1st of year Y.
    start_date = datetime.date(Y, 4, 1)
    
    # Calculate the target date d_D by adding D days to the start date.
    # The datetime library automatically handles month ends, year ends, and leap years.
    target_date = start_date + datetime.timedelta(days=D)
    # Extract the year, month, and day of the target date.
    Y_D = target_date.year
    M_D = target_date.month
    Day_D = target_date.day
    
    # Determine if year Y is a leap year. L_Y will be 1 if leap, 0 otherwise.
    L_Y = 1 if is_leap(Y) else 0
    
    # Calculate the size of the set S1.
    # S1 includes dates from January 1st to April 1st inclusive, in year Y.
    # The number of days is Jan(31) + Feb(28 or 29) + Mar(31) + Apr(1) = 31 + (28+L_Y) + 31 + 1 = 91 + L_Y.
    S1_size = 91 + L_Y
    
    # Calculate the size of the set S2.
    # S2 includes dates from April 2nd to December 31st inclusive, in year Y.
    # Total days in year Y is K_Y = 365 + L_Y.
    # S2_size = K_Y - S1_size = (365 + L_Y) - (91 + L_Y) = 274.
    # This size is constant regardless of whether Y is a leap year.
    S2_size = 274 
    
    min_val = 0 # Initialize minimum possible count of students still 12
    max_val = 0 # Initialize maximum possible count of students still 12

    # The logic depends on whether the target date d_D falls in the same year Y or the next year Y+1.
    # This transition occurs after D=274 days (Dec 31 is 274 days after Apr 1).
    if Y_D == Y:
        # Case 1: Target date d_D is within year Y. This happens when D <= 274.
        # Students still 12 are those from S1 plus those from S2 whose birthday is after d_D.
        
        # k = size of the subset of S2 with dates strictly after d_D.
        # The number of days from d_D (exclusive) to Dec 31.
        # This equals the total days in S2 (274) minus the number of days from Apr 2 to d_D (inclusive), which is D.
        # So, k = 274 - D.
        k = 274 - D 
        
        # k1 = size of S1.
        k1 = S1_size
        # k2b = size of the subset of S2 with dates on or before d_D.
        # These are dates from Apr 2 up to d_D. The number of such dates is exactly D.
        k2b = D
        
        # Calculate minimum value:
        # The number of students still 12 is N_1 + N_{2, after d_D}. This can be rewritten as N - N_{2, before d_D}.
        # To minimize this value, we maximize N_{2, before d_D}.
        # The maximum number of students whose birthdays can fall into S_{2, before d_D} is min(N, k2b).
        # So, min_val = N - min(N, k2b) = max(N - k2b, 0).
        min_val = max(0, N - k2b)
        
        # Calculate maximum value:
        # The number of students still 12 is N_1 + N_{2, after d_D}.
        # To maximize this value, we prioritize selecting students whose birthdays are in S1 or S_{2, after d_D}.
        # The maximum number of such students is min(N, k1 + k).
        max_val = min(N, k1 + k)

    else: # Y_D == Y + 1
        # Case 2: Target date d_D is in year Y+1. This happens when D > 274.
        # Students still 12 are only those from S1 whose birthday (M, D) is calendrically after (M_D, Day_D).
        
        # Calculate k1a: Number of dates in S1 that are calendrically after (M_D, Day_D).
        # We iterate through all dates in S1 and count how many satisfy the condition.
        k1a = 0 # Initialize count
        
        # Start iteration from Jan 1, Y.
        date_iter = datetime.date(Y, 1, 1)
        # End iteration at Apr 1, Y (inclusive).
        end_date_S1 = datetime.date(Y, 4, 1)

        while True:
            # Get month and day of the current date in the iteration.
            current_M = date_iter.month
            current_Day = date_iter.day
            
            # Check if the current date (M, D) is calendrically after the target date's month and day (M_D, Day_D).
            # A date (M1, D1) is after (M2, D2) if M1 > M2 or (M1 == M2 and D1 > D2).
            is_after = False
            if current_M > M_D:
                is_after = True
            elif current_M == M_D:
                if current_Day > Day_D:
                    is_after = True

            # If the current date is after (M_D, Day_D), increment the counter k1a.
            if is_after:
                 k1a += 1
                 
            # Break the loop if we have processed the last date in S1 (April 1st).
            if date_iter == end_date_S1:
                break
            
            # Move to the next day. datetime handles increments correctly across month/year boundaries and leap years.
            date_iter += datetime.timedelta(days=1)

        # k1 = Total size of S1.
        k1 = S1_size 
        # k1b = Size of S1 subset on or before (M_D, Day_D). Calculated as total size minus the 'after' part.
        k1b = k1 - k1a 
        # k2 = Total size of S2.
        k2 = S2_size

        # Calculate minimum value:
        # The number of students still 12 is N_{1, after d_D} = N_{1a}.
        # To minimize N_{1a}, we prioritize selecting students whose birthdays are from S_{1, before d_D} and S2.
        # The total capacity outside S_{1, after d_D} is k1b + k2.
        # The minimum number of students we must select from S_{1, after d_D} is max(0, N - (k1b + k2)).
        min_val = max(0, N - (k1b + k2))
        
        # Calculate maximum value:
        # To maximize N_{1a}, we prioritize selecting students whose birthdays are from S_{1, after d_D}.
        # The maximum number of students we can select from S_{1, after d_D} is min(N, k1a).
        max_val = min(N, k1a)

    # Print the calculated minimum and maximum values separated by a space.
    print(f"{min_val} {max_val}")

# Execute the solve function when the script is run.
solve()
0