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

15 篇文章 0 订阅
2 篇文章 0 订阅

博主链接

题目链接

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值