[TopCoder] Theconsecutiveintegersdivone

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.