poj 3253 Fence Repair

Fence Repair
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

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

Sample Input

3
8
5
8

Sample Output

34

Hint

He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8. 
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).
他想切一块21长度的木板分别为8,5,8,第一次花钱21,切成13和8,第二次花费13,切成8和5,一共消费34.如果21切成16和5,第二次切将一共花费37

Source

 
恩。。。这道题要看hint才能看懂,就是个haffman树的思想啦,先排序挑俩最小的放一块,然后重新塞回去再排序重新挑俩小的放一块。。。
1a很欢快~
这里用最小优先级队列,建个堆,然后每次挑俩最小的,加个和,再插回去,一直到heap_size为0,然后输出。
注意数据大小,因为其中有可能加和为20000*50000不小心超int了,所以就上long long了。就是这样,没什么其他需要在意的地方了。。。
#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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值