给定一个整数K和一个大小为N * N 的方阵mat[][] ,其元素范围为[1, K] ,任务是计算删除不同矩阵元素对的方法,以便剩余元素可以排列在垂直或水平配对。
例子:
Input: mat[][] = {{1, 2}, {3, 4}}, K = 4
Output: 4
Explanation:
Remove the row {1, 2}. Therefore, remaining elements are {3, 4}, which can be arranged in a horizontal group of 2.
Similarly, removing pairs (1, 3), (3, 4) and (2, 4) can also form such arrangements.
Input: mat[][] = {{1, 2, 3}, {2, 2, 2}, {1, 2, 2}}, K = 3
Output: 0
方法:这个想法基于以下观察:
- 如果 N 是奇数,那么无论去除哪一对,剩余的矩阵都不能被较小的矩阵覆盖。这是因为,在删除矩阵的任何对时,剩余的元素将是奇数,不可能使用 2×1 或 1×2 大小的矩阵覆盖它们。
- 对于其他情况,涵盖剩余的 删除一对(a, b)后的矩阵只有在行索引i和列索引j的总和a是偶数而b是奇数时才可能,反之亦然。
请按照以下步骤解决问题:
- 如果矩阵的大小是奇数,则打印0,因为没有可能的排列。
- 否则,请检查以下内容:
- 将变量ans初始化为0以存储所需对的数量。
- 初始化一个大小为K×2的辅助矩阵dp[][] , dp[i][0]表示i在行索引和列索引之和为偶数的矩阵mat[][]中出现的次数,并且dp[i][1]表示行索引和列索引之和为奇数的mat[][]中i的出现次数。
- 使用行索引i和列索引j逐行遍历矩阵 mat [][] ,如果i和j 的和是偶数,则将dp[mat[i][j] – 1][0]增加 1,否则将dp[mat[i][j] – 1][1]增加1 。
- 迭代范围[0,K – 1]使用变量i和嵌套迭代在范围[I + 1,K – 1]使用变量j:
- 将dp[i][0] * dp[j][1]的值添加到ans 中,因为两个块的值不同,且(i + j)且 1 的值为奇数且(i + j) 的值为 other甚至。
- 添加DP的值[I]一种[1] * DP [j]的[0]至ANS既是块的值不同,并且第(i + j)的值是偶数和(i + j)的其他值是奇数
- 经过以上步骤,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count ways to remove pairs
// such that the remaining elements can
// be arranged in pairs vertically or horizontally
void numberofpairs(vector > v,
int k)
{
// Store the size of matrix
int n = v.size();
// If N is odd, then no
// such pair exists
if (n % 2 == 1) {
cout << 0;
return;
}
// Store the number of
// required pairs
int ans = 0;
// Initialize an auxiliary
// matrix and fill it with 0s
int dp[k][2];
for (int i = 0; i < k; i++) {
for (int j = 0; j < 2; j++) {
dp[i][j] = 0;
}
}
// Traverse the matrix v[][]
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Check if i+j is odd or even
if ((i + j) % 2 == 0)
// Increment the value
// dp[v[i][j]-1][0] by 1
dp[v[i][j] - 1][0]++;
else
// Increment the value
// dp[v[i][j]-1][1] by 1
dp[v[i][j] - 1][1]++;
}
}
// Iterate in range[0, k-1] using i
for (int i = 0; i < k; i++) {
// Iterate in range[i+1, k-1] using j
for (int j = i + 1; j < k; j++) {
// Update the ans
ans += dp[i][0] * dp[j][1];
ans += dp[i][1] * dp[j][0];
}
}
// Print the answer
cout << ans;
}
// Driver Code
int main()
{
vector > mat = { { 1, 2 }, { 3, 4 } };
int K = 4;
numberofpairs(mat, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to count ways to remove pairs
// such that the remaining elements can
// be arranged in pairs vertically or horizontally
static void numberofpairs(int[][] v, int k)
{
// Store the size of matrix
int n = v.length;
// If N is odd, then no
// such pair exists
if (n % 2 == 1) {
System.out.println(0);
return;
}
// Store the number of
// required pairs
int ans = 0;
// Initialize an auxiliary
// matrix and fill it with 0s
int dp[][] = new int[k][2];
for (int i = 0; i < k; i++) {
for (int j = 0; j < 2; j++) {
dp[i][j] = 0;
}
}
// Traverse the matrix v[][]
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Check if i+j is odd or even
if ((i + j) % 2 == 0)
// Increment the value
// dp[v[i][j]-1][0] by 1
dp[v[i][j] - 1][0]++;
else
// Increment the value
// dp[v[i][j]-1][1] by 1
dp[v[i][j] - 1][1]++;
}
}
// Iterate in range[0, k-1] using i
for (int i = 0; i < k; i++) {
// Iterate in range[i+1, k-1] using j
for (int j = i + 1; j < k; j++) {
// Update the ans
ans += dp[i][0] * dp[j][1];
ans += dp[i][1] * dp[j][0];
}
}
// Print the answer
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
int[][] mat = { { 1, 2 }, { 3, 4 } };
int K = 4;
numberofpairs(mat, K);
}
}
// This code is contributed by susmitakundogoaldanga.
Python3
# Python3 program for the above approach
# Function to count ways to remove pairs
# such that the remaining elements can
# be arranged in pairs vertically or horizontally
def numberofpairs(v, k) :
# Store the size of matrix
n = len(v)
# If N is odd, then no
# such pair exists
if (n % 2 == 1) :
print(0)
return
# Store the number of
# required pairs
ans = 0
# Initialize an auxiliary
# matrix and fill it with 0s
dp = [[0 for i in range(2)] for j in range(k)]
for i in range(k) :
for j in range(2) :
dp[i][j] = 0
# Traverse the matrix v[][]
for i in range(n) :
for j in range(n) :
# Check if i+j is odd or even
if ((i + j) % 2 == 0) :
# Increment the value
# dp[v[i][j]-1][0] by 1
dp[v[i][j] - 1][0] += 1
else :
# Increment the value
# dp[v[i][j]-1][1] by 1
dp[v[i][j] - 1][1] += 1
# Iterate in range[0, k-1] using i
for i in range(k) :
# Iterate in range[i+1, k-1] using j
for j in range(i + 1, k) :
# Update the ans
ans += dp[i][0] * dp[j][1]
ans += dp[i][1] * dp[j][0]
# Print the answer
print(ans)
# Driver code
mat = [ [ 1, 2 ], [ 3, 4 ] ]
K = 4
numberofpairs(mat, K)
# This code is contributed by divyeshrabdiya07.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to count ways to remove pairs
// such that the remaining elements can
// be arranged in pairs vertically or horizontally
static void numberofpairs(List > v,
int k)
{
// Store the size of matrix
int n = v.Count;
// If N is odd, then no
// such pair exists
if (n % 2 == 1) {
Console.Write(0);
return;
}
// Store the number of
// required pairs
int ans = 0;
// Initialize an auxiliary
// matrix and fill it with 0s
int[,] dp = new int[k, 2];
for (int i = 0; i < k; i++) {
for (int j = 0; j < 2; j++) {
dp[i, j] = 0;
}
}
// Traverse the matrix v[][]
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Check if i+j is odd or even
if ((i + j) % 2 == 0)
// Increment the value
// dp[v[i][j]-1][0] by 1
dp[v[i][j] - 1, 0]++;
else
// Increment the value
// dp[v[i][j]-1][1] by 1
dp[v[i][j] - 1, 1]++;
}
}
// Iterate in range[0, k-1] using i
for (int i = 0; i < k; i++) {
// Iterate in range[i+1, k-1] using j
for (int j = i + 1; j < k; j++) {
// Update the ans
ans += dp[i, 0] * dp[j, 1];
ans += dp[i, 1] * dp[j, 0];
}
}
// Print the answer
Console.Write(ans);
}
// Driver code
static void Main()
{
List > mat = new List>();
mat.Add(new List(new int[]{1, 2}));
mat.Add(new List(new int[]{3, 4}));
int K = 4;
numberofpairs(mat, K);
}
}
// This code is contributed by divyesh072019.
Javascript
输出:
4
时间复杂度: O(N 2 + K 2 )
辅助空间: O(K)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。