顺序执行给定操作后值为 1 的索引计数
给定一个大小为N的二进制数组a[]和一个整数X 。除了索引X即a[X] = 1之外,数组的所有值都是0 。给定另一个大小为M的数组对v[][] ,表示在数组中执行的M个顺序操作。在第 i 个操作中,如果该范围内至少有一个 1 ,则位于索引[ v[i][0], v[i][1] ]中的所有值都将更改为1 。依次执行这些操作后,求1的总数。
例子:
Input: v[][] = {{1, 6}, {2, 3}, {5, 5}}, N = 6, X = 4
Output: 6
Explanation: Initially the range of elements that can be 1 is [3, 3]. The array is {0, 0, 1, 0, 0, 0}
After the first operation – there is 1 in the given range [1, 6]. So array becomes {1, 1, 1, 1, 1, 1}
After the second operation and third operation the array will be same.
So total number of 1s are 6.
Input: v[][] = {{2, 4}, {1, 2}}, N = 4, X = 1
Output: 2
Explanation: Initially the range of the elements that can be 1 is [1, 1]. The array is {1, 0, 0, 0}.
After the first operation – there is no 1 in [2, 4]. So, the array will be {1, 0, 0, 0}.
After the second operation – there is one 1 in [1, 2]. So, the array will be {1, 1, 0, 0}
So, total number of indices that can have 1 is 2.
方法:考虑当前范围为[L, R]。最初,范围[L, R]等于[X, X],因为最初,只有第 X个位置是1。现在,遍历每个给定范围,可能有 2 种可能性。
- The current range [L, R] is intersecting with the ith range, Update the current range [L, R] equals [min(L, b[i][0]), max(b[i][1])).
- The current range [L, R] is not intersecting with the ith range, no operation will be performed in this case.
最终答案将是R-L+1。请按照以下步骤解决问题:
- 将变量L和R初始化为X。
- 使用变量i迭代范围[0, M)并执行以下步骤:
- 如果l小于等于v[i].second并且R大于等于v[i][0],则将L的值设置为L或v[i][0]和R的最小值R或v[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)