📌  相关文章
📜  顺序执行给定操作后值为 1 的索引计数

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

顺序执行给定操作后值为 1 的索引计数

给定一个大小为N二进制数组a[]和一个整数X 。除了索引Xa[X] = 1之外,数组的所有值都是0 。给定另一个大小为M的数组对v[][] ,表示在数组中执行的M个顺序操作。在第 i 个操作中,如果该范围内至少有一个 1 ,则位于索引[ v[i][0], v[i][1] ]中的所有值都将更改为1 。依次执行这些操作后,求1的总数。

例子:

方法:考虑当前范围为[L, R]。最初,范围[L, R]等于[X, X],因为最初,只有第 X位置是1。现在,遍历每个给定范围,可能有 2 种可能性。

最终答案将是R-L+1。请按照以下步骤解决问题:

  • 将变量LR初始化为X。
  • 使用变量i迭代范围[0, M)并执行以下步骤:
    • 如果l小于等于v[i].second并且R大于等于v[i][0],则将L的值设置为Lv[i][0]R的最小值Rv[i][1] 的最大值。
  • 执行上述步骤后,打印(R – L + 1)的值作为答案。

以下是上述方法的实现:

C++
// C++ code for the above approach
#include 
using namespace std;
 
// Function to find Maximum number
// of indices with value equal to 1
void MaxCount(int N, int X, int M,
              vector >& v)
{
    int L, R;
 
    // Initially current range [L, R] = [X, X]
    L = X, R = X;
 
    for (int i = 0; i < M; i++)
    {
        // If current range [L, R] and
        // ith range are intersecting then
        // update the current range
        // [L, R] = [min(L, b[i][0]),
        // max(b[i][1]))]
        if (L <= v[i].second
            && v[i].first <= R) {
            L = min(L, v[i].first);
            R = max(R, v[i].second);
        }
    }
 
    // Calculating the answer
    int ans = R - L + 1;
 
    // Printing the maximum number of
    // indices which can be made 1
    // by some flip operations
    cout << ans;
}
 
// Driver Code
int main()
{
    int N, X, M;
    N = 6, X = 4, M = 3;
    vector > v
        = { { 1, 6 }, { 2, 3 }, { 5, 5 } };
    MaxCount(N, X, M, v);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG {
 
  // Function to find Maximum number
  // of indices with value equal to 1
  public static void MaxCount(int N, int X, int M,
                              int[][] v)
  {
    int L, R;
 
    // Initially current range [L, R] = [X, X]
    L = X;
    R = X;
 
    for (int i = 0; i < M; i++) {
      // If current range [L, R] and
      // ith range are intersecting then
      // update the current range
      // [L, R] = [min(L, b[i][0]),
      // max(b[i][1]))]
      if (L <= v[i][1] && v[i][0] <= R) {
        L = Math.min(L, v[i][0]);
        R = Math.max(R, v[i][1]);
      }
    }
 
    // Calculating the answer
    int ans = R - L + 1;
 
    // Printing the maximum number of
    // indices which can be made 1
    // by some flip operations
    System.out.println(ans);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    int N = 6, X = 4, M = 3;
 
    int[][] v
      = new int[][] { { 1, 6 }, { 2, 3 }, { 5, 5 } };
    MaxCount(N, X, M, v);
  }
}
 
// This code is contributed by rakeshsahni


Python3
# Python code for the above approach
 
# Function to find Maximum number
# of indices with value equal to 1
def MaxCount(N, X, M, v):
 
    # Initially current range [L, R] = [X, X]
    L = X
    R = X
 
    for i in range(M):
 
        # If current range [L, R] and
        # ith range are intersecting then
        # update the current range
        # [L, R] = [min(L, b[i][0]),
        # max(b[i][1]))]
        if (L <= v[i]["second"] and v[i]["first"] <= R):
            L = min(L, v[i]["first"])
            R = max(R, v[i]["second"])
 
    # Calculating the answer
    ans = R - L + 1
 
    # Printing the maximum number of
    # indices which can be made 1
    # by some flip operations
    print(ans)
 
# Driver Code
N = 6
X = 4
M = 3
v = [{"first": 1, "second": 6}, {"first": 2,
                                 "second": 3},  {"first": 5, "second": 5}]
MaxCount(N, X, M, v)
 
# This code is contributed by Saurabh Jaiswal


C#
// C# code for the above approach
using System;
class GFG {
 
  // Function to find Maximum number
  // of indices with value equal to 1
  static void MaxCount(int N, int X, int M, int[, ] v)
  {
 
    // Initially current range [L, R] = [X, X]
    int L = X, R = X;
 
    for (int i = 0; i < M; i++)
    {
 
      // If current range [L, R] and
      // ith range are intersecting then
      // update the current range
      // [L, R] = [min(L, b[i][0]),
      // max(b[i][1]))]
      if (L <= v[i, 1] && v[i, 0] <= R) {
        L = Math.Min(L, v[i, 0]);
        R = Math.Max(R, v[i, 1]);
      }
    }
 
    // Calculating the answer
    int ans = R - L + 1;
 
    // Printing the maximum number of
    // indices which can be made 1
    // by some flip operations
    Console.Write(ans);
  }
 
  // Driver Code
  public static int Main()
  {
    int N = 6, X = 4, M = 3;
 
    int[, ] v
      = new int[, ] { { 1, 6 }, { 2, 3 }, { 5, 5 } };
    MaxCount(N, X, M, v);
    return 0;
  }
}
 
// This code is contributed by Taranpreet


Javascript


输出
6

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