fence repair(队列水过)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 32916 | Accepted: 10638 |
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
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
Output
Sample Input
- 3
- 8
- 5
- 8
Sample Output
- 34
Hint
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).
Source
- #include <iostream>
- #include <queue>
- using namespace std;
- int main()
- {
- int n;
- while(cin>>n)
- {
- int i,k,j,t1;
- long long sum=;
- priority_queue<int,vector<int>,greater<int> > p;
- for(i=;i<n;i++)
- {
- cin>>k;
- p.push(k);
- }
- while(p.size()!=)
- {
- t1=p.top();
- p.pop();
- t1+=p.top();
- sum+=t1;
- p.pop();
- p.push(t1);
- }
- cout<<sum<<endl;
- }
- }
fence repair(队列水过)的更多相关文章
- POJ 3253 Fence Repair 贪心 优先级队列
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 77001 Accepted: 25185 De ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- 优先队列 poj3253 Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 51411 Accepted: 16879 De ...
- POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
- Fence Repair (二叉树求解)(优先队列,先取出小的)
题目链接:http://poj.org/problem?id=3253 Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- poj 3253 Fence Repair(priority_queue)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40465 Accepted: 13229 De ...
- POJ 3253:Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 33114 Accepted: 10693 De ...
- poj 3253 Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42979 Accepted: 13999 De ...
- Greedy:Fence Repair(POJ 3252)
Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...
随机推荐
- Spark学习笔记-如何运行wordcount(使用jar包)
IDE:eclipse Spark:spark-1.1.0-bin-hadoop2.4 scala:2.10.4 创建scala工程,编写wordcount程序如下 package com.luoga ...
- 用户子查询,用case
select satisfy.STARTTIME,cc.C_CrmID,cc.C_Name ClientName,be.C_NAME,count(yskj.c_id) PhoneSum,sum(ca ...
- Spring Data JPA之Hello World
Spring Data Jpa 配置 使用 Spring Data JPA 进行持久层开发需要的四个步骤: 1.配置 Spring 整合 JPA 2.在 Spring 配置文件中配置 Spring D ...
- python描述符 descriptor
descriptor 在python中,如果一个新式类定义了__get__, __set__, __delete__方法中的一个或者多个,那么称之为descriptor.descriptor通常用来改 ...
- Qt5+VS2013兼容XP方法
用Qt5+VS2013编译程序默认配置会在XP运行时报"不是有效的Win32程序"工作需要必须要XP运行 pro文件中加一句: 复制代码 QMAKE_LFLAGS_WINDOWS ...
- 【LeetCode练习题】Valid Palindrome
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- 自己写jstl标签解析long时间
数据库里存储的是long型的时间,现在想输出到jsp页面,由于使用的是jstl标签,而要显示的是可读的时间类型,找来找去有个fmt:formatDate可以转化,但是只能转date型,long型则不可 ...
- Sort(归并)
Sort 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 You want to processe a sequence of n distinct integers ...
- UCTF Final-Hackventure
抽出世间将UCTF Final中的hackventure给记录下,算是个总结.题目是有一个游戏,游戏地图是随机生成的,用户可以攻打Server,如果3个Server都被攻占的话,那么用户就赢了,但是并 ...
- ASP.NET中的Request和Respone对象的使用
前台<body>中的表单代码: ASP.NET对象有如下几个: 本文从“asp.net中通过from表单submit提交到后台的实例”来谈谈Request和Response ...