给定一个二维数组Arr [] [] ,该数组由N行组成,并且两个人A和B根据以下规则玩交替回合的游戏:
- 随机选择一行,其中A只能拿走最左边的剩余硬币,而B只能拿走最右边的剩余硬币。
- 当没有硬币剩下时,游戏结束。
任务是确定A获得的最大金额。
例子:
Input: N = 2, Arr[][] = {{ 5, 2, 3, 4 }, { 1, 6 }}
Output: 8
Explanation:
Row 1: 5, 2, 3, 4
Row 2: 1, 6
Operations:
- A takes the coin with value 5
- B takes the coin with value 4
- A takes the coin with value 2
- B takes the coin with value 3
- A takes the coin with value 1
- B takes the coin with value 6
Optimally, money collected by A = 5 + 2 + 1 = 8
Money collected by B = 3 + 4 + 6 = 13
Input: N = 1, Arr[] = {{ 1, 2, 3 }}
Output : 3
方法:按照以下步骤解决问题
- 在具有最佳策略的游戏中
- 初始化变量(例如金额)以存储A获得的金额。
- 如果N是偶数,则A将收集硬币的前一半
- 否则,首先,(N / 2)的硬币将被由A收集和最后的(N / 2)将由乙收集
- 如果N为奇数,则根据移动顺序,中间的硬币可以由A或B收集。
- 将硬币存储在变量中所有奇数行的中间,例如mid_odd []。
- 以降序对数组mid_odd []进行排序。
- 最佳地, A将以min_odd []的偶数索引收集所有硬币。
- 最后,打印A的分数。
下面是上述方法的实现:
C++
// CPP Program to implement
// the above approach
#include
using namespace std;
// Function to calculate the
// maximum amount collected by A
void find(int N, vector>Arr)
{
// Stores the money
// obtained by A
int amount = 0;
// Stores mid elements
// of odd sized rows
vector mid_odd;
for(int i = 0; i < N; i++)
{
// Size of current row
int siz = Arr[i].size();
// Increase money collected by A
for (int j = 0; j < siz / 2; j++)
amount = amount + Arr[i][j];
if(siz % 2 == 1)
mid_odd.push_back(Arr[i][siz/2]);
}
// Add coins at even indices
// to the amount colected by A
sort(mid_odd.begin(),mid_odd.end());
for(int i = 0; i < mid_odd.size(); i++)
if (i % 2 == 0)
amount = amount + mid_odd[i];
// Print the amount
cout<>Arr{{5, 2, 3, 4}, {1, 6}};
// Function call to calculate the
// amount of coins collected by A
find(N, Arr);
}
// This code is contributed by ipg2016107.
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to calculate the
// maximum amount collected by A
static void find(int N, int[][] Arr)
{
// Stores the money
// obtained by A
int amount = 0;
// Stores mid elements
// of odd sized rows
ArrayList mid_odd
= new ArrayList();
for(int i = 0; i < N; i++)
{
// Size of current row
int siz = Arr[i].length;
// Increase money collected by A
for (int j = 0; j < siz / 2; j++)
amount = amount + Arr[i][j];
if(siz % 2 == 1)
mid_odd.add(Arr[i][siz/2]);
}
// Add coins at even indices
// to the amount colected by A
Collections.sort(mid_odd);
for(int i = 0; i < mid_odd.size(); i++){
if (i % 2 == 0)
amount = amount + mid_odd.get(i);
}
// Print the amount
System.out.println(amount);
}
// Driver Code
public static void main(String[] args)
{
int N = 2;
int[][] Arr = {{5, 2, 3, 4}, {1, 6}};
// Function call to calculate the
// amount of coins collected by A
find(N, Arr);
}
}
// This code is contributed by splevel62.
Python3
# Python Program to implement
# the above approach
# Function to calculate the
# maximum amount collected by A
def find(N, Arr):
# Stores the money
# obtained by A
amount = 0
# Stores mid elements
# of odd sized rows
mid_odd = []
for i in range(N):
# Size of current row
siz = len(Arr[i])
# Increase money collected by A
for j in range(0, siz // 2):
amount = amount + Arr[i][j]
if(siz % 2 == 1):
mid_odd.append(Arr[i][siz // 2])
# Add coins at even indices
# to the amount colected by A
mid_odd.sort(reverse=True)
for i in range(len(mid_odd)):
if i % 2 == 0:
amount = amount + mid_odd[i]
# Print the amount
print(amount)
# Driver Code
N = 2
Arr = [[5, 2, 3, 4], [1, 6]]
# Function call to calculate the
# amount of coins collected by A
find(N, Arr)
C#
// C# Program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to calculate the
// maximum amount collected by A
static void find(int N, List> Arr)
{
// Stores the money
// obtained by A
int amount = 0;
// Stores mid elements
// of odd sized rows
List mid_odd = new List();
for(int i = 0; i < N; i++)
{
// Size of current row
int siz = Arr[i].Count;
// Increase money collected by A
for (int j = 0; j < siz / 2; j++)
amount = amount + Arr[i][j];
if(siz % 2 == 1)
mid_odd.Add(Arr[i][siz/2]);
}
// Add coins at even indices
// to the amount colected by A
mid_odd.Sort();
for(int i = 0; i < mid_odd.Count; i++)
if (i % 2 == 0)
amount = amount + mid_odd[i];
// Print the amount
Console.WriteLine(amount);
}
// Driver code
static void Main()
{
int N = 2;
List> Arr = new List>();
Arr.Add(new List());
Arr[0].Add(5);
Arr[0].Add(2);
Arr[0].Add(3);
Arr[0].Add(4);
Arr.Add(new List());
Arr[1].Add(1);
Arr[1].Add(6);
// Function call to calculate the
// amount of coins collected by A
find(N, Arr);
}
}
// This code is contributed by divyesh072019.
输出:
8
时间复杂度: O(N)
辅助空间: O(1)