http://community.topcoder.com/stat?c=problem_statement&pm=13625&rd=16278
First of all, if you remember the question of the minimum distance in Manhattan, you will think of a one-dimensional case where the point appears to be the smallest in the median. It is also shown here that four points, which appear at the median position, are the smallest.
The way to do this is to test all the things that minimize the absolute value of an idea.
My code is a little ugly, but it's over:
#include <vector> #include <algorithm>using namespace Std;class theconsecutiveintegersdivone {public:int Find (vector <int> numbers, int k) {sort (Numbers.begin (), Numbers.end ()); int d = K/2;int left = 0;int SumL = 0;for (int i = left, I < left + D; i++) {SumL + = Numbers[i];} int SUMR = 0;int right = left + d;if (k% 2 = = 1) {right++;} for (int i = right, I < right + D; i++) {SUMR + = Numbers[i];} int result = sumr-suml-d * D;while (right + D < numbers.size ()) {SumL + = Numbers[left + d];suml-= Numbers[left];su MR + = numbers[right + D];sumr = numbers[right];result = Min (result, sumr-suml-d * d); left++;right++;} return result;};
[TopCoder] Theconsecutiveintegersdivone