Looking for a Millionaire
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4504 Accepted Submission (s): 1838
Problem description Zhejiang Tongxiang Wu Town A total of n people, please find out the town of the former m monopoly.
Input inputs contain multiple sets of test cases.
Each use case first contains 2 integers n (0<n<=100000) and M (0<m<=10), Where: N is the number of people in town, M is the number of millionaires that need to be found, and the next line is to enter the wealth value of N people in the town.
N and m at 0 indicate the end of the input.
Output please export the wealth of the former m monopoly of Wuzhen, the number of assets in front of the row, if the monopoly less than m, then all output, each group of output accounted for one row.
Sample Input3 12 5-15 31 2 3 4 50 0
Sample Output55 4 3 would have thought the priority queue speed would have been faster. More than 100 mssort sorted:
#include <stdio.h> #include <string.h> #include <queue> #include <algorithm>using namespace std ; bool CMP (int a,int b) {return a>b;} int Str[100010];int Main () {int n,m,j,i,s,t;while (scanf ("%d%d", &n,&m) &&n!=0&&m!=0) {for (i=0 ; i<n;i++) {scanf ("%d", &str[i]);} Sort (str,str+n,cmp), if (n>m) {for (i=0;i<m;i++) { if (i==0) printf ("%d", Str[i]); else printf ("%d", Str[i]); } printf ("\ n");} Else{for (i=0;i<n;i++) { if (i==0) printf ("%d", Str[i]); else printf ("%d", Str[i]); }
Priority queue:
#include <stdio.h> #include <queue>using namespace Std;int main () {int n,m,j,i,t,k;while (scanf ("%d%d", &N,&M) &&n!=0&&m!=0) {Priority_queue<int>q;int c=n;while (c--) {scanf ("%d", &t); Q.push (t);} if (n<m) {k=n;n=m;m=k;} int A=n-m;int p=0;while (q.size () >a) {if (p==0) printf ("%d", Q.top ()), elseprintf ("%d", Q.top ());p ++;q.pop ();} printf ("\ n");} return 0;}
Hdoj 3785 looking for monopoly "priority queue +sort Sort"