Description
给你n个整数,请按从大到小的顺序输出其中前m大的数。
Input
每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。
Output
对每组测试数据按从大到小的顺序输出前m大的数。
Sample Input
5 3 3 -35 92 213 -644
Sample Output
213 92 3
HintHint
请用VC/VC++提交
大意: 注意范围,加一个正数进去。

#include<cstring> #include<cstdio> using namespace std; const int MAX = 1000015; const int N = 510000; int w[MAX],a[MAX],p[MAX]; void sort(int *a,int n,int mx) { for(int i =1 ; i<= mx;i++) w[i] =0; for(int i = 1; i <= n;i++) w[a[i]]++; for(int i =1; i <= mx;i++) w[i] += w[i-1]; for(int i = n; i >= 1; i--){ p[w[a[i]]] = a[i]; w[a[i]]--; } } int main() { int n,m; scanf("%d%d",&n,&m); for(int i = 1; i <= n; i++){ scanf("%d",&a[i]); a[i] += N; } sort(a,n,MAX); for(int i = n ; i >= n-m+1 ;i--) printf("%d ",p[i]-N); return 0; }