
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to find minimum number of locations to place bombs to kill all monsters
Suppose we have two arrays X and H. Both are with N elements, also have another two numbers D and A. Consider in a story, a silver fox is fighting with N monsters. The monsters are standing in a row, coordinate of ith monster is X[i], and its health is H[i]. Silver fox can use bombs to attack monsters. Dropping bomb at location x will damage all monsters within the range of x - D to x + D. Their health will be reduced by A. When all monsters have 0 health left, then the fox wins. We have to find the minimum possible are needed to win.
So, if the input is like D = 3; A = 2; X = [1, 5, 9]; H = [2, 4, 2], then the output will be 2, because first bomb at coordinate 4, new health values are [0, 2, 2], then at position 6 to make all health values [0, 0, 0].
Steps
To solve this, we will follow these steps −
Define a large array q Define an array of x and h pairs n := size of X d := D a := A for initialize i := 1, when i <= n, update (increase i by 1), do: num[i].x := X[i - 1] num[i].h := H[i - 1] sort the array num sum := 0 for initialize i := 1, when i <= n, update (increase i by 1), do: q[i] := q[i] + q[i - 1] num[i].h := num[i].h - q[i] * a if num[i].h <= 0, then: Ignore following part, skip to the next iteration p := (if num[i].h mod a is same as 0, then num[i].h / a, otherwise num[i].h / a + 1) tmp := num[i].x + 2 * d sum := sum + p q[i] := q[i] + p l := i, r = n while l < r, do: mid := (l + r + 1) / 2 if num[mid].x <= tmp, then: l := mid Otherwise r := mid - 1 q[l + 1] - = p return sum
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 20; int n; int d, a, q[maxn]; struct node{ int x, h; bool operator<(const node& a) const{ return x < a.x; } } num[maxn]; int solve(int D, int A, vector<int> X, vector<int> H){ n = X.size(); d = D; a = A; for (int i = 1; i <= n; i++){ num[i].x = X[i - 1]; num[i].h = H[i - 1]; } sort(num + 1, num + n + 1); int sum = 0; for (int i = 1; i <= n; i++){ q[i] += q[i - 1]; num[i].h -= q[i] * a; if (num[i].h <= 0) continue; int p = (num[i].h % a == 0 ? num[i].h / a : num[i].h / a + 1); int tmp = num[i].x + 2 * d; sum += p; q[i] += p; int l = i, r = n; while (l < r){ int mid = (l + r + 1) >> 1; if (num[mid].x <= tmp) l = mid; else r = mid - 1; } q[l + 1] -= p; } return sum; } int main(){ int D = 3; int A = 2; vector<int> X = { 1, 5, 9 }; vector<int> H = { 2, 4, 2 }; cout << solve(D, A, X, H) << endl; }
Input
3, 2, { 1, 5, 9 }, { 2, 4, 2 }
Output
2
- Related Articles
- Minimum number of bombs in Java
- Program to find minimum number of vertices to reach all nodes using Python
- Program to find minimum number of pins required to hang all banners in C++
- Program to find minimum cost to pick up gold in given two locations in Python
- Program to find minimum number of operations to move all balls to each box in Python
- Program to find minimum number of movie theatres required to show all movies in python
- C++ code to find minimum moves with weapons to kill enemy
- Program to find minimum number of busses are required to pass through all stops in Python
- Program to find minimum number of swaps needed to arrange all pair of socks together in C++
- Program to find number of minimum steps required to meet all person at any cell in Python
- Program to find minimum number of people to teach in Python
- Program to find minimum time to complete all tasks in python
- Program to find minimum cost to connect all points in Python
- Program to find minimum time to finish all jobs in Python
- C++ Program to find minimum moves to get all books contiguous
