Matrix Fraud

시간 제한1초메모리 제한2048 MB

요약
모든 행과 열에 1이 있고 각 행의 1이 연속이며 행 구간이 오른쪽으로 단조 이동하도록 만드는 최소 토글 횟수를 구한다.
난이도

어려움10점 중 8점

유형
동적 계획법, 그리디, 누적 합, 구현
정답자
아직 제출이 없습니다

문제

For the purposes of this problem: A matrix is a binary matrix if all its entries are 00 or 11. A matrix is a banded binary matrix if its rows and columns satisfy the following properties:

  1. Every row has at least one 11.
  2. Every column has at least one 11.
  3. All 11s in each row are contiguous.
  4. For row ii, if s_is\_i is the leftmost column that has a 11 entry and t_it\_i is the rightmost column that has a 11 entry, then it must satisfy s_i≥s_i−1s\_i \ge s\_{i-1} and t_i≥t_i−1t\_i \ge t\_{i-1} for i>1i > 1.

Detecting banded binary matrices is an important method used in various fields like biology, paleontology, and linguistics to unearth clusters in data sets. Unfortunately, a group called the Immoral Cartel of Pure Cozeners (ICPC) has decided to do the unthinkable: manipulate data! The ICPC wishes to present their groundbreaking scientific results, but the scientific community will not take their results seriously because their matrices may not be banded. To have publishable results, they want to toggle some cells such that their data is a banded binary matrix.

The ICPC gives you its raw data, represented as a binary matrix. They want to toggle some cells (meaning, change a 00 to a 11 or a 11 to a 00) so that the resulting matrix is a banded binary matrix, as defined above. What is the fewest number of toggles needed to turn the given matrix into a banded binary matrix?

입력

The first line of input contains two integers rr and cc (1≤r×c≤2⋅1051 \leq r \times c \leq 2 \cdot 10^5), which are the dimensions of the matrix. The matrix has rr rows and cc columns.

Each of the next rr lines contains a string of binary digits of length cc. This is the matrix.

출력

Output a single integer, which is the minimum number of entries in the matrix to toggle to make the input matrix a binary banded matrix.

예제1

  1. 예제 1

    입력
    3 4
    1100
    0101
    0011
    
    예상 출력
    1