Find Monopoly
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3541 accepted submission (s): 1503
Problem description There are n people in Wuzhen, Tongxiang, Zhejiang province. Please find the first M monopoly in this town.
The input contains multiple groups of test cases.
Each use case contains two integers, n (0 <n <= 100000) and M (0 <m <= 10). N indicates the number of people in the town, M is the number of richest people to be identified. Next, enter the wealth values of N people in the town.
If n and m are both 0, the input ends.
Output please output the number of properties of the first M richest people in Wuzhen, and the number of items in front of the multiple items. If there are less than m items, all items are output, and each group occupies one row.
Sample Input
3 12 5 -15 31 2 3 4 50 0
Sample output
55 4 3
#include<stdio.h>#include<stdlib.h>#define MAXN 100000+10int a[MAXN];int cmp(const void *b,const void *a){return *(int *)a-*(int *)b;}int main(){int n,m;while(scanf("%d%d",&n,&m)&&n!=0&&m!=0){int i,j;for(i=0;i<n;i++)scanf("%d",&a[i]);qsort(a,n,sizeof(a[0]),cmp);printf("%d",a[0]);for(j=1;j<m;j++)printf(" %d",a[j]);printf("\n");}return 0;}