fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void swap(int x, int y)
  5. {
  6. int temp;
  7. temp=x;
  8. x=y;
  9. y=temp;
  10. }
  11.  
  12. void min_heap(int a[], int size, int index)
  13. {
  14. int p;
  15. do
  16. {
  17. p=(index-1)/2;
  18. if (a[p]<a[index])
  19. swap(a[p],a[index]);
  20. index=p;
  21. }while (index>0);
  22. }
  23.  
  24. void extract_min(int a[], int n, int index)
  25. {
  26. int lc,rc;
  27. do
  28. {
  29. lc=2*index+1;
  30. rc=2*index+2;
  31. if (lc<n && a[index]<a[lc])
  32. {
  33. swap(a[index],a[lc]);
  34. index=lc;
  35. }
  36. else if (rc<n && a[index]<a[rc])
  37. {
  38. swap(a[index],a[rc]);
  39. index=rc;
  40. }
  41. }while (index<n);
  42. }
  43.  
  44. int main() {
  45.  
  46. int t,n,k,a[100];
  47. cin>>t;
  48.  
  49. for (int i=0;i<t;i++)
  50. {
  51. cin>>n>>k;
  52. for (int j=0;j<n;j++)
  53. {
  54. cin>>a[j];
  55. min_heap(a,n,j);
  56. }
  57. cout<<"hello"<<endl;
  58. for (int j=0;j<k;j++)
  59. {
  60. cout<<a[0]<<" ";
  61. a[0]=a[n-1];
  62. n=n-1;
  63. extract_min(a,n,0);
  64. }
  65. }
  66. return 0;
  67. }
Time limit exceeded #stdin #stdout 5s 15240KB
stdin
1
5 2
12 5 787 1 23
stdout
hello