Advertisement
Go-Ice

NTHU Online Judge #10925 (TLE)

Feb 29th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. void Down_Heap(int [], int, int);
  6. void Construct_by_Adjust(int [], int);
  7. void Heap_Sort(int [], int);
  8.  
  9. main(){
  10.     int index, index2;
  11.     int nodeAmount;
  12.     int length;     /* 0 < length < 10000 */
  13.     int answer;
  14.    
  15.     FILE *fptr;
  16.     fptr = fopen("D:\\PB.in.txt", "r");
  17.     if (fptr==NULL) return EXIT_FAILURE;
  18.    
  19.     float total_time = 0;
  20.     clock_t start_time, end_time;
  21.     start_time = clock(); /* microsecond */
  22.  
  23.     while ( fscanf(fptr, "%d", &length)!=EOF ){
  24.         int *arr1 = malloc(length * sizeof(int));
  25.         int *arr2 = malloc(length * sizeof(int));
  26.         int *sum  = malloc(length * length * sizeof(int));
  27.  
  28.         for ( index=0 ; index<length ; index++ )
  29.             fscanf(fptr, "%d", &arr1[index]);
  30.         for ( index=0 ; index<length ; index++ )
  31.             fscanf(fptr, "%d", &arr2[index]);
  32.  
  33.         for ( index=0, nodeAmount=0 ; index<length ; index++ )
  34.             for ( index2=0 ; index2<length ; index2++ )
  35.                 sum[nodeAmount++] = arr1[index] + arr2[index2];
  36.  
  37.         Heap_Sort( sum, nodeAmount );
  38.  
  39.         for ( index=0, answer=0 ; index<length ; index++ )
  40.             answer += sum[index];
  41.         printf("%d\n", answer);
  42.        
  43.         free(arr1); free(arr2);
  44.         break;
  45.     } /* END while */
  46.     end_time = clock();
  47.     total_time = (float)(end_time - start_time)/CLOCKS_PER_SEC;
  48.     printf("elapsed time: %.3f sec \n", total_time);
  49.    
  50.     fclose(fptr);
  51.     system("PAUSE");
  52.     return EXIT_SUCCESS;
  53. } /* END main() */
  54.  
  55. /* Max heap */
  56. /* move arr[index] to the button to meets the property of heap */
  57. void Down_Heap ( int arr[], int nodeAmount, int index ){
  58.     int son;
  59.     int up = arr[index];
  60.  
  61.     /* when arr[index] have son */
  62.     while ( index < nodeAmount/2 ){
  63.         son = 2*index + 1;  /* assume Left son is MAX son */
  64.         if ( son+1 < nodeAmount && arr[son]<arr[son+1] )
  65.             son++;          /* right son is MAX son */
  66.  
  67.         if ( up >= arr[son] ) /* arr[index] larger than MAX son */
  68.             break;
  69.  
  70.         /* else if arr[index] < arr[son] */
  71.         arr[index] = arr[son];
  72.         index = son;
  73.     }
  74.     arr[index] = up;
  75. } /* END Down_Heap() */
  76.  
  77. /* Max heap */
  78. void Construct_by_Adjust ( int arr[], int nodeAmount ){
  79.     int index;
  80.     for ( index = nodeAmount/2 - 1 ; index >= 0 ; index-- )
  81.         Down_Heap( arr, nodeAmount, index );
  82. } /* END Construct_by_Adjust() */
  83.  
  84. void Heap_Sort ( int arr[], int nodeAmount ){
  85.     int temp;
  86.     Construct_by_Adjust( arr, nodeAmount ); /* construct heap */
  87.  
  88.     while ( nodeAmount > 1 ){
  89.         /* switch the first element and the least element */
  90.         temp = arr[nodeAmount-1];
  91.         arr[nodeAmount-1] = arr[0];
  92.         arr[0] = temp;
  93.  
  94.         nodeAmount--;
  95.         Down_Heap( arr, nodeAmount, 0 );
  96.     }
  97. } /* END Heap_Sort() */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement