fork(1) download
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<vector>
  6. #include<string>
  7. #include<algorithm>
  8. #include<utility>
  9. #include<set>
  10. #include<queue>
  11.  
  12. using namespace std;
  13.  
  14. void printArray(int* a,int n){
  15.  
  16. printf("%d",a[0]);
  17. for(int i=1;i<n;i++){
  18. printf(" %d",a[i]);
  19. }
  20. printf("\n");
  21.  
  22. return;
  23. }
  24.  
  25. void sortCheck(int* a,int n);
  26.  
  27. void bogoBogoSort(int* a,int n){
  28.  
  29. bool isSorted;
  30. do{
  31. random_shuffle(a,a+n);
  32.  
  33. int chk[n];
  34.  
  35. copy(a,a+n,chk);
  36.  
  37. sortCheck(chk,n);
  38.  
  39. isSorted = true;
  40. for(int i=0;i<n;i++){
  41. if(a[i] != chk[i]){
  42. isSorted = false;
  43. break;
  44. }
  45. }
  46.  
  47. }while(!isSorted);
  48.  
  49. }
  50.  
  51.  
  52. void sortCheck(int* a,int n){
  53.  
  54. if(n == 1){
  55. return;
  56. }
  57.  
  58. while(true){
  59. bogoBogoSort(a,n-1);
  60.  
  61. if(a[n-2]<=a[n-1])return;
  62. random_shuffle(a,a+n);
  63. }
  64.  
  65. return;
  66. }
  67.  
  68. int main(){
  69.  
  70. int NUM;
  71. cin>>NUM;
  72. int a[NUM];
  73.  
  74. srand(time(0));
  75.  
  76. for(int i=0;i<NUM;i++){
  77. a[i] = rand();
  78. }
  79.  
  80. printArray(a,NUM);
  81.  
  82. bogoBogoSort(a,NUM);
  83.  
  84. printArray(a,NUM);
  85.  
  86. return 0;
  87. }
  88.  
Time limit exceeded #stdin #stdout 15s 2728KB
stdin
6
stdout
Standard output is empty