POJ 3253 贪心法,优先队列

来源:互联网 发布:苹果vr软件 编辑:程序博客网 时间:2024/05/17 03:06

Fence Repair
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 35249 Accepted: 11392

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

3858

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



遇到的问题和思路:

       首先吧,看到这道题真心不会。因为优先队列的知识没有学过,所以做起来有点吃力。起初看了题解以为只是单纯的排序好放进去就好了,根本就没有想到有优先队列,后来才知道,原来优先队列还是有自动排序的,长知识了,有收获!


给出AC代码:


#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<queue>using namespace std;int a[20005];int t;long long ans;//首先注意queue的()不能忘了,然后通过ans来计数不能忘了。还有一定要先对a数组进行排序 void solve(){priority_queue <int, vector<int>, greater<int> > que;ans = 0; for (int i = 0; i < t; i++)    que.push(a[i]);while (que.size() > 1){int a1 = que.top();que.pop();int a2 = que.top();que.pop();que.push(a1 + a2);ans = ans + a1 + a2;}printf("%I64d\n",ans);}int main(){while(scanf("%d",&t)!=EOF){memset(a, 0, sizeof(a));for(int i = 0; i < t; i++){scanf("%d",&a[i]);}sort(a,a+t);solve();}return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 图虫签约师通过了认证标识怎么办 签证做假工作证明资料被拒怎么办 在广州办个建设厅电工证怎么办 水利考的五大员证到有效期怎么办 额头注射玻尿酸吸收后不平怎么办 施工员证书挂靠拿不回来怎么办 森林公安未转政法编制的人员怎么办 北京的限行新政策外地车怎么办 报考二级建造师工作年限不够怎么办 郑州航院图书馆密码忘了怎么办 无锡妇幼预约挂号过号了怎么办 云南建筑八大员考试没通过怎么办 订车ax7一个月提不到车怎么办 提车时间到了却没车怎么办 一汽大众速腾气囊灯亮该怎么办呢? 幼儿园上课时候电脑上的课件怎么办 黑米紫薯红豆粥不好煮怎么办 母狗生了小狗后不吃东西没奶怎么办 狗给扑倒了主人不想负责怎么办 山东政务网个人中心账号忘了怎么办 网易博客忘记登入名和密码了怎么办 奶水不够怎么办怎样让奶水变多 和初恋分手多年又爱上初恋该怎么办 分手六年的初恋想要和我复合怎么办 不小心把手机里的视频删了怎么办 网签过了中介不配合过户怎么办 房屋被中介恶意网签了怎么办 重介质选矿块矿品位低怎么办 去泰国酒吧选小姐只会说中文怎么办 脚被窝烫伤后发炎里面有龙怎么办 脚被窝烫了发炎有龙怎么办 辣椒落花的药喷到孩子嘴巴了怎么办 香炉里的小米生虫子了怎么办 香炉里面放的小米生虫子了怎么办 肉肉上长满了白色的小虫子怎么办 多肉上面有白色的虫子怎么办 多肉植物根部长白色虫子怎么办 朋友玩期货把我钱赔了怎么办 宝宝床上虫子咬了肿大包怎么办 喷药的杀虫剂的喷头坏了怎么办 宅e经营贷个人没有公司怎么办