zoukankan      html  css  js  c++  java
  • CodeForces -337A:Puzzles(水)

    The end of the school year is near and Ms. Manana, the teacher, will soon have to say goodbye to a yet another class. She decided to prepare a goodbye present for her n students and give each of them a jigsaw puzzle (which, as wikipedia states, is a tiling puzzle that requires the assembly of numerous small, often oddly shaped, interlocking and tessellating pieces).

    The shop assistant told the teacher that there are m puzzles in the shop, but they might differ in difficulty and size. Specifically, the first jigsaw puzzle consists of f1 pieces, the second one consists of f2 pieces and so on.

    Ms. Manana doesn't want to upset the children, so she decided that the difference between the numbers of pieces in her presents must be as small as possible. Let A be the number of pieces in the largest puzzle that the teacher buys and B be the number of pieces in the smallest such puzzle. She wants to choose such n puzzles that A - B is minimum possible. Help the teacher and find the least possible value of A - B.

    Input

    The first line contains space-separated integers n and m (2 ≤ n ≤ m ≤ 50). The second line contains m space-separated integers f1, f2, ..., f**m (4 ≤ f**i ≤ 1000) — the quantities of pieces in the puzzles sold in the shop.

    Output

    Print a single integer — the least possible difference the teacher can obtain.

    Examples

    input

    4 6
    10 12 10 7 5 22
    

    output

    5
    

    Note

    Sample 1. The class has 4 students. The shop sells 6 puzzles. If Ms. Manana buys the first four puzzles consisting of 10, 12, 10 and 7 pieces correspondingly, then the difference between the sizes of the largest and the smallest puzzle will be equal to 5. It is impossible to obtain a smaller difference. Note that the teacher can also buy puzzles 1, 3, 4 and 5 to obtain the difference 5.

    题意:

    m个元素,要求选择n个,使得其中最大值减最小值最小

    解题思路:

    排序。肯定就是在连续n个元素中才会出现最大值减最小值的情况,而这个值就是第n个减第1个,整个区间往右移找出最小的就可以了

    #include<bits/stdc++.h>
    using namespace std;
    int main() {
        int n, a, c = 10001;
        cin >> n >> a;
        int b[a];
        for (int i = 0; i < a; i++) {
            cin >> b[i];
        }
        sort(b, b + a);
        for (int j = 0; j <= a - n; j++) {
            c = min(c, abs(b[j + n - 1] - b[j]));
        }
        cout << c;
    }
    
  • 相关阅读:
    第一次冲次(补)
    软件工程概论个人总结
    第16周进度条
    读《梦断代码》第2章有感
    java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
    传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确。参数 1 (""): 数据类型 0x38 未知。
    怎么卸载VS2005呢?控制面板里内容太多,找不出哪些是属于VS2005?我的VS2005出问题了
    常用正则表达式的写法
    Http响应头字段详解,MyEclipse配置tomcat,servlet运行方式
    html框架 字体颜色 列表 表格 图片 定义列表 a标签
  • 原文地址:https://www.cnblogs.com/RioTian/p/13184291.html
Copyright © 2011-2022 走看看