📌  相关文章
📜  给定数组可能的最大分区,成本最多为 K,奇偶元素的数量相等

📅  最后修改于: 2022-05-13 01:57:50.243000             🧑  作者: Mango

给定数组可能的最大分区,成本最多为 K,奇偶元素的数量相等

给定两个整数N, K和一个大小为N的数组arr[] ,包含相等数量的偶数和奇数元素,并且还假设通过在索引ii+1之间进行切割来分割数组的成本为等于abs(arr[i]-arr[i+1]) ,任务是找到数组的最大分区,使得每个分区的奇偶元素数量相等,总成本小于或等于K

例子:

方法:如果i的前缀中偶数和奇数元素的计数相等,则可以根据观察来解决给定的问题,即通过在索引ii+i之间进行切割总是可以进行有效分区。按照步骤解决问题。

  • 初始化一个向量V以存储数组中所有可能切割的成本。
  • 此外,初始化变量,将奇数设为0 ,将偶数设为0 ,它们存储偶数和奇数元素的计数。
  • 使用变量i遍历数组arr[]并执行以下步骤:
    • 如果当前元素是奇数,则奇数1。否则,偶数1。
    • 如果奇数的值等于偶数的值,则附加 | 的值arr[i]arr[i+1] |到V。
  • 按升序对向量V进行排序。
  • 初始化一个整数变量ans1,用于存储数组的分区数。
  • 使用变量i遍历向量V并执行以下步骤:
    • 如果V[i]的值小于或等于K ,则将值K更新为KV[i]并将ans1。
    • 否则,跳出循环。
  • 最后,完成以上步骤后,打印ans的值作为得到的答案。

下面是上述方法的实现:

C++
// C++ code for the above approach
#include 
using namespace std;
 
// Function to find the maximum
// partitions of the array with
// equal even and odd elements
// with cost less than k
int maximumcut(int arr[], int N, int K)
{
    // Stores count of odd elements
    int odd = 0;
    // Stores count of even elements
    int even = 0;
 
    // Stores the cost of partitions
    vector V;
 
    for (int i = 0; i < N - 1; i++) {
        // Odd element
        if (arr[i] % 2 == 1) {
            odd++;
        }
        // Even element
        else {
            even++;
        }
 
        // Partition is possible
        if (odd == even) {
            int cost = abs(arr[i] - arr[i + 1]);
 
            // Append the cost of partition
            V.push_back(cost);
        }
    }
    // Stores the maximum number of
    // partitions
    int ans = 0;
 
    // Sort the costs in ascending order
    sort(V.begin(), V.end());
 
    // Traverse the vector V
    for (int i = 0; i < V.size(); i++) {
        // Check if cost is less than K
        if (V[i] <= K) {
            // Update the value of K
            K = K - V[i];
 
            // Update the value of ans
            ans++;
        }
        else {
            break;
        }
    }
    // Return ans
    return ans;
}
 
// Driver code
int main()
{
    // Given Input
    int arr[] = {1, 2, 5, 10, 15, 20};
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 4;
    // Function call
    cout << maximumcut(arr, N, K);
    return 0;
}


Java
// Java code for the above approach
import java.util.ArrayList;
import java.util.Arrays;
 
class GFG
{
   
// Function to find the maximum
// partitions of the array with
// equal even and odd elements
// with cost less than k
public static int maximumcut(int arr[], int N, int K)
{
   
    // Stores count of odd elements
    int odd = 0;
   
    // Stores count of even elements
    int even = 0;
 
    // Stores the cost of partitions
    ArrayList V = new ArrayList();
 
    for (int i = 0; i < N - 1; i++) {
        // Odd element
        if (arr[i] % 2 == 1) {
            odd++;
        }
        // Even element
        else {
            even++;
        }
 
        // Partition is possible
        if (odd == even) {
            int cost = Math.abs(arr[i] - arr[i + 1]);
 
            // Append the cost of partition
            V.add(cost);
        }
    }
    // Stores the maximum number of
    // partitions
    int ans = 0;
 
    // Sort the costs in ascending order
    V.sort(null);
 
    // Traverse the vector V
    for (int i = 0; i < V.size(); i++)
    {
       
        // Check if cost is less than K
        if (V.get(i) <= K)
        {
           
            // Update the value of K
            K = K - V.get(i);
 
            // Update the value of ans
            ans++;
        }
        else {
            break;
        }
    }
    // Return ans
    return ans;
}
 
// Driver code
public static void  main(String args[])
{
    // Given Input
    int arr[] = {1, 2, 5, 10, 15, 20};
    int N = arr.length;
    int K = 4;
    // Function call
    System.out.println(maximumcut(arr, N, K));
}
 
}
 
// This code is contributed by gfgking.


Python3
# Python3 code for the above approach
 
# Function to find the maximum
# partitions of the array with
# equal even and odd elements
# with cost less than k
def maximumcut(arr, N, K):
     
    # Stores count of odd elements
    odd = 0
     
    # Stores count of even elements
    even = 0
 
    # Stores the cost of partitions
    V = []
 
    for i in range(0, N - 1, 1):
         
        # Odd element
        if (arr[i] % 2 == 1):
            odd += 1
 
        # Even element
        else:
            even += 1
 
        # Partition is possible
        if (odd == even):
            cost = abs(arr[i] - arr[i + 1])
 
            # Append the cost of partition
            V.append(cost)
 
    # Stores the maximum number of
    # partitions
    ans = 0
 
    # Sort the costs in ascending order
    V.sort()
 
    # Traverse the vector V
    for i in range(len(V)):
         
        # Check if cost is less than K
        if (V[i] <= K):
             
            # Update the value of K
            K = K - V[i]
 
            # Update the value of ans
            ans += 1
             
        else:
            break
 
    # Return ans
    return ans
 
# Driver code
if __name__ == '__main__':
     
    # Given Input
    arr = [ 1, 2, 5, 10, 15, 20 ]
    N = len(arr)
    K = 4
     
    # Function call
    print(maximumcut(arr, N, K))
     
# This code is contributed by SURENDRA_GANGWAR


C#
// C# code for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to find the maximum
// partitions of the array with
// equal even and odd elements
// with cost less than k
static int maximumcut(int []arr, int N, int K)
{
     
    // Stores count of odd elements
    int odd = 0;
     
    // Stores count of even elements
    int even = 0;
 
    // Stores the cost of partitions
    List V = new List();
 
    for(int i = 0; i < N - 1; i++)
    {
         
        // Odd element
        if (arr[i] % 2 == 1)
        {
            odd++;
        }
         
        // Even element
        else
        {
            even++;
        }
 
        // Partition is possible
        if (odd == even)
        {
            int cost = Math.Abs(arr[i] - arr[i + 1]);
 
            // Append the cost of partition
            V.Add(cost);
        }
    }
     
    // Stores the maximum number of
    // partitions
    int ans = 0;
 
    // Sort the costs in ascending order
    V.Sort();
 
    // Traverse the vector V
    for(int i = 0; i < V.Count; i++)
    {
         
        // Check if cost is less than K
        if (V[i] <= K)
        {
             
            // Update the value of K
            K = K - V[i];
 
            // Update the value of ans
            ans++;
        }
        else
        {
            break;
        }
    }
     
    // Return ans
    return ans;
}
 
// Driver code
public static void Main()
{
     
    // Given Input
    int []arr = { 1, 2, 5, 10, 15, 20 };
    int N = arr.Length;
    int K = 4;
     
    // Function call
    Console.Write(maximumcut(arr, N, K));
}
}
 
// This code is contributed by ipg2016107


Javascript


输出:
1

时间复杂度: O(N*log(N))
辅助空间: O(N)