Codeforces Round #525 (Div. 2)B. Ehab and subtraction

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;
}

 

转载于:https://www.cnblogs.com/heyuhhh/p/10074020.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值