Advertisement
Go-Ice

NTHU Online Judge #10925 (AC)

Mar 1st, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.18 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. void Down_Heap(int [], int, int, int);
  6. void Construct_by_Adjust(int [], int, int);
  7. void Heap_Sort(int [], int, int);
  8. int length; /* 0 < length < 10000 */
  9.  
  10. main(){
  11.     int index, index2;
  12.     int nodeAmount;
  13.     int answer;
  14.     int tempMax;
  15.     float total_time = 0;
  16.     clock_t start_time, end_time;
  17.  
  18.     FILE *fptr;
  19.     fptr = fopen("D:\\PB.in.txt", "r");
  20.     if (fptr==NULL) return EXIT_FAILURE;
  21.  
  22.     start_time = clock(); /* microsecond */
  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.         Heap_Sort( arr1, length, 1 );
  34.         Heap_Sort( arr2, length, 1 );
  35.  
  36.         for ( index=0, nodeAmount=0 ; index<length ; index++ ){
  37.             for ( index2=0 ; index2<length ; index2++ ){
  38.  
  39.                 if ( index==0 ){
  40.                     sum[nodeAmount++] = arr1[index] + arr2[index2];
  41.                     continue;
  42.                 }
  43.  
  44.                 if ( arr1[index]+arr2[index2] < tempMax )
  45.                     sum[nodeAmount++] = arr1[index] + arr2[index2];
  46.                     else break;
  47.  
  48.             } /* END index2 for */
  49.             tempMax = sum[length-1];
  50.         } /* END index for */
  51.  
  52.         Heap_Sort( sum, nodeAmount, 2 );
  53.  
  54.         for ( index=nodeAmount-1, answer=0 ; index>=nodeAmount-length ; index-- )
  55.             answer += sum[index];
  56.         printf("%d\n", answer);
  57.  
  58.         free(arr1); free(arr2);
  59.         break;
  60.     } /* END while */
  61.  
  62.     end_time = clock();
  63.     total_time = (float)(end_time - start_time)/CLOCKS_PER_SEC;
  64.     printf("elapsed time: %.3f sec \n", total_time);
  65.  
  66.     fclose(fptr);
  67.     system("PAUSE");
  68.     return EXIT_SUCCESS;
  69. } /* END main() */
  70.  
  71. /* move arr[index] to the button to meets the property of heap */
  72. void Down_Heap ( int arr[], int nodeAmount, int index, int mode ){
  73.     int son;
  74.     int up = arr[index];
  75.  
  76.     /* when arr[index] have son */
  77.     while ( index < nodeAmount/2 ){
  78.         son = 2*index + 1;  /* assume Left son is MAX(min) son */
  79.  
  80.         /* Max heap */
  81.         if ( mode==1 ){
  82.             if ( son+1 < nodeAmount && arr[son]<arr[son+1] )
  83.                 son++;            /* right son is MAX son */
  84.  
  85.             if ( up >= arr[son] ) /* arr[index] larger than MAX son */
  86.                 break;
  87.         }
  88.  
  89.         /* min heap */
  90.         else if ( mode==2 ){
  91.             if ( son+1 < nodeAmount && arr[son]>arr[son+1] )
  92.                 son++;            /* right son is min son */
  93.  
  94.             if ( up <= arr[son] ) /* arr[index] less than min son */
  95.                 break;
  96.         }
  97.  
  98.         /* else if arr[index] < arr[son] ( arr[index] > arr[son] ) */
  99.         arr[index] = arr[son];
  100.         index = son;
  101.     } /* END while */
  102.     arr[index] = up;
  103. } /* END Down_Heap */
  104.  
  105. void Construct_by_Adjust ( int arr[], int nodeAmount, int mode ){
  106.     int index;
  107.  
  108.     if ( mode==1 )      /* build Max heap */
  109.         for ( index = nodeAmount/2 - 1 ; index >= 0 ; index-- )
  110.             Down_Heap( arr, nodeAmount, index, 1 );
  111.  
  112.     else if ( mode==2 ) /* build min heap */
  113.         for ( index = nodeAmount/2 - 1 ; index >= 0 ; index-- )
  114.             Down_Heap( arr, nodeAmount, index, 2 );
  115.  
  116. } /* END Construct_by_Adjust() */
  117.  
  118. void Heap_Sort ( int arr[], int nodeAmount, int mode ){
  119.     int temp;
  120.     int round = 0;
  121.  
  122.     if ( mode==1 ) Construct_by_Adjust( arr, nodeAmount, 1 );
  123.     else if ( mode==2 ) Construct_by_Adjust( arr, nodeAmount, 2 );
  124.  
  125.     while ( nodeAmount > 1 ){
  126.         /* switch the first element and the least element */
  127.         temp = arr[nodeAmount-1];
  128.         arr[nodeAmount-1] = arr[0];
  129.         arr[0] = temp;
  130.  
  131.         nodeAmount--;
  132.         round++;
  133.  
  134.         if ( mode==1 )
  135.             Down_Heap( arr, nodeAmount, 0, 1 );
  136.         else if ( mode==2 ){
  137.             Down_Heap( arr, nodeAmount, 0, 2 );
  138.             if ( round==length ) break;
  139.         }
  140.     }
  141. } /* END Heap_Sort() */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement