B. Ehab and subtraction
阿新 • • 發佈:2018-12-06
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; }
這個貼上格式。。。無語了
啊,這個一下寫好了,是想的排序後,每一次減掉每一次最小值,相當於模擬了導致超時,但是沒有想到這個本質,應該相當於減掉前面一個數,所以要儘量化簡,越簡單,越不容易錯。