给定N x N大小的零矩阵,任务是在执行Q查询后查找包含偶数的像元数。每个查询将采用(X,Y)的形式,这样对于给定的单元格(X,Y),必须在第X行和第Y列中的所有单元格上执行递增操作。
注意:最初的0也应视为计数中的偶数。
例子:
Input: N = 2, Q = { {1, 1}, {1, 2}, {2, 1} }
Output: 2
Explanation:
In the first query, we add 1
to all elements of row 1 and column 1.
Our matrix become
2 1
1 0
In the second query, we add 1
to all elements of row 1 and column 2.
Our matrix become
3 3
1 1
In the last query, we add 1
to all elements of row 2 and column 1.
Our matrix finally become
4 3
3 2
In the final updated matrix, there
are 2 even numbers 4 and 3
respectively, hence ans is 2
Input: N = 2, Q = { {1, 1} }
Output: 1
天真的方法:天真的方法是根据查询更新矩阵中的每个单元。然后遍历矩阵以找出偶数单元。
时间复杂度: O(N 2 )
高效方法:
- 维护两个数组,一个数组用于行操作,另一个数组用于列操作。
- 行[Ⅰ]将操作的次数存储在第i + 1行和col [I]将操作的次数存储在第i + 1列中。
- 如果将1添加到第i行,我们将基于基于0的索引执行row [i-1] ++。
- 列也一样。
- 现在,我们需要观察奇数+奇数=偶数和偶数+偶数=偶数。
- 因此,如果row [i]和col [j]均为奇数或偶数,则该单元格将具有偶数值。
- 因此,我们只需要计算两个数组中的奇数和偶数值并将它们相乘即可。
下面是上述方法的实现:
C++
// C++ program find Number of Even cells
// in a Zero Matrix after Q queries
#include
using namespace std;
// Function to find the number of
// even cell in a 2D matrix
int findNumberOfEvenCells(int n, int q[][2], int size)
{
// Maintain two arrays, one for rows operation
// and one for column operation
int row[n] = { 0 };
int col[n] = { 0 };
for (int i = 0; i < size; i++) {
int x = q[i][0];
int y = q[i][1];
// Increment operation on row[i]
row[x - 1]++;
// Increment operation on col[i]
col[y - 1]++;
}
int r1 = 0, r2 = 0;
int c1 = 0, c2 = 0;
// Count odd and even values in
// both arrays and multiply them
for (int i = 0; i < n; i++) {
// Count of rows having even numbers
if (row[i] % 2 == 0) {
r1++;
}
// Count of rows having odd numbers
if (row[i] % 2 == 1) {
r2++;
}
// Count of columns having even numbers
if (col[i] % 2 == 0) {
c1++;
}
// Count of columns having odd numbers
if (col[i] % 2 == 1) {
c2++;
}
}
int count = r1 * c1 + r2 * c2;
return count;
}
// Driver code
int main()
{
int n = 2;
int q[][2] = { { 1, 1 },
{ 1, 2 },
{ 2, 1 } };
int size = sizeof(q) / sizeof(q[0]);
cout << findNumberOfEvenCells(n, q, size);
return 0;
}
Java
// Java program find Number of Even cells
// in a Zero Matrix after Q queries
class GFG
{
// Function to find the number of
// even cell in a 2D matrix
static int findNumberOfEvenCells(int n, int q[][], int size)
{
// Maintain two arrays, one for rows operation
// and one for column operation
int row[] = new int[n] ;
int col[] = new int[n] ;
for (int i = 0; i < size; i++)
{
int x = q[i][0];
int y = q[i][1];
// Increment operation on row[i]
row[x - 1]++;
// Increment operation on col[i]
col[y - 1]++;
}
int r1 = 0, r2 = 0;
int c1 = 0, c2 = 0;
// Count odd and even values in
// both arrays and multiply them
for (int i = 0; i < n; i++)
{
// Count of rows having even numbers
if (row[i] % 2 == 0)
{
r1++;
}
// Count of rows having odd numbers
if (row[i] % 2 == 1)
{
r2++;
}
// Count of columns having even numbers
if (col[i] % 2 == 0)
{
c1++;
}
// Count of columns having odd numbers
if (col[i] % 2 == 1)
{
c2++;
}
}
int count = r1 * c1 + r2 * c2;
return count;
}
// Driver code
public static void main (String[] args)
{
int n = 2;
int q[][] = { { 1, 1 },
{ 1, 2 },
{ 2, 1 } };
int size = q.length;
System.out.println(findNumberOfEvenCells(n, q, size));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 program find Number of Even cells
# in a Zero Matrix after Q queries
# Function to find the number of
# even cell in a 2D matrix
def findNumberOfEvenCells(n, q, size) :
# Maintain two arrays, one for rows operation
# and one for column operation
row = [0]*n ;
col = [0]*n
for i in range(size) :
x = q[i][0];
y = q[i][1];
# Increment operation on row[i]
row[x - 1] += 1;
# Increment operation on col[i]
col[y - 1] += 1;
r1 = 0;
r2 = 0;
c1 = 0;
c2 = 0;
# Count odd and even values in
# both arrays and multiply them
for i in range(n) :
# Count of rows having even numbers
if (row[i] % 2 == 0) :
r1 += 1;
# Count of rows having odd numbers
if (row[i] % 2 == 1) :
r2 += 1;
# Count of columns having even numbers
if (col[i] % 2 == 0) :
c1 +=1;
# Count of columns having odd numbers
if (col[i] % 2 == 1) :
c2 += 1;
count = r1 * c1 + r2 * c2;
return count;
# Driver code
if __name__ == "__main__" :
n = 2;
q = [ [ 1, 1 ],
[ 1, 2 ],
[ 2, 1 ] ];
size = len(q);
print(findNumberOfEvenCells(n, q, size));
# This code is contributed by AnkitRai01
C#
// C# program find Number of Even cells
// in a Zero Matrix after Q queries
using System;
class GFG
{
// Function to find the number of
// even cell in a 2D matrix
static int findNumberOfEvenCells(int n, int [,]q, int size)
{
// Maintain two arrays, one for rows operation
// and one for column operation
int []row = new int[n] ;
int []col = new int[n] ;
for (int i = 0; i < size; i++)
{
int x = q[i, 0];
int y = q[i, 1];
// Increment operation on row[i]
row[x - 1]++;
// Increment operation on col[i]
col[y - 1]++;
}
int r1 = 0, r2 = 0;
int c1 = 0, c2 = 0;
// Count odd and even values in
// both arrays and multiply them
for (int i = 0; i < n; i++)
{
// Count of rows having even numbers
if (row[i] % 2 == 0)
{
r1++;
}
// Count of rows having odd numbers
if (row[i] % 2 == 1)
{
r2++;
}
// Count of columns having even numbers
if (col[i] % 2 == 0)
{
c1++;
}
// Count of columns having odd numbers
if (col[i] % 2 == 1)
{
c2++;
}
}
int count = r1 * c1 + r2 * c2;
return count;
}
// Driver code
public static void Main ()
{
int n = 2;
int [,]q = { { 1, 1 },
{ 1, 2 },
{ 2, 1 } };
int size = q.GetLength(0);
Console.WriteLine(findNumberOfEvenCells(n, q, size));
}
}
// This code is contributed by AnkitRai01
输出:
2
时间复杂度: O(N)