根据给定条件最大化矩阵
给定一个 N*N 矩阵,其中矩阵的每个元素都在 0 到 M 的范围内。您可以对矩阵应用以下操作任意次数:
- 选择任意两个连续元素
- 其中一个加 1,另一个减 1
注意:应用上述操作后,元素应保持在 0 到 M 的范围内。
任务是如果需要,在对矩阵执行上述操作后,找到如下所示表达式的最大值:
res += (i+j)*A[i][j]
for 0 <= i, j <= N
例子:
Input : A[][] = {1, 2,
5, 1}
M = 5
Output : RESULT = 27
Matrix : 0 0
4 5
Input : A[][] = {3, 4,
5, 4}
M = 6
Output : RESULT = 43
Matrix : 0 4
6 6
算法 :
以下是执行此操作的分步算法:
- 首先,将给定矩阵的所有元素的总和计算为 SUM。
- 从最后一个元素即 A(n, n) 开始,逆对角地向后移动到 A(0,0) 为 A(n, n), A(n, n-1), A(n-1, n) , A(n, n-2), A(n-1, n-1), A(n-2, n)…..
- 用 M 填充矩阵的每个单元格并更新每个元素的 SUM = SUM-M 直到 SUM < M。现在,如果 SUM 值大于零并且所有其他剩余位置为零,则按顺序填充下一个位置的 SUM 值。
- 最后,您可以按照上述公式计算 RESULT。
例子 :
输入矩阵:
应用上述算法后的解矩阵:
以下是上述想法的实现:
C++
// CPP to maximize matrix result
#include
using namespace std;
#define n 4
// utility function for maximize matrix result
int maxMatrix(int A[][n], int M)
{
int sum = 0, res = 0;
for ( int i=0; i0; j--)
{
for (int i=0; i M)
{
A[n-1-i][j+i] = M;
sum -= M;
}
else
{
A[n-1-i][j+i] = sum;
sum -= sum;
}
}
}
// diagonals above longest diagonal
for (int i=n-1; i>=0; i--)
{
for (int j=0; j<=i; j++)
{
if (sum > M)
{
A[i-j][j] = M;
sum -= M;
}
else
{
A[i-j][j] = sum;
sum -= sum;
}
}
}
// calculating result
for (int i=0; i
Java
// Java to maximize matrix result
class GFG {
static final int n = 4;
// utility function for maximize matrix result
static int maxMatrix(int A[][], int M) {
int sum = 0, res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sum += A[i][j];
}
}
// diagonals below longest diagonal
// starting from last element of matrix
for (int j = n - 1; j > 0; j--) {
for (int i = 0; i < n - j; i++) {
if (sum > M) {
A[n - 1 - i][j + i] = M;
sum -= M;
} else {
A[n - 1 - i][j + i] = sum;
sum -= sum;
}
}
}
// diagonals above longest diagonal
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j <= i; j++) {
if (sum > M) {
A[i - j][j] = M;
sum -= M;
} else {
A[i - j][j] = sum;
sum -= sum;
}
}
}
// calculating result
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
res += (i + j + 2) * A[i][j];
}
}
return res;
}
// driver program
static public void main(String[] args) {
int A[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 1, 1, 2},
{3, 4, 5, 6}};
int m = 9;
System.out.println(maxMatrix(A, m));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python to maximize matrix result
n = 4
# utility function for maximize
# matrix result
def maxMatrix(A, M):
sum, res = 0, 0
for i in range(n):
for j in range(n):
sum += A[i][j]
# diagonals below longest diagonal
# starting from last element of matrix
for j in range(n - 1, 0, -1):
for i in range(n - j):
if (sum > M):
A[n - 1 - i][j + i] = M
sum -= M
else:
A[n - 1 - i][j + i] = sum
sum -= sum
# diagonals above longest diagonal
for i in range(n - 1, -1, -1):
for j in range(i + 1):
if (sum > M):
A[i - j][j] = M
sum -= M
else:
A[i - j][j] = sum
sum -= sum
# calculating result
for i in range(n):
for j in range(n):
res += (i + j + 2) * A[i][j]
return res
# Driver Code
if __name__ == '__main__':
A = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 1, 1, 2],
[3, 4, 5, 6]]
m = 9
print(maxMatrix(A, m))
# This code is contributed by 29AjayKumar
C#
// C# to maximize matrix result
using System;
public class GFG {
static readonly int n = 4;
// utility function for maximize matrix result
static int maxMatrix(int [,]A, int M) {
int sum = 0, res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sum += A[i,j];
}
}
// diagonals below longest diagonal
// starting from last element of matrix
for (int j = n - 1; j > 0; j--) {
for (int i = 0; i < n - j; i++) {
if (sum > M) {
A[n - 1 - i,j + i] = M;
sum -= M;
} else {
A[n - 1 - i,j + i] = sum;
sum -= sum;
}
}
}
// diagonals above longest diagonal
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j <= i; j++) {
if (sum > M) {
A[i - j,j] = M;
sum -= M;
} else {
A[i - j,j] = sum;
sum -= sum;
}
}
}
// calculating result
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
res += (i + j + 2) * A[i,j];
}
}
return res;
}
// driver program
static public void Main() {
int [,]A= {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 1, 1, 2},
{3, 4, 5, 6}};
int m = 9;
Console.Write(maxMatrix(A, m));
}
}
// This code is contributed by Rajput-Ji
PHP
0; $j--)
{
for ($i = 0; $i < $n - $j; $i++)
{
if ($sum > $M)
{
$A[$n - 1 - $i][$j + $i] = $M;
$sum -= $M;
}
else
{
$A[$n - 1 - $i][$j + i] = $sum;
$sum -= $sum;
}
}
}
// diagonals above longest diagonal
for ($i = $n - 1; $i >= 0; $i--)
{
for ($j = 0; $j <= $i; $j++)
{
if ($sum > $M)
{
$A[$i - $j][$j] = $M;
$sum -= $M;
}
else
{
$A[$i - $j][$j] = $sum;
$sum -= $sum;
}
}
}
// calculating result
for ($i = 0; $i < $n; $i++)
{
for ($j = 0; $j < $n; $j++)
$res += ($i + $j + 2) *
$A[$i][$j];
}
return $res;
}
// Driver Code
$A = array(array(1, 2, 3, 4),
array(5, 6, 7, 8),
array(9, 1, 1, 2),
array(3, 4, 5, 6));
$m = 9;
echo maxMatrix($A, $m);
// This code is contributed by anuj_67.
?>
Javascript
输出:
425