📌  相关文章
📜  矩阵不成对重叠所需的最大操作数

📅  最后修改于: 2021-04-27 23:32:52             🧑  作者: Mango

给定一个整数N,接着在X的升序一个矩阵V [] [],包括对{X,Y},任务是提供一种用于在X的升序给出每一对中,可以执行以下操作:

  • 将对{X,Y}转换为{X – Y,X}
  • 将对{X,Y}转换为{X,X + Y}
  • 将该对更改为{X,X}

任务是找到所需的加法和减法运算的计数,以确保没有两对重叠。

例子:

方法:
主要思想是观察到,无论如何,答案都不会超过N ,因为这三个操作中的任何一个都不能在一个对上进行两次。请按照以下步骤解决问题:

  • 第一对始终选择“操作1 ”,因为第一对X最小。
  • 始终选择最后一个对的操作2 ,因为X是最后一个对的最大值。
  • 对于其余的对,检查应用操作1是否违反规则。如果不违反规则,那么它将始终使结果最大化。否则,请检查操作2 。如果两个操作中的任何一个适用,则增加计数
  • 如果这两个规则都不适用,请执行操作3。
  • 最后,打印计数

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
  
// Function to find maximum count of operations
int find_max(vector > v, int n)
{
    // Initialize count by 0
    int count = 0;
  
    if (n >= 2)
        count = 2;
  
    else
        count = 1;
  
    // Iterate over remaining pairs
    for (int i = 1; i < n - 1; i++) {
  
        // Check if first operation
        // is applicable
        if (v[i - 1].first
            < (v[i].first - v[i].second))
            count++;
  
        // Check if 2nd operation is applicable
        else if (v[i + 1].first
                 > (v[i].first + v[i].second)) {
            count++;
            v[i].first = v[i].first + v[i].second;
        }
  
        // Otherwise
        else
            continue;
    }
  
    // Return  the count of operations
    return count;
}
  
// Driver Code
int main()
{
    int n = 3;
    vector > v;
  
    v.push_back({ 10, 20 });
    v.push_back({ 15, 10 });
    v.push_back({ 20, 16 });
  
    cout << find_max(v, n);
  
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
  
class GFG{
      
static class pair
{ 
    int first, second; 
    public pair(int first, int second) 
    { 
        this.first = first; 
        this.second = second; 
    } 
} 
  
// Function to find maximum count of operations
static int find_max(Vector v, int n)
{
    // Initialize count by 0
    int count = 0;
  
    if (n >= 2)
        count = 2;
    else
        count = 1;
  
    // Iterate over remaining pairs
    for(int i = 1; i < n - 1; i++)
    {
          
        // Check if first operation
        // is applicable
        if (v.get(i - 1).first < 
           (v.get(i).first - v.get(i).second))
            count++;
  
        // Check if 2nd operation is applicable
        else if (v.get(i + 1).first > 
                (v.get(i).first + v.get(i).second)) 
        {
            count++;
            v.get(i).first = v.get(i).first + 
                             v.get(i).second;
        }
  
        // Otherwise
        else
            continue;
    }
  
    // Return the count of operations
    return count;
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 3;
    Vector v = new Vector<>();
  
    v.add(new pair(10, 20));
    v.add(new pair(15, 10));
    v.add(new pair(20, 16));
  
    System.out.print(find_max(v, n));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement
# the above approach
  
# Function to find maximum count of
# operations
def find_max(v, n):
  
    # Initialize count by 0
    count = 0
  
    if(n >= 2):
        count = 2
    else:
        count = 1
  
    # Iterate over remaining pairs
    for i in range(1, n - 1):
  
        # Check if first operation
        # is applicable
        if(v[i - 1][0] > (v[i][0] +
                          v[i][1])):
            count += 1
  
        # Check if 2nd operation is applicable
        elif(v[i + 1][0] > (v[i][0] +
                            v[i][1])):
            count += 1
            v[i][0] = v[i][0] + v[i][1]
  
        # Otherwise
        else:
            continue
  
    # Return the count of operations
    return count
  
# Driver Code
n = 3
v = []
  
v.append([ 10, 20 ])
v.append([ 15, 10 ])
v.append([ 20, 16 ])
  
print(find_max(v, n))
  
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
  
class GFG{
      
class pair
{ 
    public int first, second; 
    public pair(int first, int second) 
    { 
        this.first = first; 
        this.second = second; 
    } 
} 
  
// Function to find maximum count of operations
static int find_max(List v, int n)
{
      
    // Initialize count by 0
    int count = 0;
  
    if (n >= 2)
        count = 2;
    else
        count = 1;
  
    // Iterate over remaining pairs
    for(int i = 1; i < n - 1; i++)
    {
          
        // Check if first operation
        // is applicable
        if (v[i - 1].first < 
           (v[i].first - v[i].second))
            count++;
  
        // Check if 2nd operation is applicable
        else if (v[i + 1].first > 
                (v[i].first + v[i].second)) 
        {
            count++;
            v[i].first = v[i].first + 
                         v[i].second;
        }
  
        // Otherwise
        else
            continue;
    }
  
    // Return the count of operations
    return count;
}
  
// Driver Code
public static void Main(String[] args)
{
    int n = 3;
    List v = new List();
  
    v.Add(new pair(10, 20));
    v.Add(new pair(15, 10));
    v.Add(new pair(20, 16));
  
    Console.Write(find_max(v, n));
}
}
  
// This code is contributed by 29AjayKumar


输出:
2

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