首页 > 其他分享 >POJ--3253 Fence Repair(贪心/优先队列)

POJ--3253 Fence Repair(贪心/优先队列)

时间:2023-01-26 00:55:20浏览次数:62  
标签:Repair cut 21 Fence -- length que mii1 mii2

记录
23:57 2023-1-25

http://poj.org/problem?id=3253

reference:《挑战程序设计竞赛(第2版)》2.2.4 p47

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

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).

贪心算法,不断的寻找当前最短的和次短的木板进行合成,将合成的木板加入继续合成直到得到一块木板。找最短和次短的木板可以用最小(大)堆(c++中的优先队列)。
找资料说贪心+优先队列 = 哈夫曼 。。。哈哈哈,数据结构毕设后哈夫曼全忘完了。。。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define MAX_N 100000
typedef long long ll;

int N, L[MAX_N];

void solve() {
    ll ans = 0;

    while (N > 1) {
        // get the shortest and the second shortest
        int mii1 = 0, mii2 = 1;
        if(L[mii1] > L[mii2]) swap(mii1, mii2);

        for(int i = 2; i < N; i++) {
            if(L[i] < L[mii1]) {
                mii2 = mii1;
                mii1 = i;
            } else if (L[i] < L[mii2]) {
                mii2 = i;
            }
        }

        // merge the two
        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("%d\n", ans);
}

//使用优先队列
void solve1() {
    ll ans = 0;

    priority_queue<int, vector<int>, greater<int> > que;

    for(int i = 0; i < N; i++) {
        que.push(L[i]);
    }

    while (que.size() > 1) {
        int l1, l2;
        l1 = que.top();
        que.pop();
        l2 = que.top();
        que.pop();

        ans += l1 + l2;
        que.push(l1 + l2);
    }
    std::cout << ans << std::endl;
}

int main() {
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
        scanf("%d", &L[i]);
    }
    solve1();
}

标签:Repair,cut,21,Fence,--,length,que,mii1,mii2
From: https://www.cnblogs.com/57one/p/17067501.html

相关文章

  • 萝卜 间章2.1
    1傻子的眼珠子混浊得像是注了泥。他削瘦得可怕,踉踉跄跄接近黑脸刚离开的垃圾桶。黑脸转头看他,傻子局促地避开目光,将身影躲进垃圾桶的影子里。黑脸的表情没有变化,一如之前......
  • Day02 - MySQL的条件查询
    1.聚合函数聚合函数的介绍聚合函数又叫组函数,通常是对表中的数据进行统计和计算,一般结合分组(groupby)来使用,用于统计和计算分组数据。常用的聚合函数:1.count(co......
  • 单词溯源
    记录单词学习相关知识介词相关单词古义古例所来例句about(aboutisonbyout)在外围接触的,围绕着的(onbutan)(onbyout)由古义引申的:关于:Thebookisabo......
  • 浙大“python->机器语言“的学习二(循环计算)
    辗转相除Euclid设a,b为两个自然数,欲求a,b的最大公约数若a%b为0,则b就是a,b的最大公约数,计算结束否则令a为b,而b为原来的a%b,重复步骤2a,b=map(int,input().spli......
  • KlipperPad 安装精简优化版 Windows10 教程(完美驱动)
    前言本文针对思兼的KlipperPad,介绍如何安装Windows10精简优化版操作系统。一、使用品铂原版系统操作系统链接:http://pipo.cn/index.php?m=About&a=gujian_show&id=......
  • vue学习之----- 图片懒加载插件【vue-lazyload】
    1、用npm安装npmivue-lazyload2、main.js中绑定到vue对象上 3、在需要懒加载的img标签上把src换成v-lazy 4、懒加载的意义:(1)显示在屏幕之外的图片不加载,图片......
  • An-Introduction-to-English-Morphology
    什么是word?一种是因为其含义无法预测而必须被写进词典的word,另一种是组成phrase和sentence的基本元素之外的其他基本元素。真实情况远比这种分类复杂meaningunitsor......
  • 56python文字转语音
    首先安装依赖库pyttsx3pipinstallpyttsx3再来看具体的实例importpyttsx3engine=pyttsx3.init()engine.say("Helloworld!")engine.runAndWait()执行上述脚......
  • POJ--2431 Expedition(优先队列)
    记录0:172023-1-26http://poj.org/problem?id=2431reference:《挑战程序设计竞赛(第2版)》2.2.4p77DescriptionAgroupofcowsgrabbedatruckandventuredona......
  • 百度地图提示JS API版本过低
    当网站遇到调用百度地图页面时,提示“JSAPI版本过低”jsapi版本过低解决方法:在https://lbsyun.baidu.com/注册用户,在应用管理,创建应用应用类型:浏览器端(推荐全......