2019 Meituan Machine Learning/Data Mining Algorithm Intern Written Exam Programming Question Modification Matrix

Mainly used for communication and thinking

1. Modify the matrix

时间限制:C/C++语言 1000MS;其他语言 3000MS
内存限制:C/C++语言 65536KB;其他语言 589824KB题目描述:
我们称一个矩阵为黑白矩阵,当且仅当对于该矩阵中每一个位置如(i,j),其上下左右四个方向的数字相等,即(i-1,j),(i+1,j),(i,j+1),(i,j-1)四个位置上的数字两两相等且均不等于(i,j)位置上的数字。(超出边界的格子忽略)
现在给出一个n*m的矩阵,我们想通过修改其中的某些数字,使得该矩阵成为黑白矩阵,问最少修改多少个数字。
输入
第一行包含两个整数n,m,表示矩阵的长宽。(1≤n,m≤100000,1≤n*m≤100000)
接下来有n行,每行包含m个整数,中间用空格隔开,表示n*m的矩阵。
输出
输出仅包含一个数字,表示该矩阵想修改为黑白矩阵最少修改的数字数量。

样例输入
3 3
1 1 1
1 1 1
1 1 1
样例输出
4
提示
补充样例
输入样例2
3 3
1 1 1
1 5 1
1 1 1
输出样例2
4

My thinking: The
question says n rows and m columns. According to my personal habits, I reversed it
and solved it directly with violence. Row m, column n
1. Starting from row 0, store each value of the row into the dictionary alternately one by one a,b, the key is the value, and the value is the number of times the value appears.
If the row is even (index starts from 0), the storage order is a dictionary a,b; otherwise, it is b,a. and the final sum of the values ​​in a and the sum of the values ​​in b is always 1 difference, and the sum is m*n.
2. Sort the dictionary in reverse order of values, that is, the keys with the most occurrences are listed first.
3. Two for loops (thought to be time-consuming, but actually okay) compare the keys with the largest number of occurrences one by one.
4. If the two keys are different, the number m*n-v(k1)-v(k2)of . It means that the keys are different and the current number of occurrences is the highest, and it is only necessary to modify the remaining different keys to the current keys respectively.
E.g:

1 3 1 
3 1 3
1 1 3

In this matrix, the dictionary a is: {'1':4,'3':1}, and the dictionary b is {'3':3,'1':1}. According to my thinking, that is, the keys with the highest number of occurrences are different, just change the key that is not 1 in a to 1, and the value that is not 3 in b is changed to 3. That is, the minimum number of modifications is 3*3-4-3=2. Return directly.
5. If the keys with the highest number of occurrences are the same, the number of occurrences is compared, and two cases of 6 and 7 are considered at this time.
6. At this time, there is only one number in the matrix, for example, all 1s. At this time, the sum of the two occurrences for comparison is the sum of the two occurrences m*n, then the value with the smaller occurrence is recorded as the number of modifications. Refer to Example 1
7. At this time, there are two or more numbers in the matrix, compare v(k1) and v(k2), keep the current larger value, take the next largest value of another group, and loop 4-7.

Criticism, correction, correction

Code:

# m,n
# matrix1 raw col
# a_dict
# b_dict
# num

"""
该段为题目中的标准输入,每行读取,并存入矩阵
"""
m,n = map(int ,input().split(' '))
matrix = []
raw = []
for i in range(m):
    raw = [x for x in input().split(' ')]
    matrix.append(raw)

#print(m,n)
#print(matrix)

# i=(0,0) or i=(0,1)
a_dict = {
    
    }
b_dict = {
    
    }

"""
该段为测试,建立随机数大矩阵,和元矩阵。
并记录不同过程的时间
"""
import numpy as np
import time

matrix = np.random.randint(0,10,size=[1,100000])
#matrix = np.ones((1000,100))

m = 1
n =100000
"""
该段为字典更新,其中以行为外循环,列以每2步一个循环,每次确保不越界。
交叉存储a,b。
"""
m1 = time.time()
for i in range(m):
    if i%2==0:
        for j in range(0,n,2):
            a_dict[matrix[i][j]] = a_dict.get(matrix[i][j],0)+1
            if j+1< n:
                b_dict[matrix[i][j+1]] = b_dict.get(matrix[i][j+1],0)+1 
    else:
        for j in range(0,n,2):
            b_dict[matrix[i][j]] = b_dict.get(matrix[i][j],0)+1
            if j+1 < n:
                a_dict[matrix[i][j+1]] = a_dict.get(matrix[i][j+1],0)+1

m2 = time.time()
"""
该段为字典排序,此处需要注意是,排序后是一个列表
"""
a_dict = sorted(a_dict.items(),key=lambda item:item[1],reverse=True)
#print(a_dict)
b_dict = sorted(b_dict.items(),key=lambda item:item[1],reverse=True)
#print(b_dict)
"""
该段是计算修改次数。思路如上
"""
m3 = time.time()
def compute_result(a_dict,b_dict):
    result = 0
    for a in a_dict:
        for b in b_dict:
            if a[0]!=b[0]:
                result = (m*n)-a[1]-b[1]
                return result
            else:
                if a[1]>=b[1]:
                    result = b[1]
                    continue
                else:
                    result = a[1]
                    break
    return result

print(compute_result(b_dict,a_dict))
m4 = time.time()

print(m2-m1)
print(m3-m2)
print(m4-m3)

The samples I tested myself were given by the title and randomly created using numpy. Due to the limitation in the title (1≤n,m≤100000,1≤n*m≤100000), when I created a random matrix test, I used a (100000,1)和(10000,10)和(1000,100)matrix of shape , and the time did not exceed 1 second.

┗|`O′|┛ Ow~~! ! Time to brush up.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326292485&siteId=291194637