Fence Repair

Time Limit: 2000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem 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

Source

这题 结果不用long long  会wa

#include <bits/stdc++.h>
using namespace std;
int main()
{ios::sync_with_stdio(false);int n, k;priority_queue<int, vector<int>, greater<int>> q; // 升序的优先队列cin >> n;for (int i = 0; i < n; i++){cin >> k;q.push(k);}long long ans = 0; // 不用 long long 过不了while (!q.empty()){int a = q.top();q.pop();if (!q.empty()){int b = q.top();q.pop();ans += (a + b);q.push(a + b);}}cout << ans << endl;return 0;
}/***************************************************
User name: jk180602
Result: Accepted
Take time: 8ms
Take Memory: 364KB
Submit time: 2019-01-11 20:09:23
****************************************************/

转载于:https://www.cnblogs.com/iQXQZX/p/10258724.html

SDUT 2423 Fence Repair(优先队列)相关推荐

  1. Fence Repair(优先队列)

    Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  2. Fence Repair (二叉树求解)(优先队列,先取出小的)

    题目链接:http://poj.org/problem?id=3253 Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  3. Fence Repair(不会优先队列的看过来)

    Fence Repair(不会优先队列的看过来) Farmer John wants to repair a small length of the fence around the pasture. ...

  4. POJ3253 Fence Repair【哈夫曼树+优先队列】

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 48026   Accepted: 15784 De ...

  5. POJ 3253 -- Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 55661   Accepted: 18331 De ...

  6. bzoj1724[Usaco2006 Nov]Fence Repair 切割木板*

    bzoj1724[Usaco2006 Nov]Fence Repair 切割木板 题意: FJ需要n块木板,第i块木板长度为ai.但他只有一块长度为sigma(i,1,n)ai的木板.每切一次的代价为 ...

  7. bzoj1724【Usaco2006 Nov】Fence Repair 切割木板

    1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 920  Solved: 4 ...

  8. 贪心:Fence Repair、Saruman's Army

    Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  9. [BZOJ1724][Usaco2006 Nov]Fence Repair 切割木板

    1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1272  Solved: ...

最新文章

  1. 数人云轻量级PaaS将弹性做到极致
  2. Autoit 在word中绘图
  3. ORA-01658: 无法为表空间space中的段创建 INITIAL
  4. wxWidgets:窗口样式
  5. 中继(洪泛中继、定向中继)在无线通讯中的应用
  6. 分享大牛开发经验,浅谈java程序员职业规划
  7. 使用OpenCV-python实现颜色特征跟踪视频中的物体
  8. Android:Touch和Click的区别
  9. 理解python - 类
  10. 小米回应“海量备货致亏损”;美团饿了么“偷听”?苹果发布新 iPad | 极客头条...
  11. Oracle 存储方式大比拼
  12. Yahoo!团队实践分享:网站性能优化的34条黄金守则(二)
  13. 华为交换机 查ip冲突_华为交换机:如何解决网络中IP地址发生冲突故障?
  14. python文件和数据格式化
  15. 基于Android的BMI指数运算
  16. 高级程序员的思维模式
  17. Android动画之 Alpha与Translate结合使用
  18. 12306网站 =≈=泄露用户信息解读之撞库
  19. 为什么这么多学生讨厌数学(以及如何解决它)?
  20. 使用pyecharts绘制各个3D图表代码示例整理和分享

热门文章

  1. 怎样用C语言去实现一个日程管理系统
  2. B3402 [Usaco2009 Open]Hide and Seek 捉迷藏 最短路
  3. matlab lmtool,借用许可证 - MATLAB Simulink - MathWorks 中国
  4. ui标注生成html,自动标注一键生成,手动标注自由补充
  5. PHP+SQLite3简约网址导航、书签管理器网站源码/百素导航系统
  6. GPS卫星主要观测量
  7. Unity移动---使用WASD控制物体朝摄像机方向移动和旋转,并且自动贴合斜坡角度
  8. Word数据对比左右并排看
  9. 使用百度云AI实现刷脸登录
  10. 应用软件的日志系统的设计要求