题意

求一个序列当中 其 长度为 K 的子序列 中的 最大值 - 最小值

求 这个值 最小是多少

思路

先将序列排序

然后

I = 0, J = K - 1

然后 往下遍历

如果 arr[j] - arr[i] < ans

则更新答案 就可以了

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <climits>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll; const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-6; const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7; int arr[maxn] = {0}; int main()
{
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
sort (arr, arr + n);
int ans = INF;
for (int i = 0, j = k - 1; j < n; i++, j++)
{
int num = arr[j] - arr[i];
if (num < ans)
ans = num;
}
printf("%d\n", ans);
}

HackerRank - angry-children 【排序】的更多相关文章

  1. HackerRank "Angry Children 2"

    Fun one! A combination of Greedy and DP. The solution sparkled in my mind - I almost lost it.. Greed ...

  2. React.Children详解

    React.Children提供了处理this.props.children的工具,this.props.children可以任何数据(组件.字符串.函数等等).React.children有5个方法 ...

  3. Hadoop YARN:调度性能优化实践(转)

    https://tech.meituan.com/2019/08/01/hadoop-yarn-scheduling-performance-optimization-practice.html 文章 ...

  4. zk系列三:zookeeper实战之分布式锁实现

    一.分布式锁的通用实现思路 分布式锁的概念以及常规解决方案可以参考之前的博客:聊聊分布式锁的解决方案:今天我们先分析下分布式锁的实现思路: 首先,需要保证唯一性,即某一时点只能有一个线程访问某一资源: ...

  5. uva 10905 Children's Game (排序)

    题目连接:uva 10905 Children's Game 题目大意:给出n个数字, 找出一个序列,使得连续的数字组成的数值最大. 解题思路:排序,很容易想到将数值大的放在前面,数值小的放在后面.可 ...

  6. 【字符串排序,技巧!】UVa 10905 - Children’s Game

    There are lots of number games for children. These games are pretty easy to play but not so easy to ...

  7. UVa 10905 - Children's Game 排序,题目没有说输入是int 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  8. UVA10905: Children's Game(排序)

    题目:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/A 题目需求:,给n个数字,将它们重新排序得到一个最大的数 ...

  9. dragsort html拖拽排序

    一.Jquery List DragSort 对于有些页面,如首页的定制,需要进行动态的拖拽排序.由于自己实现比较困难,我们一般会使用一些js插件来实现.dragsort 就是帮助我们完成这一需求.通 ...

随机推荐

  1. xpinyin-函数返回多个值-lambda匿名函数-列表生成式-三元表达式

    import xpinyinp=xpinyin.Pinyin() #实例化print(p.get_pinyin('小白','')) 函数返回多个值:1.函数如果返回多个值的话,它会把这几个值放到一个元 ...

  2. discuz密码生成

    Discuz的密码加密算法其实就是两次MD5加密,首先用明文进行一次加密,之后随机生成一个salt,再把第一次的密文后面添加salt作为明文再进行一次MD5加密.salt保存在uc_members表里 ...

  3. C#的三大难点之二:托管与非托管

    相关文章: C#的三大难点之前传:什么时候应该使用C#?​C#的三大难点之一:byte与char,string与StringBuilderC#的三大难点之二:托管与非托管C#的三大难点之三:消息与事件 ...

  4. SVN 钩子操作-同步更新web目录

    一个简单的钩子演示:也可以网上搜索其他高级的 本次想要达到的功能是:每次用户commit 到仓库后,仓库的钩子会自动把程序又更新的www/的web发布目录 1.现在web目录下创建一个test.com ...

  5. SRIO常用缩写

    HELLO:Header Encoded Logical Layer Optimized (HELLO) format FTYPE:format type TTYPE:transaction type ...

  6. MySQL四-2:完整性约束

    阅读目录 一 介绍 二 not null与default 三 unique 四 primary key 五 auto_increment 六 foreign key 七 作业 一 介绍 约束条件与数据 ...

  7. PHP 依据IP地址获取所在城市

    有这种需求,须要依据用户的IP地址,定位用户所在的城市. 本文记录性文章,无逻辑性.有这样需求的朋友.能够直接拷贝使用.直接上代码,不需赘述. <? php header('Content-Ty ...

  8. Active Directory的基本概念

    前言 本文是面对准备加入Active Directory编程的初学者的一份文章,主要是讲解Active Directory(活动目录)的一些概念和相关知识.这篇文章本来是不想写下来的,因为概念性内容的 ...

  9. iOS自动化构建 xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/D...

    报这个错误的原因是xcode-select不在默认的路径 1.找到xcode-select的当前路径终端命令行 xcode-select --print-path /Library/Developer ...

  10. es6 初始化样式加载到head中

    Demo1:function loadCss(css) { css.forEach((path)=>{ console.log($('<link/>', { rel: 'styles ...