📜  所需的最大操作数,以便矩阵中的任何对都不重叠

📅  最后修改于: 2021-10-26 02:36:24             🧑  作者: Mango

给定一个整数N后跟一个矩阵V[][]由按X升序排列的对{X, Y}组成,任务是对于按 X 升序给出的每一对,可以执行以下操作:

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

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

例子:

方法:
主要思想是观察答案在任何情况下都不会超过 N ,因为三个操作中的任何一个都不能对一对应用两次。请按照以下步骤解决问题:

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

下面是上述方法的实现:

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


Javascript


输出:
2

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