Skip to content

Instantly share code, notes, and snippets.

@Florian3k
Created November 24, 2017 21:37
Show Gist options
  • Save Florian3k/154080e34754a71590758686a3d21aef to your computer and use it in GitHub Desktop.
Save Florian3k/154080e34754a71590758686a3d21aef to your computer and use it in GitHub Desktop.
int calcLoad(int sectors[], int n)
{
//return 0;
if (n > 43)
{
n = 43;
}
int max = std::accumulate(sectors, sectors + (n > 7 ? 7 : n), 0);
if (n <= 7)
{
return max;
}
int nextRange = 12;
int elements = 6;
for (;;)
{
auto last = sectors + (nextRange + 1 > n ? n : nextRange + 1);
auto middle = last - elements;
auto first = middle - 7;
if (first < sectors)
{
first = sectors;
}
std::nth_element(first, middle, last);
int sum = std::accumulate(middle, last, 0);
max = max > sum ? max : sum;
if (elements > 1 && nextRange + 1 < n)
{
nextRange += 6;
elements -= 1;
}
else
{
break;
}
}
return max;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment