Fence Repair

我会带着你远行 2022-07-12 05:24 281阅读 0赞

Problem Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the “kerf”, the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn\‘t own a saw with which to cut the wood, so he mosies over to Farmer Don\‘s Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn\‘t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input

Line 1: One integer N, the number of planks

Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

Output

Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

Example Input

  1. 3
  2. 8
  3. 5
  4. 8

Example Output

  1. 34
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #define maxsize 1000
  6. typedef struct noden
  7. {
  8. int size;
  9. int capacity;
  10. int *data;
  11. }maxheap;
  12. maxheap * create(int b[],int top)
  13. {
  14. int i;
  15. maxheap * h;
  16. h=(maxheap *)malloc(sizeof(maxheap));
  17. h->data=(int *)malloc((maxsize+1)*sizeof(int));
  18. h->size=top+1;
  19. h->capacity=maxsize;
  20. h->data[0]=-1;
  21. for(i=1;i<=top+1;i++)
  22. {
  23. h->data[i]=b[i-1];
  24. }
  25. return h;
  26. }
  27. maxheap * adjust(maxheap * h)
  28. {
  29. int parent ,child;
  30. int tmp;
  31. int j;
  32. j=h->size/2;
  33. while(j>=1)
  34. {
  35. for(parent=j;parent*2<=h->size;parent=child)
  36. {
  37. child=2*parent;
  38. if(child!=h->size)
  39. {
  40. if(h->data[child]>h->data[child+1])
  41. {
  42. child++;
  43. }
  44. }
  45. if(h->data[parent]>h->data[child])
  46. {
  47. tmp=h->data[parent];
  48. h->data[parent]=h->data[child];
  49. h->data[child]=tmp;
  50. }
  51. else
  52. {
  53. break;
  54. }
  55. }
  56. j--;
  57. }
  58. return h;
  59. }
  60. int dele(maxheap * h)
  61. {
  62. int parent ,child;
  63. int minnum;
  64. int item;
  65. minnum=h->data[1];
  66. item=h->data[h->size];
  67. h->size--;
  68. for(parent=1;2*parent<=h->size;parent=child)
  69. {
  70. child=2*parent;
  71. if(child!=h->size)
  72. {
  73. if(h->data[child]>h->data[child+1])
  74. {
  75. child++;
  76. }
  77. }
  78. if(item<h->data[child])
  79. {
  80. break;
  81. }
  82. else
  83. {
  84. h->data[parent]=h->data[child];
  85. }
  86. }
  87. h->data[parent]=item;
  88. return minnum;
  89. }
  90. void insert(int item,maxheap * h)
  91. {
  92. int parent,child;
  93. h->size++;
  94. parent=h->size/2;
  95. child=h->size;
  96. for(;item<h->data[parent];parent=parent/2)
  97. {
  98. h->data[child]=h->data[parent];
  99. child/=2;
  100. }
  101. h->data[child]=item;
  102. }
  103. long long * create_hfmtree(maxheap * h)
  104. {
  105. int i;
  106. int a,b;
  107. int k;
  108. long long max=0;
  109. k=h->size;
  110. for(i=1;i<k;i++)
  111. {
  112. a=dele(h);
  113. b=dele(h);
  114. insert(a+b,h);
  115. max+=a+b;
  116. }
  117. return max;
  118. }
  119. int main()
  120. {
  121. long long ans;
  122. int top,n;
  123. int b[20000];
  124. maxheap * heap;
  125. scanf("%d",&n);
  126. for(top=-1;top<n-1;)
  127. {
  128. scanf("%d",&b[++top]);
  129. }
  130. heap=create(b,top);
  131. heap=adjust(heap);
  132. ans=create_hfmtree(heap);
  133. printf("%lld\n",ans);
  134. return 0;
  135. }

发表评论

表情:
评论列表 (有 0 条评论,281人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Fence Repair

    think: 1 今天AC了人生中第一道英文Oj题目,内心也不知如何描述,一开始提交发现wrong answer, 后来借鉴了鑫哥的代码,发现虽然和鑫哥用的方法不同,但注意

    相关 Fence Repair

    think: 1 今天AC了人生中第一道英文Oj题目,内心也不知如何描述,一开始提交发现wrong answer, 后来借鉴了鑫哥的代码,发现虽然和鑫哥用的方法不同,但注意