Accommodation (Codeforces 1804D)
Tags: Greedy, brute force

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 \times m$$$ windows. That means that the building has $$$n$$$ floors and each floor has exactly $$$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$$$ is guaranteed to be divisible by $$$4$$$ and it is known that each floor has exactly $$$\frac{m}{4}$$$ two-bedroom apartments and exactly $$$\frac{m}{2}$$$ 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 $$$\frac{m}{4}$$$ two-bedroom apartments (two consecutive windows) and $$$\frac{m}{2}$$$ 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$$$ and $$$m$$$ ($$$1 \leq n \cdot m \leq 5 \cdot 10^5$$$) — the number of floors in the building and the number of windows per floor, respectively. It is guaranteed that $$$m$$$ is divisible by $$$4$$$.Then follow $$$n$$$ lines containing $$$m$$$ characters each. The $$$j$$$-th character of the $$$i$$$-th line is "0" if the $$$j$$$-th window on the $$$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 $$$\frac{m}{4}$$$ two-bedroom and $$$\frac{m}{2}$$$ one-bedroom apartments.

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$$$. |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$$$. |0 1|0|0||1|1 0|0||0 1|1|0||1|0 1|0||1 0|1|1|

Sample Inputs:
5 4
0100
1100
0110
1010
1011

1 8
01011100

Sample Outputs:
7 10

3 4

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (n, m) = readInts()
    mi = ma = 0
    for _ in range(n):
        S = readStr()
        v = m // 4
        val = sum([c == '1' for c in S])
        mi += val
        ma += val
        i = 0
        while i < m - 1 and v:
            if S[i] == S[i + 1] == '1':
                mi -= 1
                v -= 1
                i += 2
            else:
                i += 1
        (u, v) = (m // 2, m // 4)
        i = cnt = 0
        while i < m - 1 and v:
            if S[i] != S[i + 1]:
                v -= 1
                i += 2
            elif S[i] == S[i + 1] == '0':
                v -= 1
                i += 2
            else:
                i += 1
                cnt += 1
        ma -= v
    return (mi, ma)
print(*solve())

Solution (Python):
(x, y) = map(int, input().split())
(mx, mn) = (0, 0)
for _ in range(x):
    s = input()
    (a, b, c, i) = (s.count('1'), s.count('11'), 0, 0)
    while i < y - 1:
        if s[i] == '0' or s[i + 1] == '0':
            c += 1
            i += 1
        i += 1
    mn += max(0, a - min(b, y // 4))
    mx += max(0, a - max(0, y // 4 - c))
print(mn, mx)

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (n, m) = readInts()
    mi = ma = 0
    for _ in range(n):
        S = readStr()
        v = m // 4
        val = sum([c == '1' for c in S])
        mi += val
        ma += val
        i = 0
        while i < m - 1 and v:
            if S[i] == S[i + 1] == '1':
                mi -= 1
                v -= 1
                i += 2
            else:
                i += 1
        (u, v) = (m // 2, m // 4)
        i = 0
        exp = [0] * m
        while i < m - 1 and v:
            if S[i] == S[i + 1] == '1':
                i += 1
            elif S[i] == S[i + 1] == '0':
                v -= 1
                exp[i] = exp[i + 1] = 1
                i += 2
            else:
                v -= 1
                exp[i] = exp[i + 1] = 1
                i += 2
        if not v:
            continue
        i = 0
        while i < m - 1 and v:
            if exp[i]:
                i += 1
                continue
            if S[i] == S[i + 1] == '1':
                if u:
                    u -= 1
                    i += 1
                else:
                    ma -= 1
                    v -= 1
                    i += 2
            elif S[i] == S[i + 1] == '0':
                if v:
                    v -= 1
                    i += 2
                else:
                    u -= 1
                    i += 1
            elif v:
                v -= 1
                i += 2
            else:
                u -= 1
                i += 1
    return (mi, ma)
print(*solve())