给定一个由N行组成的二维数组Arr[][]和两个人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
处理方法:按照以下步骤解决问题
- 在以最优策略玩的游戏中
- 初始化一个变量,比如amount ,以存储A获得的钱。
- 如果N是偶数, A将收集前一半的硬币
- 否则,首先, A将收集(N / 2) 个硬币, B将收集最后一个(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 collected 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 collected 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 collected 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 collected 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.
Javascript
输出:
8
时间复杂度: O(N)
辅助空间: O(1)