I have to do this program and I have a time limit for it to run. I am trying various solutions, but each time I exceed the maximum execution time.
Could anyone help me optimize my program?
The assignment:
I have a series of houses (house heights) that must be bombed by military aircraft. I have a random number of planes at my disposal. When a plane passes over the houses it manages to destroy all the houses that are higher than the two adjacent ones. The destroyed houses become with height 0. Subsequent planes will do the same thing with the houses not yet destroyed. I have to print how many houses were destroyed by the planes.
Link to more details: https://training.olinfo.it/#/task/ois_raid/statement
My current solution:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int aircrafts, number_of_houses, burned_houses=0, x;
cin >> number_of_houses >> aircrafts;
// int houses[number_of_houses];
vector <int> houses;
for(int i=0; i<number_of_houses; i++)
{
cin >> x;
houses.push_back(x);
}
for(int i=0; i<aircrafts; i++)
{
for(int j=0; j<houses.size(); j++)
{
if(houses[j]>0)
{
if(houses[j]>houses[j+1] && houses[j]>houses[j-1])
{
houses[j] = 0;
j++;
burned_houses++;
}
}
}
}
cout << burned_houses;
}
Aucun commentaire:
Enregistrer un commentaire