B. Ehab and subtraction
题目链接:https://codeforc.es/contest/1088/problem/B
题意:
给出n个数,给出k次操作,然后每次操作把所有数减去最小值,输出这个最小值,k用不完用0来补。
题解:
考虑到重复的数会被一起减去,所以我用了个set来存放这n个数,然后用个累加器记录下减去了多少最小值,把数取出来时减去这个累加器就好了,最后用0来补。
代码如下:
#include <bits/stdc++.h> using namespace std; const int N = 1e5+5; int n,k; int a[N]; set <int > S; int main(){ cin>>n>>k; for(int i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+n+1); for(int i=1;i<=n;i++) S.insert(a[i]); int cnt=0; for(auto it=S.begin();it!=S.end();it++){ cout<<*it-cnt<<endl; cnt+=*it-cnt; k--; if(k<=0) break; } while(k--) cout<<0<<endl; return 0; }