HDU 3785 find monopoly (SORT)
Find Monopoly
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 4132 Accepted Submission (s): 1698
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 first contains two integers n (0 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
# Include
# Include
Using namespace std; int a [100010]; int aux [100010]; void swap (int * a, int * B) {int t = * a; * a = * B; * B = t;} int partition (int a [], int l, int h) {int I = l; int j = h + 1; int v = a [l]; while (1) {while (a [++ I]> v) if (I = h) break; while (a [-- j]
= J) break; swap (& a [I], & a [j]);} swap (& a [l], & a [j]); return j ;} void q_sort (int a [], int l, int h) {if (l> = h) return; int j = partition (a, l, h); q_sort (, l, J-1); q_sort (a, j + 1, h);} void merge (int a [], int l, int mid, int h) {int I = l; int j = mid + 1; for (int k = l; k <= h; ++ k) aux [k] = a [k]; for (int k = l; k <= h; ++ k) {if (I> mid) a [k] = aux [j ++]; else if (j> h) a [k] = aux [I ++]; else if (aux [I]> aux [j]) a [k] = aux [I ++]; else a [k] = aux [j ++] ;}} void m_sort (int a [], int l, int h) {if (h <= l) return; int mid = l + (h-l)/2; m_sort (a, l, mid); m_sort (a, mid + 1, h); merge (a, l, mid, h);} int main (int argc, char * argv []) {freopen ("random 5.in"," r ", stdin); int n, m; while (scanf ("% d", & n, & m) = 2) {memset (a, 0, sizeof ()); if (n = 0 & m = 0) return 0; for (int I = 0; I
Merged and quickly written each time ~