CSU-ACM2019寒假训练1-F - 经典经典题

这道题和之前B题的找最少钞票有相似之处,和洛谷的合并果子也是一类的,运用了逆向思维以及STL中的优先队列,要好好学习一下 o( ̄┰ ̄*)ゞ

题目:
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).

日常翻译:(o゚v゚)ノ
农场主约翰想修理牧场周围的一小段篱笆。他措施篱笆和发现,他需要N (1≤N≤20000)木板木材,每个有一些整数长度李(李1≤≤50000)单位。然后他买了一块长木板,刚好能锯进N块木板。,其长度为长度Li的和)。福建是忽略了“kerf”,额外的长度损失的锯屑时,锯切;你也应该忽略它。

FJ很遗憾地意识到他自己没有锯木头的锯,所以他就去农场的农场,用这个长板子,礼貌地问他是否可以借一只锯子。

农场主唐,一个不为人知的资本家,不借钱给FJ a锯,而是提出要向农民约翰的每N-1削减木板。切一块木头的费用正好等于它的长度。切割长度为21的木板需要21美分。

农夫唐让农夫约翰决定切木板的顺序和位置。帮助农民约翰确定他可以用来制作N块木板的最小金额。福建知道,他可以削减董事会在不同的订单,这将导致不同的收费,因为由此产生的中间板是不同的长度。

输入
第一行:一个整数N,木板的个数
第2 . .N+1行:每行包含一个整数,描述所需木板的长度

输出
第一行:一个整数:进行N-1次切割所必须花费的最小金额

样例输入
3.
8
5
8

样例输出
34

提示
他想把一块长21的木板切成8、5和8段。原来的板是8+5+8=21。
第一次切割的成本是21美元,应该用来将木板切割成13和8的小块。第二次切割成本为13,应该用来切割成8和5。这将花费21+13=34。如果把21个削减为16个和5个,那么第二次削减的成本是16个,总共37个(比34个多)。

思路:若一开始就直接做的话,那就肯定要将木板每次分成相差最小的两块木板,因为这样留下来的部分越少,花费越少,但是这样每次每块木板都找两部分很麻烦,这是从反面入手,最后木板被分成N份,那么这N份合并起来的花费最少即木板分成N份的花费最少,所以只要合并最少,那么每次合并最短的两块即可,即每次合并后插入,在进行判断最小的两块,这里用优先队列很方便的解决这个问题~

代码如下:

#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int n,l;
long long ans=0;
int main()
{
	cin>>n;
	priority_queue<int,vector<int>,greater<int>>ff;//按照从小到大自动排序,并且其函数可以动态改变数组大小
	for(int i=0;i<n;i++)
	{
		cin>>l;
		ff.push(l);//插入队列,并自动排序
    }
    while(ff.size()>1)
    {
    	int l1,l2;
    	l1=ff.top();   //得到最小的一个木板
    	ff.pop();      //删除最小木板
    	l2=ff.top();   //得到第二个木板
    	ff.pop();     //再次删除
    	ans+=l1+l2;
    	ff.push(l1+l2);//将其放入队列,自动排序
    }
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值