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
Input
The first line contains space-separated integers n
Output
Print a single integer — the least possible difference the teacher can obtain.
Example
Input4 6Output
10 12 10 7 5 22
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距離計算最短的差距即可
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int c[55]; int main() { int n,m,minn=1005; scanf("%d %d",&n,&m); for(int i=1;i<=m;i++) scanf("%d",&c[i]); sort(c,c+m+1); /*for(int i=1;i<=m;i++) cout<<c[i]<<" ";*/ //cout<<endl; for(int i=n;i<=m;i++) if((c[i]-c[i-n+1])<minn) { minn=c[i]-c[i-n+1]; //cout<<i<<" "<<i-n+1<<" "<<c[i]<<" "<<c[i-n+1]<<" "<<minn<<endl; } printf("%d",minn); return 0; }
CodeForces - 337A Puzzles