给定一个整数K和一个大小为N * N且具有范围[1,K]的元素的方阵mat [] [] ,任务是计算删除一对不同矩阵元素的方式,以便可以将其余元素排列在垂直或水平配对。
例子:
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是偶数,并且B的是奇数,或反之亦然。
请按照以下步骤解决问题:
- 如果矩阵的大小为奇数,则打印0 ,因为可能没有布置。
- 否则,请检查以下内容:
- 将变量ans初始化为0,以存储所需对的数量。
- 初始化辅助矩阵dp [] [] ,大小为K×2 , 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 。
- 使用变量i在[0,K – 1]范围内迭代,并使用变量j在[i + 1,K – 1]范围内嵌套:
- 添加DP的值[I] [0] * DP [j]的[1]〜ANS既是块的值不同,并且第(i + j)的和一个的值是奇数和其他第(i + j)的值甚至。
- 将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.
输出:
4
时间复杂度: O(N 2 + K 2 )
辅助空间: O(K)