DAY4.12.A - Build a Contest

A - Build a Contest

You’re given an array aa. You should repeat the following operation kk times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.

Input
The first line contains integers nn and kk (1≤n,k≤105)(1≤n,k≤105), the length of the array and the number of operations you should perform.

The second line contains nn space-separated integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109), the elements of the array.

Output
Print the minimum non-zero element before each operation in a new line.

Examples
Input
3 5
1 2 3
Output
1
1
1
0
0
Input
4 2
10 3 5 3
Output
3
2

思路与过程

先排序,之后用一个SUM记录到当前一共需要减掉多少。

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	long long n,k,i,j,q,sum,temp;
	static int a[100010];
	cin>>n>>k;
	for(i=1;i<=n;i++)
	cin>>a[i];
	sort(a+1,a+1+n);
	i=1;
	sum=0;
	bool t=true;
	for(q=1;q<=k;q++)
	{
		if(!t) 
		{
			cout<<"0"<<endl;
			continue;
		}
		for(j=i;j<=n;j++) 
		{
			if (a[j]-sum>0)
			{
				i=j+1;
				temp=a[j]-sum;
				sum=sum+temp;
				cout<<temp<<endl;
				break;
			}
		}
		if(a[n]==sum) t=false;
	}
	return 0;
 } 

后记

从今天开始就用日期记录咯,不然记不清。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值