CodeForces 337A Puzzles(极值差最小)
来源:互联网 发布:最后一次网络歌手 编辑:程序博客网 时间:2024/06/03 17:02
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.
The first line contains space-separated integers n and m (2 ≤ n ≤ m ≤ 50). The second line contains m space-separated integersf1, f2, ..., fm (4 ≤ fi ≤ 1000) — the quantities of pieces in the puzzles sold in the shop.
Print a single integer — the least possible difference the teacher can obtain.
4 610 12 10 7 5 22
5
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 个,问所取出的最小极值差(最大值 - 最小值)。
思路:
模拟走一遍
AC:
#include<stdio.h>#include<cstring>#include<algorithm>#define AC main()using namespace std;const int MYDD = 1103;int AC {int n, m, f[MYDD];while(scanf("%d %d", &n, &m) != EOF) {for(int j = 1; j <= m; j++) {scanf("%d", &f[j]);}sort(f+1, f+m+1);int ans = MYDD;for(int j = 1; j <= m-n+1; j++) {ans = min(ans, f[j+n-1]-f[j]);}printf("%d\n", ans);}return 0;}
- CodeForces 337A Puzzles(极值差最小)
- CodeForces 337A Puzzles
- CodeForces 337A - Puzzles
- codeforces 337A Puzzles
- codeforces 337A(Puzzles) Java
- CodeForces -337A:Puzzles(水)
- codeforces----191A Dynasty Puzzles
- codeforces 191A Dynasty Puzzles
- CodeForces - 191A Dynasty Puzzles
- Codeforces Round #196 (Div. 2) / 337A Puzzles (搜索)
- Codeforces-191A-Dynasty Puzzles(简单dp)
- codeforces 191A Dynasty Puzzles(简单DP)
- Codeforence 337A Puzzles
- codeforces 191A A. Dynasty Puzzles(dp)
- CodeForces Round #121 (191A) - Dynasty Puzzles
- CodeForces#196Div.2-A. Puzzles
- Codeforces Round #196 (Div. 2) A. Puzzles
- Codeforces Round #196 (Div. 2) A. Puzzles
- Another app is currently holding the yum lock; waiting for it to exit...
- b
- 智慧xxx项目记录(二)进入内容页和侧边栏绘制
- AES128比特10轮密钥编排方案
- Linux下的C语言编程——判断大端小段字节序
- CodeForces 337A Puzzles(极值差最小)
- 8.内存管理的概述
- for循环嵌套
- python3 文件迭代器
- SqlServer窗口函数
- 中国大学MOOC-陈越、何钦铭-数据结构-2016秋期中考试
- cf(聚簇因子)调优,计划调优,SQL改造
- 汇编知识
- Java Web网页编程常见报错及处理方法