其他分享
首页 > 其他分享> > 7-38 寻找大富翁 (25分)--排序

7-38 寻找大富翁 (25分)--排序

作者:互联网

多提交几次就不超时了(#滑稽)

 1 #include<iostream>
 2 using namespace std;
 3 long  int a[1000005];
 4 long  int N;
 5 inline void swap(long int* a, long int* b)
 6 {
 7     int temp = *a;
 8     *a = *b;
 9     *b = temp;
10 }
11 inline void percdown(long  int a[], long int r, long int N)
12 {
13     long int child;
14     long int temp;
15     
16     temp = a[r]; 
17     for (; 2 * r + 1 < N; r = child)
18     {
19         child = 2 * r + 1;
20         if (2 * r + 2 < N && a[2 * r + 2] < a[2 * r + 1]) 
21             child++;
22         if (a[child] < temp&& child < N) 24             a[r] = a[child];
25         }
26         else
27             break;
28     }
29     a[r] = temp; 
30 }
31 
32 void Heap_Sort(long  int a[])
33 {
34     
35     for (long int i = N / 2; i >= 0; i--)
36     {
37         percdown(a, i, N);
38     }
39    
40     for (long int i = N - 1; i > 0; i--) 42         swap(&a[0], &a[i]);
43         percdown(a, 0, i);
44     }
45 }
46  int main()
47 {
48     long  int M;
49     cin >> N >> M;
50     if(N<M)M=N;
51     for (long  int i = 0; i < N; i++)
52     {
53         cin >> a[i];
54     }
55     Heap_Sort(a);
56     for (long  int i = 0; i < M; i++)
57     {
58         printf("%ld",a[i]);
59             if (i != M - 1)printf(" ");
60     }
61     return 0;
62 }

 

标签:25,38,temp,int,大富翁,void,long,percdown,child
来源: https://www.cnblogs.com/2020R/p/12770984.html