POj 3253 Fence Repair

深入理解数据库原理
以实战为基础,以数据库原理为基础,结合实战,深入理解数据库原理。
wangxiaoming


Fence Repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 30086   Accepted: 9704

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

Sample Input

3
8
5
8

Sample Output

34




代码:

#include<iostream>
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;

int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        int ans = 0;
        multiset<int> S;  //特点是可以自动排序
        for(int i = 0; i < n ; i++)
        {//建立一个二叉树集合最后再进行计算
            int x ;
            scanf("%d", &x);
            S.insert(x);
        }
        while(S.size() >1)
        {
            //下面定义一个迭代器
            multiset<int>::iterator it = S.begin();
            int temp = *it; //用来存第一个数据
            S.erase(it);    //删除
            it = S.begin();  //取出第二个数据
            temp += *it;
            S.erase(it);   //将删掉第二个数据
            ans += temp;
            S.insert(temp); //将他们的和加入
        }
        printf("%d\n", ans);
    }
    return 0;
}

其次在看一段代码:

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const int MAX_N = 10000;
int  N, L[MAX_N];
void swap(int a, int b)
{
    int temp ;
    temp = a;
    a = b;
    b = temp;
}
void solve()
{
    ll ans = 0;
    while(N > 1)
    {
        int mii1 = 0;
        int mii2 = 1;
        if(L[mii1] > L[mii2]) swap(mii1, mii2); //先找到两个之中的最小的情况
        //下面从第二个开始进行排序
        for(int i = 2; i < N ; i++)
        {// 这个循环的目的是
              if(L[i] < L[mii1])
              {//mii1 是其中较小的
                  mii2 = mii1;
                  mii1 = i;
              }
              else if (L[i] <L[mii2])
              {
                  mii2 = i;
              }
        }
        int t = L[mii1] + L[mii2];
        ans += t ;
        if(mii1 == N -1) swap(mii1,mii2);
        L[mii1] = t;
        L[mii2] = L[N-1];
        N--;
    }
    printf("%lld\n", ans);
}
int main()
{// 题目大意是有一块很成啊的模板一起切成N块
    // 准备切成的木块的长度为Li L2,, 问 最小开销
    //所谓的最小开销是有点类似于最小哈夫曼数的构造
    //二叉树的构造 最优解来说 最短的木板应当是深度最大的叶子节点之一
    //思路 不妨将Li按照大小的顺序排序, 那么最短是L1 次短是L2 如果他们的在二叉树
    /*
      中是兄弟节点, 那么可以看成 (L1 + L2) L3 L4 L5....
    */


   return 0;
}


wangxiaoming CSDN认证博客专家 架构 Spring Boot Redis
博客是很好的总结和记录工具,如果有问题,来不及回复,关注微信公众号:程序员开发者社区,获取我的联系方式,向我提问,也可以给我发送邮件,联系 1275801617@qq.com
©️2020 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值