Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 22703 | Accepted: 7236 |
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.
农民约翰想修一个围绕牧场的围栏。他测量了围栏长度,并发现他需要N(1≤N≤20,000)个木板,每个有一些整数长度Li (1 ≤ Li ≤ 50,000)。随后,他购买了一个足够长的长坂,足够分成N块木板(即,它的长度是Li之和)。 FJ忽略了“缝”,不小心锯出来的锯末,你应该记得忽略它们。。。
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.
FJ伤心地发现他没锯子,所以他带着长木板跑到了Don的农场借一个锯子。
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.
Don农民,资本主义,不借给FJ锯子,而是提供锯木板服务。木头切多长,就收多少钱。切割一块木板的长度21,收21美分。
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.
农民Don让农民John决定切割木板的顺序和位置。帮助农民约翰以最便宜的方式锯出来N块木板。FJ知道他以不同的顺序切割木板将导致不同的花费,既然中间的木板的长度不同。。。
(读完之后不知道在说什么。。。蛋疼。。。具体还是看hint能看懂)
Input
Lines 2.. N+1: Each line contains a single integer describing the length of a needed plank
Output
Sample Input
3 8 5 8
Sample Output
34
Hint
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).
Source
#include <iostream>
#include <stdio.h>
#define PARENT(i) (i >> 1)
#define LEFT(i) (i << 1)
#define RIGHT(i) (i << 1) + 1
#define MAX_INT
void MIN_HEAPIFY(long long a[22222], int i);
void BUILD_MIN_HEAP(long long a[22222]);
long long HEAP_EXTRACT_MIN(long long a[22222]);
void MIN_HEAP_INSERT(long long a[22222], long long key);
int heap_size;
int n;
int main(){
int i;
long long a[22222];
long long sum;
long long result;
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
while(scanf("%d", &n) != EOF){
for (i = 1; i <= n;i ++){
scanf("%lld", &a[i]);
}
BUILD_MIN_HEAP(a);
result = 0;
while(heap_size > 1){
sum = 0;
sum = HEAP_EXTRACT_MIN(a) + HEAP_EXTRACT_MIN(a);
//printf("heap_size = %d, sum = %d \n", heap_size, sum);
MIN_HEAP_INSERT(a, sum);
result += sum;
}
printf("%lld", result);
}
return 0;
}
void MIN_HEAPIFY(long long a[22222], int i){
int l = LEFT(i);
int r = RIGHT(i);
long long smallest, temp;
if (l <= heap_size && a[l] < a[i]){
smallest = l;
}else{
smallest = i;
}
if (r <= heap_size && a[r] < a[smallest]){
smallest = r;
}
if (smallest != i){
temp = a[i]; a[i] = a[smallest]; a[smallest] = temp;
MIN_HEAPIFY(a, smallest);
}
}
void BUILD_MIN_HEAP(long long a[22222]){
int i;
heap_size = n;
for (i = (heap_size >> 1);i >= 1; i --){
MIN_HEAPIFY(a, i);
}
}
long long HEAP_EXTRACT_MIN(long long a[22222]){
long long minnum = a[1];
long long temp;
temp = a[1]; a[1] = a[heap_size]; a[heap_size] = temp;
heap_size--;
MIN_HEAPIFY(a, 1);
return minnum;
}
void MIN_HEAP_INSERT(long long a[22222], long long key){
long long i, temp;
heap_size++;
a[heap_size] = key;
i = heap_size;
while(i > 1 && a[i] < a[PARENT(i)]){
temp = a[i]; a[i] = a[PARENT(i)]; a[PARENT(i)] = temp;
i = PARENT(i);
}
}