[Python题解] CodeForces 1804 D. Accommodation

26 篇文章 0 订阅
本文描述了一个关于编程挑战的问题,来自CodeForces的1804D题目。问题涉及计算给定夜间建筑照片中,基于灯光情况的最少和最多可能的占用公寓数量。每个楼层有固定数量的一室和两室公寓,每个公寓对应一定数量的窗户。解决方案包括解析输入,计算每层楼不同公寓布局下的亮灯窗户数量,从而确定占用公寓的范围。
摘要由CSDN通过智能技术生成

✅作者简介:人工智能专业本科在读,喜欢计算机与编程,写博客记录自己的学习历程。
🍎个人主页:小嗷犬的个人主页
🍊个人网站:小嗷犬的技术小站
🥭个人信条:为天地立心,为生民立命,为往圣继绝学,为万世开太平。



Title

CodeForces 1804 D. Accommodation

Time Limit

2 seconds

Memory Limit

512 megabytes

Problem Description

Annie is an amateur photographer. She likes to take pictures of giant residential buildings at night. She just took a picture of a huge rectangular building that can be seen as a table of n × m n \times m n×m windows. That means that the building has n n n floors and each floor has exactly m m m windows. Each window is either dark or bright, meaning there is light turned on in the room behind it.

Annies knows that each apartment in this building is either one-bedroom or two-bedroom. Each one-bedroom apartment has exactly one window representing it on the picture, and each two-bedroom apartment has exactly two consecutive windows on the same floor. Moreover, the value of m m m is guaranteed to be divisible by 4 4 4 and it is known that each floor has exactly m 4 \frac{m}{4} 4m two-bedroom apartments and exactly m 2 \frac{m}{2} 2m one-bedroom apartments. The actual layout of apartments is unknown and can be different for each floor.

Annie considers an apartment to be occupied if at least one of its windows is bright. She now wonders, what are the minimum and maximum possible number of occupied apartments if judged by the given picture?

Formally, for each of the floors, she comes up with some particular apartments layout with exactly m 4 \frac{m}{4} 4m two-bedroom apartments (two consecutive windows) and m 2 \frac{m}{2} 2m one-bedroom apartments (single window). She then counts the total number of apartments that have at least one bright window. What is the minimum and maximum possible number she can get?

Input

The first line of the input contains two positive integers n n n and m m m ( 1 ≤ n ⋅ m ≤ 5 ⋅ 1 0 5 1 \leq n \cdot m \leq 5 \cdot 10^5 1nm5105) — the number of floors in the building and the number of windows per floor, respectively. It is guaranteed that m m m is divisible by 4 4 4.

Then follow n n n lines containing m m m characters each. The j j j-th character of the i i i-th line is “0” if the j j j-th window on the i i i-th floor is dark, and is “1” if this window is bright.

Output

Print two integers, the minimum possible number of occupied apartments and the maximum possible number of occupied apartments, assuming each floor can have an individual layout of m 4 \frac{m}{4} 4m two-bedroom and m 2 \frac{m}{2} 2m one-bedroom apartments.

Sample Input

5 4
0100
1100
0110
1010
1011

Sample Onput

7 10

Note

In the first example, each floor consists of one two-bedroom apartment and two one-bedroom apartments.

The following apartment layout achieves the minimum possible number of occupied apartments equal to 7 7 7.

|0 1|0|0|
|1 1|0|0|
|0|1 1|0|
|1|0 1|0|
|1|0|1 1|

The following apartment layout achieves the maximum possible number of occupied apartments equal to 10 10 10.

|0 1|0|0|
|1|1 0|0|
|0 1|1|0|
|1|0 1|0|
|1 0|1|1|

Source

CodeForces 1804 D. Accommodation


Solution

n, m = map(int, input().split())
smin = smax = 0

for i in range(n):
    s = input()

    two = j = 0
    # 将连续两盏灯都先视为两居室
    while j < m - 1:
        if s[j] == '1' and s[j + 1] == '1':
            j += 1
            two += 1
        j += 1
    two = min(two, m // 4)  # 两居室的数量不能超过总窗户数的四分之一
    smin += s.count('1') - two

    two = j = 0
    # 统计可能的不开灯的两居室和只开一盏灯的两居室数量
    while j < m - 1:
        if s[j] != '1' or s[j + 1] != '1':
            j += 1
            two += 1
        j += 1
    two = min(two, m // 4)  # 两居室的数量不能超过总窗户数的四分之一
    smax += s.count('1') - (m // 4 - two)  # (m // 4 - two) 为开两盏灯的两居室数量
print(smin, smax)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小嗷犬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值