📜  在小偷被抓住之前可能发生的最大炸弹爆炸次数

📅  最后修改于: 2021-10-25 09:18:01             🧑  作者: Mango

给定的阵列,编曲[]中号整数,其中i元素表示的时间之后,其i炸弹将滴加后BLAST和三个整数N,X,Y表示相邻的连续细胞在X数量的-coordinate ,以及小偷和警察的初始牢房位置。任务是找出在小偷被抓住之前可能发生的炸弹爆炸的最大数量,如果小偷每一秒都可以投下炸弹或移动到警察没有访问过的现有牢房的左侧或右侧。

例子:

方法:根据以下观察可以解决给定的问题:

  1. 如果警察和小偷都以最佳方式移动,那么每一秒警察都会向小偷移动。因此,小偷在被抓住之前的最长时间是他们的位置之间的距离。
  2. 可以看出,最好的选择是先投下激活时间多的炸弹,而不是激活时间少的炸弹。如果先投下时间较短的炸弹,然后投下时间较长的炸弹,则可能会超过小偷在被抓住之前的时间。

请按照以下步骤解决问题:

  • 按降序对数组 arr[] 进行排序。
  • 初始化两个变量,比如counttime ,值为0以存储可能发生的炸弹爆炸的最大计数和经过的时间。
  • 找到XY之间的绝对差异并将其存储在一个变量中,比如maxSec
  • [0, M-1]范围内迭代,使用变量i ,并执行以下步骤:
    • 如果当前元素时间的总和小于或等于maxSec,则将counttime增加1
  • 完成上述步骤后,将计数更新为count = min(count, abs(XY)-1)。
  • 最后,完成以上步骤后,打印count的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum number
// of bomb that can be blasted before
// the thief gets caught
int findMaxBomb(int N, int M, int X, int Y, int arr[])
{
    // Sort the array arr[] in
    // descending order
    sort(arr, arr + M, greater());
 
    // Stores the maxtime the thief
    // has before getting caught
    int maxSec;
 
    // If Y is less than X
    if (Y < X) {
        maxSec = N - Y;
    }
    // Otherwise
    else {
        maxSec = Y - 1;
    }
 
    // Stores the current
    // second
    int time = 1;
 
    // Stores the count of
    // bomb blasts
    int count = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < M; i++) {
 
        // If arr[i]+time is less
        // than or equal to the
        // maxSec
        if (arr[i] + time <= maxSec) {
            // Increment time and
            // count by 1
            time++;
            count++;
        }
    }
 
    // Update count
    count = min(count, abs(X - Y) - 1);
 
    // Return the value of count
    return count;
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 7, X = 3, Y = 6;
    int arr[] = { 1, 4 };
    int M = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << findMaxBomb(N, M, X, Y, arr);
    return 0;
}


Java
// Java program for the above approach
import java.util.Arrays;
import java.util.Collections;
 
public class GFG {
 
    // Function to find the maximum number
    // of bomb that can be blasted before
    // the thief gets caught
    static int findMaxBomb(int N, int M, int X, int Y,
                           Integer arr[])
    {
        // Sort the array arr[] in
        // descending order
        Arrays.sort(arr, Collections.reverseOrder());
 
        // Stores the maxtime the thief
        // has before getting caught
        int maxSec;
 
        // If Y is less than X
        if (Y < X) {
            maxSec = N - Y;
        }
        // Otherwise
        else {
            maxSec = Y - 1;
        }
 
        // Stores the current
        // second
        int time = 1;
 
        // Stores the count of
        // bomb blasts
        int count = 0;
 
        // Traverse the array arr[]
        for (int i = 0; i < M; i++) {
 
            // If arr[i]+time is less
            // than or equal to the
            // maxSec
            if (arr[i] + time <= maxSec) {
                // Increment time and
                // count by 1
                time++;
                count++;
            }
        }
 
        // Update count
        count = Math.min(count, Math.abs(X - Y) - 1);
 
        // Return the value of count
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Given Input
        int N = 7, X = 3, Y = 6;
        Integer arr[] = { 1, 4 };
        int M = arr.length;
 
        // Function Call
        System.out.println(findMaxBomb(N, M, X, Y, arr));
    }
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to find the maximum number
# of bomb that can be blasted before
# the thief gets caught
def findMaxBomb(N, M, X, Y, arr):
     
    # Sort the array arr[] in
    # descending order
    arr.sort(reverse = True)
 
    # Stores the maxtime the thief
    # has before getting caught
    maxSec = 0
 
    # If Y is less than X
    if (Y < X):
        maxSec = N - Y
 
    # Otherwise
    else:
        maxSec = Y - 1
 
    # Stores the current
    # second
    time = 1
 
    # Stores the count of
    # bomb blasts
    count = 0
 
    # Traverse the array arr[]
    for i in range(M):
         
        # If arr[i]+time is less
        # than or equal to the
        # maxSec
        if (arr[i] + time <= maxSec):
             
            # Increment time and
            # count by 1
            time += 1
            count += 1
 
    # Update count
    count = min(count, abs(X - Y) - 1)
 
    # Return the value of count
    return count
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    N = 7
    X = 3
    Y = 6
    arr = [ 1, 4 ]
    M = len(arr)
 
    # Function Call
    print(findMaxBomb(N, M, X, Y, arr))
 
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the maximum number
// of bomb that can be blasted before
// the thief gets caught
static int findMaxBomb(int N, int M, int X,
                       int Y, int[] arr)
{
     
    // Sort the array arr[] in
    // descending order
    Array.Sort(arr);
 
    // Reverse array
    Array.Reverse(arr);
 
    // Stores the maxtime the thief
    // has before getting caught
    int maxSec;
 
    // If Y is less than X
    if (Y < X)
    {
        maxSec = N - Y;
    }
     
    // Otherwise
    else
    {
        maxSec = Y - 1;
    }
 
    // Stores the current
    // second
    int time = 1;
 
    // Stores the count of
    // bomb blasts
    int count = 0;
 
    // Traverse the array arr[]
    for(int i = 0; i < M; i++)
    {
         
        // If arr[i]+time is less
        // than or equal to the
        // maxSec
        if (arr[i] + time <= maxSec)
        {
             
            // Increment time and
            // count by 1
            time++;
            count++;
        }
    }
 
    // Update count
    count = Math.Min(count, Math.Abs(X - Y) - 1);
 
    // Return the value of count
    return count;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given Input
    int N = 7, X = 3, Y = 6;
    int[] arr = { 1, 4 };
    int M = arr.Length;
 
    // Function Call
    Console.WriteLine(findMaxBomb(N, M, X, Y, arr));
}
}
 
// This code is contributed by target_2


Javascript


输出
2

时间复杂度: O(M*log(M)),其中M是数组arr[]的大小
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程