码迷,mamicode.com
首页 > 其他好文 > 详细

HackerRank - Max Min

时间:2015-02-27 07:42:23      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

https://www.hackerrank.com/challenges/angry-children

Among N ints, pick K with min ‘unfairness‘ (max of k - min of k). Here‘s the strategy: larger numbers should be picked together with other larger numbers, and only by this can we make sure a smaller unfairness - that means: sorting. Then we can go through each sorted K section: data[i + k - 1] - data[i]:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() 
{
int N, K, unfairness; cin >> N >> K; vector<int> candies(N); for (int i=0; i<N; i++) cin >> candies[i]; std::sort(candies.begin(), candies.end()); unfairness = std::numeric_limits<int>::max(); for(int i = 0; i <= N - K ; i ++) { unfairness = std::min(unfairness, candies[i + K - 1] - candies[i]); } cout << unfairness << "\n"; return 0; }

HackerRank - Max Min

标签:

原文地址:http://www.cnblogs.com/tonix/p/4302503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!