Taskcproblem calendar

Joined
Aug 30, 2023
Messages
4
Reaction score
0
Here is the task:
Analyzing weekends and holidays around New Year's and May 1st in Russia, the president of Flatlandia has concluded that the rest of its citizens can be drastically optimized. The main goal is to ensure that citizens do not have to work more than 6 consecutive days in a calendar year. The president has instructed the Ministry of Labor to develop a schedule for transferring weekends (Saturdays and Sundays) so that citizens can have as many consecutive rest days as possible.

It is important to note that if a Flatlandian public holiday falls on a weekend (Saturday or Sunday), that holiday automatically moves to the first working day after the holiday. However, according to the president's decree, any weekend, whether it coincides with a public holiday or not, can be moved to any working day. Public holidays, however, are never moved.

Write a program that will help the Ministry of Labor create the required schedule for transferring weekends in the upcoming year. Public holidays and weekends from the previous and following year should not be considered. The objective is to maximize the number of consecutive rest days in one year.

Input Format:

The first line of input contains two numbers - the year number (Y) (2012≤Y≤2050) and the day of the week for January 1st of that year (W) (1≤W≤7, from Monday to Sunday). In this range of years, leap years are divisible by 4.

The second line contains the number of annual public holidays (N) in Flatlandia. Each of the following N lines contains the date of the next holiday in the format DD.MM. The holiday dates are listed in chronological order, and all dates are valid and correct for the given year.

Output Format:

Output a single number - the maximum possible number of consecutive days off for the residents of Flatlandia in the specified year if the weekends are moved so that the number of consecutive working days in this year does not exceed 6.

Example:

Input:

2012 7

1

01.01

Output:

63


My try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Code:
import datetime
 
def is_leap_year(year):
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                return True
            else:
                return False
        else:
            return True
    else:
        return False
 
def get_weekday(year, month, day):
    date = datetime.date(year, month, day)
    return date.weekday()
 
def get_weekdays_in_year(year):
    weekdays = [0, 0, 0, 0, 0, 0, 0]  # Sunday to Saturday
    for month in range(1, 13):
        days_in_month = 31
        if month == 4 or month == 6 or month == 9 or month == 11:
            days_in_month = 30
        elif month == 2:
            if is_leap_year(year):
                days_in_month = 29
            else:
                days_in_month = 28
        for day in range(1, days_in_month + 1):
            weekday = get_weekday(year, month, day)
            weekdays[weekday] += 1
    return weekdays
 
def get_maximum_consecutive_holidays(year, start_weekday, holidays):
    weekdays = get_weekdays_in_year(year)
    consecutive_holidays = 0
    current_consecutive_holidays = 0
    for holiday in holidays:
        day, month = map(int, holiday.split('.'))
        weekday = get_weekday(year, month, day)
        if weekdays[weekday] == 0:  # Weekday is not already a holiday
            current_consecutive_holidays += 1
        else:
            current_consecutive_holidays = 0
        weekdays[weekday] += 1
        if current_consecutive_holidays > consecutive_holidays:
            consecutive_holidays = current_consecutive_holidays
    return consecutive_holidays
 
 
year, start_weekday = map(int, input().split())
num_holidays = int(input())
holidays = []
for _ in range(num_holidays):
    holiday = input()
    holidays.append(holiday)
 
maximum_consecutive_holidays = get_maximum_consecutive_holidays(year, start_weekday, holidays)
 
 
print(maximum_consecutive_holidays)
But uhhh, it doesn't complete the first test, help plzz
Yah its about russia, so sorry..
 
Joined
Aug 30, 2023
Messages
4
Reaction score
0
Here is algorith, help to realize
Data Reading: The first step is to read the input data, which is the year and the day of the week of January 1, as well as the dates of all holidays.

Calendar Generation: The next step is to create a dictionary, i.e., a calendar for the given year, taking into account whether the year is a leap year or not. Weekdays are marked as 0, Sundays and Saturdays as 1.

Holiday Indication: Then, iterate through the list of holidays, and if holiday falls on a weekday, mark it as 2, replacing the corresponding element in the calendar (not 1 because it cannot be moved when creating the longest chain); if it falls on a weekend, place a one here (i.e., Sunday or Saturday), but replace the next working day after the weekend with festive (i.e., 2).

Calculations and Weekend Transposition: With this internal calendar representation, you now need to calculate how many weekends can be moved to weekdays in such a way as to maximize the number of consecutive days of rest without violating the condition "no more than 6 working days in a row". When creating a chain, swap 0 and 1. It can be seen that under a normal schedule, weekdays always go 5 in a row, so all Sundays can be boldly changed to weekdays and a chain can be built from them. Also, since 2 cannot be moved, they should be included in a chain of 2 and 1, and it is necessary to ensure that the chain includes a maximum number of twos (but they cannot be moved).

Counting the Maximum Continuity Value: Finally, walk through the resulting calendar and count the maximum number of consecutive days off (days marked '1' or '2').

Answer Output: Once the maximum duration of consecutive days off has been found, print that number.
 
Joined
Sep 21, 2022
Messages
122
Reaction score
15
"The objective is to maximize the number of consecutive rest days in one year."

This statement is open to interpretation.

I read it as TOTAL consecutive rest days. You seem to be talking about the longest single group of days off.
 
Joined
Aug 30, 2023
Messages
4
Reaction score
0
"The objective is to maximize the number of consecutive rest days in one year."

This statement is open to interpretation.

I read it as TOTAL consecutive rest days. You seem to be talking about the longest single group of days off.
here is my the closest code, pls helpp:

Code:
a, b = map(int, input().split())
c = int(input())
copy_c = c
 
 
def func1(dt):
    e, f = map(int, dt.split('.'))
    g = {
        1: 31,
        2: 29 if a % 4 == 0 else 28,
        3: 31,
        4: 30,
        5: 31,
        6: 30,
        7: 31,
        8: 31,
        9: 30,
        10: 31,
        11: 30,
        12: 31
    }
    return sum(g[u] for u in range(1, f)) + e
 
 
h = [func1(input()) - 1 for _ in range(c)]
i = 366 if a % 4 == 0 else 365
 
j = 0
for _ in range(i):
    b %= 7
    if b in {0, 6}:
        j += 1
    b += 1
 
k = [0] * i
for l in h:
    k[l] = 2
 
b = 0
for l in range(i - 1, -1, -1):
    if k[l] == 0:
        b += 1
    elif k[l] == 2:
        b = 0
    if b == 7:
        k[l] = 1
        j -= 1
        b = 0
 
 
def func2(n):
    o = p = 0
    for q in n:
        if q in {1, 2}:
            p += 1
        else:
            o = max(o, p)
            p = 0
    o = max(o, p)
    return o
 
 
r = 0
for s in range(i):
    k_copy = k.copy()
    jc = j
    b = 0
    for t in range(s):
        if k_copy[t] == 0:
            b += 1
        elif k_copy[t] == 1:
            if b < 6:
                b += 1
                k_copy[t] = 0
                jc += 1
            else:
                b = 0
        elif k_copy[t] == 2:
            b = 0
    if b == 7:
        copy_t = t
        while k_copy[copy_t] == 0:
            copy_t -= 1
        k_copy[copy_t] = 1
        jc -= 1
        b = 0
    for t in range(s, i):
        if jc == 0:
            break
        if k_copy[t] == 0:
            k_copy[t] = 1
            jc -= 1
        else:
            continue
    r = max(r, func2(k_copy))
 
print(r - copy_c)
 
Joined
Sep 21, 2022
Messages
122
Reaction score
15
This is how I would do it.

block: The largest group of consecutive days off.

satsun: ordinary day off

There are 365 (or 366) places where the block could start. I would perform a simple test for each of those 365 cases.

Start with an array of 365 zeros. That is the year.

Put a 1 at each holiday position.

From here on, the problem has nothing to do with the calendar.

If there are 7 consecutive zeros, place a satsun in the 7th position.

Now the array is ready for the test cases.

A starting position is valid if there is a 0 to the left, or the starting position is the start of the year.

Move right from the start position, replacing zeros with your remaining satsun.

Before counting the block, there may be one satsun after the block that can be moved left, to join the block. Test if that is valid.

Count the block, if the block's end is beyond the end of year, ignore the case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top