CodeForces 337A Puzzles(极值差最小)
2016-10-22 23:00
239 查看
A. Puzzles
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 integersf1, f2, ..., fm (4 ≤ fi ≤ 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 个,问所取出的最小极值差(最大值 - 最小值)。
思路:
模拟走一遍
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 472D 最小生成树
- codeforces 545E E. Paths and Trees(单源最短路+总权重最小)
- Codeforces 903G. Yet Another Maxflow Problem 最小割+线段树
- Codeforces 240E. Road Repairs 最小树形图+输出路径
- codeForces 472D 最小生成树
- 【codeforces 721D】【贪心 堆】D. Maxim and Array 【给出n个数,k次机会,每次使得任意一个数字减少或者加上x,使得最后的乘积最小,最后的n个数大小】
- 【codeforces 27 E】【反素数】【给一个数n,求一个最小的正整数,使得它的因子个数为n】
- codeforces 733 F. Drivers Dissatisfaction(最小生成树+lca+倍增去环)
- CodeForces - 240E Road Repairs(最小树形图+输出路径)
- Codeforces 842D Vitya and Strange Lesson【逆向思维+字典树查询亦或最小值】
- Codeforces 600C Make Palindrome 【贪心 找字典序最小回文串】
- CodeForces 609E Minimum spanning tree for each edge (lca+最小生成树+倍增)
- CodeForces 724E(Goods transportation 最小割)
- CodeForces 337C Quiz(二分,快速幂,答题得分最小值)
- PCA、最小平方差等中用拉格朗日求极值时对常见的一个f(x)=X'A'AX求导的证明
- Codeforces 892/E Envy 最小生成树的query
- CodeForces - 939D Love Rescue (最小生成树)
- [codeforces] 854D. Jury Meeting(前/后缀最小值)
- 【Codeforces 609E】【Stree】【最小生成树】