Codeforces ~ 1088B ~ Ehab and subtraction (set,模拟)

题意

给你一个长度为n序列,让你重复m次以下操作:选出序列中最小的正(>0)整数,输出这个数字,所有数字减去这个数,全为0就输出0。

思路

通过手推第一个样例发现每次减去的值是两个不同的数的差值,所以直接放入set,每次输出当前值减去上一个值即可。

#include<bits/stdc++.h>
using namespace std;
int n, k;
set<int> s;
int main()
{
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; i++)
    {
        int a; scanf("%d", &a);
        s.insert(a);
    }
    s.insert(0);
    auto it = next(s.begin());
    while (k--)
    {
        if (it == s.end()) { printf("0\n"); continue; }
        printf("%d\n", *it - *prev(it));
        it++;
    }
    return 0;
}
/*
3 5
1 2 3
*/

相关文章: