Created
November 26, 2017 18:33
-
-
Save Florian3k/f4d5c42ba8b2fa08b5056f11f5fdec75 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
namespace fornal { | |
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