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;
}
后记
从今天开始就用日期记录咯,不然记不清。。。