传送门:https://codeforces.com/problemset/problem/1088/B
B.Ehab and subtraction
You’re given an array ?. You should repeat the following operation ? times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
Input
The first line contains integers ? and ? (1≤?,?≤105), the length of the array and the number of operations you should perform.
The second line contains ? space-separated integers ?1,?2,…,?? (1≤??≤109), the elements of the array.
Output
Print the minimum non-zero element before each operation in a new line.
Examples
inputCopy
3 5
1 2 3
outputCopy
1
1
1
0
0
inputCopy
4 2
10 3 5 3
outputCopy
3
2
Note
In the first sample:
In the first step: the array is [1,2,3], so the minimum non-zero element is 1.
In the second step: the array is [0,1,2], so the minimum non-zero element is 1.
In the third step: the array is [0,0,1], so the minimum non-zero element is 1.
In the fourth and fifth step: the array is [0,0,0], so we printed 0.
In the second sample:
In the first step: the array is [10,3,5,3], so the minimum non-zero element is 3.
In the second step: the array is [7,0,2,0], so the minimum non-zero element is 2.
题意:输入n个数,k个操作,每次数组元素全部减去最小的元素,但如果是0并且后面的元素不为0的情况下就跳过。
首先会有个误区,就是每次求出最小元素,然后让所有元素都去减掉它。这样会很麻烦。不然换一种思路,我们先将数组元素排序,从第一项开始遍历,如果相邻元素相等时(这个时候就是0,需要跳过),不相等时就相减然后输出。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define ll long long
#define ld long double
#define ull unsigned long long
#define inf 0x3f3f3f3f
#define rep(i,a,b)for(int i=a;i<=b;i++)
#define rep1(i,a,b)for(int i=a;i<b;i++)
#define rep2(i,a,b)for(int i=a;i>=b;i--)
#define rep3(i,a,b)for(int i=a;i>b;i--)
using namespace std;
const int maxn=3e5+10;
int n,k;
int arr[maxn];
int cmp(int a,int b)
{
return a<b;
}
int main(){
scanf("%d%d",&n,&k);
rep(i,1,n)
scanf("%d",&arr[i]);
sort(arr+1,arr+n+1,cmp);
int ans=0;
rep(i,1,k){
if(i>n)
printf("0\n");
else{
if(arr[i]-ans==0){
k++;
continue;}
printf("%d\n",arr[i]-ans);
ans=arr[i];
}
}
return 0;
}