Created
March 13, 2015 14:03
-
-
Save alekseyl1992/fc718cd6da0fde423136 to your computer and use it in GitHub Desktop.
This file contains 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 <iostream> | |
#include <vector> | |
#include <limits> | |
#include <assert.h> | |
size_t solution(const std::vector<double>& data) { | |
std::vector<double> avgs(data.size(), 0); | |
size_t l = data.size(); | |
avgs[l - 1] = data[l - 1]; | |
size_t sliceSize = 1; | |
// calc avgs | |
for (int i = l - 2; i >= 0; --i, ++sliceSize) { | |
double continueAvg = (avgs[i + 1] * sliceSize + data[i]) / (sliceSize + 1); | |
double twoAvg = (data[i] + data[i + 1]) / 2; | |
if (twoAvg <= continueAvg) { | |
avgs[i] = twoAvg; | |
sliceSize = 2; | |
} else { | |
avgs[i] = continueAvg; | |
++sliceSize; | |
} | |
} | |
//find min | |
double min = std::numeric_limits<double>::max(); | |
size_t pos = 0; | |
for (size_t i = 0; i < avgs.size() - 1; ++i) { | |
if (avgs[i] < min) { | |
min = avgs[i]; | |
pos = i; | |
} | |
} | |
return pos; | |
} | |
int main() | |
{ | |
assert(solution({4, 2, -2, 5, 1, 5, -7}) == 5); | |
assert(solution({4, 2, 2, 5, 1, 5, 8}) == 1); | |
assert(solution({1, 1, 1, 8}) == 0); | |
assert(solution({8, 8, 8, 1}) == 2); | |
std::cout << "OK" << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment