在n座建筑物中有外星人(每座建筑物中最少有1个),您必须杀死所有炸弹,使其数量最少。建筑物编号为1 – n。一架被炸毁的建筑物中的外星人在第一次爆炸中受伤,在第二次爆炸中死亡。首次轰炸一幢建筑物时,该建筑物中的外星人会试图逃到最近的建筑物(对于第一座建筑物,最近的建筑物是第二座建筑物,对于第n座建筑物,它是n-1个)。计算杀死所有外星人所需的最小炸弹数量和爆炸顺序。
例子:
Input: 3
Output: 4
2 1 3 2
Explanation: Minimum number of bombs required are 4.
First bomb the 2nd building, aliens
will move to 1st or 3rd to save
themselves. Then bomb at 1st building,
if some aliens have moved from 2nd
building to 1st they will be killed and
the 1st building aliens will be injured,
and they will move to the 2nd building
as it is nearest to them. Now, bomb at
the 3rd building to kill aliens who
moved from the 2nd building to 3rd and
injure 3rd building aliens so they move
to 2nd building as it is nearest to them.
Now, bomb at the 2nd building again and
all aliens who moved from 1st or 3rd
building will be killed.
Input: 2
Output: 3
2 1 2
我们可以采取建设性的方式杀死所有外星人。由于每个人都向左或向右移动,因此我们必须确保所有偶数位置都受到攻击,一次是在开始伤害外星人时,另一次是在结束时。当我们第一次在偶数位置攻击外星人时,它们会移至奇数位置的建筑物,因此要在奇数攻击它们时杀死所有先前的偶数位置并伤害奇数位置的外星人。奇数位置的外星人会受伤,并且将移动到偶数位置,因此请在偶数结束时攻击他们以杀死他们。
路数将为n / 2 + n / 2 + n / 2 ,即n + n / 2。
下面是上述方法的实现:
C++
// CPP program to find number of bombings required
// to kill all aliens.
#include
using namespace std;
// function to print where to shoot
void print(int n)
{
// no. of bombs required
cout << n + n / 2 << endl;
// bomb all the even positions
for (int i = 2; i <= n; i += 2)
cout << i << " ";
// bomb all the odd positions
for (int i = 1; i <= n; i += 2)
cout << i << " ";
// bomb all the even positions again
for (int i = 2; i <= n; i += 2)
cout << i << " ";
}
// driver program
int main()
{
int n = 3;
print(n);
return 0;
}
Java
// Java program to find number of bombings
// required to kill all aliens.
class GFG {
// function to print where to shoot
static void print(int n)
{
// no. of bombs required
System.out.println(n + n / 2);
// bomb all the even positions
for (int i = 2; i <= n; i += 2)
System.out.print( i + " ");
// bomb all the odd positions
for (int i = 1; i <= n; i += 2)
System.out.print(i + " ");
// bomb all the even positions again
for (int i = 2; i <= n; i += 2)
System.out.print( i + " ");
}
// Driver code
public static void main (String[] args)
{
int n = 3;
print(n);
}
}
// This code is contributed by Anant Agarwal.
Python3
"""Python program to find number of
bombings required to kill all aliens"""
# function to print where to shoot
def bomb_required(n):
# no. of bombs required
print(n+n // 2)
# bomb all the even positions
for i in range(2, n + 1, 2):
print(i, end = " ")
# bomb all the odd positions
for i in range(1, n + 1, 2):
print(i, end = " ")
# bomb all the even positions again
for i in range(2, n, 2):
print(i, end = " ")
# Driver Code
bomb_required(3)
# This code is contributed by Abhishek Agrawal.
C#
// C# program to find number of bombings
// required to kill all aliens.
using System;
class GFG {
// function to print where to shoot
static void print(int n)
{
// no. of bombs required
Console.WriteLine(n + n / 2);
// bomb all the even positions
for (int i = 2; i <= n; i += 2)
Console.Write( i + " ");
// bomb all the odd positions
for (int i = 1; i <= n; i += 2)
Console.Write(i + " ");
// bomb all the even positions
// again
for (int i = 2; i <= n; i += 2)
Console.Write( i + " ");
}
// Driver code
public static void Main ()
{
int n = 3;
print(n);
}
}
// This code is contributed by vt_m.
PHP
输出:
4
2 1 3 2
时间复杂度: O(n)