Herowaree

UVa Problem

May 23rd, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. //Resolução UVa 10954 - Thulio Guilherme
  2.  
  3. #include <stdio.h>
  4.  
  5. void quick_sort(int *a, int left, int right);
  6.  
  7. int main(){
  8.  
  9. int num, count;
  10.  
  11. while(1){
  12. scanf("%d", &num);
  13. if(num==0) return 0;
  14.  
  15. int array[num];
  16. for(count = 0; count < num; count++){
  17. scanf("%d", &array[count]);
  18. }
  19.  
  20. int total = 0, cost = 0;
  21. int i, j;
  22.  
  23. while(num != 1){
  24.  
  25. quick_sort(array, 0, num - 1);
  26.  
  27. total = array[num - 2] + array[num -1];
  28. cost = cost + total;
  29. array[num - 1] = '\0';
  30. array[num - 2] = total;
  31. num--;
  32. }
  33. printf("%d\n", cost);
  34. }
  35.  
  36. return 0;
  37. }
  38.  
  39. void quick_sort(int *a, int left, int right) {
  40. int i, j, x, y;
  41.  
  42. i = left;
  43. j = right;
  44. x = a[(left + right) / 2];
  45.  
  46. while(i <= j) {
  47. while(a[i] > x && i < right) {
  48. i++;
  49. }
  50. while(a[j] < x && j > left) {
  51. j--;
  52. }
  53. if(i <= j) {
  54. y = a[i];
  55. a[i] = a[j];
  56. a[j] = y;
  57. i++;
  58. j--;
  59. }
  60. }
  61.  
  62. if(j > left) {
  63. quick_sort(a, left, j);
  64. }
  65. if(i < right) {
  66. quick_sort(a, i, right);
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment