I made this code for a past test, I only got 30/100 points obtainable, because it went out of time,how can I make it more efficient?
#include <iostream>
#include <fstream>
bool check(int s[], int n)
{
for (int i = 0; i < n; i++)
{
if (s[i] < s[i + 1] && i != n - 1)
{
return false;
}
}
return true;
}
int main()
{
std::ifstream in("input.txt");
std::ofstream out("output.txt");
int n;
int s[1000000];
bool legge = false;
int c = 0;
in >> n;
for (int i = 0; i < n; i++)
{
in >> s[i];
}
legge = check(s,n);
while (!legge)
{
for (int i = 0; i < n; i++)
{
int m = i+1;
if (s[i] < s[m] && i != n-1)
{
s[i] = s[m];
}
}
legge = check(s,n);
c ++;
}
out << c;
}
Text: In order to increase their efficiency, a new law mandates that every tower in a power line must be at least as high as the following one. Luca needs to change the height of some towers, in order to meet the new requirements. Every day, starting from the first tower, Luca will increase the height of every tower to the same height of the next one. Of course, if one tower is already taller than the following one it will not be modified. How many days does Luca need to finish the job?