题意

求一个序列当中 其 长度为 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. C. Glass Carving (CF Round #296 (Div. 2) STL--set的运用 &amp;&amp; 并查集方法)

    C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. 【Excle数据透视表】如何为一个字段添加多种分类汇总方式

    解决方案1 右键单击人员分类字段包含的任意单元格→右键→字段设置→自定义→(最大值.最小值) 解决方案2 单击人员分类→分析→字段设置

  3. 通用对话弹窗CommonDialog

    代码地址如下:http://www.demodashi.com/demo/12592.html 通用对话弹窗CommonDialog Version 1.0 Created by chenchangj ...

  4. Jenkins与Docker相关的Plugin使用

    原文地址:http://blog.csdn.net/ztsinghua/article/details/52128140 Jenkins与Docker相关的Plugin 在Jenkins Plugin ...

  5. Netbeans打开包括中文文件时提示错误

    Netbeans打开包括中文文件时提示错误.在Netbeans里找了半天没找到怎么设置,最后发现要改动Netbeans的配置文件才干解决. 编辑C:\Program Files\NetBeans 8. ...

  6. Spring事务管理简介

    © 版权声明:本文为博主原创文章,转载请注明出处 1.什么是事务 - 事务是指逻辑上的一组操作,这组操作要么全部成功,要么全部失败 2.事务特性(ACID) - 1.原子性(Atomicity):指事 ...

  7. java 实现HttpRequest 发送http请求

    package com.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStr ...

  8. Android Camera子系统之源码View

    本文基于Android 4.2.2+Linux3.6.9+SAMA5D3 SoC从源码的角度审视Android Camera子系统. 应用层 Androd原生Camera应用 /system/app/ ...

  9. 《Python基础教程》第20章学习笔记

    python实现:https://github.com/captainwong/instant_markup c++实现:https://github.com/captainwong/instant_ ...

  10. 线性判别函数-Fisher 线性判别

    这是我在上模式识别课程时的内容,也有参考这里. 线性判别函数的基本概念 判别函数为线性的情况的一般表达式 式中x是d 维特征向量,又称样本向量, 称为权向量, 分别表示为 是个常数,称为阈值权. 设样 ...