博主链接

题目链接

Note

In the first sample:

In the first step: the array is [1,2,3][1,2,3], so the minimum non-zero element is 1.

In the second step: the array is [0,1,2][0,1,2], so the minimum non-zero element is 1.

In the third step: the array is [0,0,1][0,0,1], so the minimum non-zero element is 1.

In the fourth and fifth step: the array is [0,0,0][0,0,0], so we printed 0.

In the second sample:

In the first step: the array is [10,3,5,3][10,3,5,3], so the minimum non-zero element is 3.

In the second step: the array is [7,0,2,0][7,0,2,0], so the minimum non-zero element is 2.

题意:

给你两个整数n,k,然后n个整数a[1~n]。然后进行k次操作,每次操作打印出最小非零元素,并把所有非零元素减去这个数;如果没有没有非零数了就打印零

题解:

先用sort对a排序,然后从最小位置开始消,同时定义一个变量sum,记录每一次操作前面减去数字的和,也就是当前位置需要减的数字。同时特判下当前位置数-sum==0,如果为真直接跳过看下一位,同时因为跳过了所以一定要k++;

代码:

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+7;
const int mod=1e9+7;
int a[maxn];
int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)scanf("%d",&a[i]);
    sort(a,a+n);
    ll sum=0;
    for(int i=0;i<k;i++){
        if(i>=n)printf("0\n");
        else{
            if(a[i]-sum==0){
                k++;
                continue;
            }
            printf("%d\n",a[i]-sum);
            sum=a[i];
        }
    }
    return 0;
}