B. Ehab and subtraction

                              B. Ehab and subtractiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou'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.InputThe 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.OutputPrint the minimum non-zero element before each operation in a new line.ExamplesinputCopy3 5

1 2 3
outputCopy1
1
1
0
0
inputCopy4 2
10 3 5 3
outputCopy3
2
NoteIn 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.



#include<bits/stdc++.h>

using namespace std;

int a[1000001];int j;int x;

int main()

{

  
int n,k;

  
cin>>n>>k;

  
for(int i=0;i<n;i++)

   {

      
cin>>a[i];

   }

  
sort(a,a+n);

  
while(k--)

{  
while(a[j]-x==0&&j<n-1)

      
{

          
j++;

      
}

      
cout<<a[j]-x<<endl;

      
x=a[j];

 

   }

  
return 0;

}

 


这个粘贴格式。。。无语了
啊,这个一下写好了,是想的排序后,每一次减掉每一次最小值,相当于模拟了导致超时,但是没有想到这个本质,应该相当于减掉前面一个数,所以要尽量化简,越简单,越不容易错。

猜你喜欢

转载自blog.csdn.net/qq_42865713/article/details/84838956