POJ3253 Fence Repair(贪心)
分割木板的顺序是自由的,所以每次选择两块最短的板,组合在一起,增加队列,原来两个板出队,直到队列中为空或者仅仅剩下一个板时结束。这里使用优先队列较为方便。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define ll __int64
using namespace std; int len[20005]; int main()
{
//freopen("d:\\test.txt","r",stdin);
int n;
ll ans=0;
cin>>n;
priority_queue<int,vector<int>,greater<int> >q;//最小元素在队头
for(int i=0;i<n;i++)
{
cin>>len[i];
q.push(len[i]);
}
while(!q.empty())
{
int t1=q.top();
q.pop();
if(!q.empty())
{
int t2=q.top();
q.pop();
ans+=t1+t2;
q.push(t1+t2);
}
else break;
}
printf("%I64d\n",ans);
return 0;
}
POJ3253 Fence Repair(贪心)的更多相关文章
- POJ 3253 Fence Repair 贪心 优先级队列
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 77001 Accepted: 25185 De ...
- POJ 3253 Fence Repair (贪心)
Fence Repair Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- 优先队列 poj3253 Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 51411 Accepted: 16879 De ...
- Poj3253 Fence Repair (优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 67319 Accepted: 22142 De ...
- poj-3253 fence repair(贪心题)
题目描述: Farmer John wants to repair a small length of the fence around the pasture. He measures the fe ...
- poj3253 Fence Repair(贪心+哈夫曼 经典)
https://vjudge.net/problem/POJ-3253 很经典的题,运用哈夫曼思想,想想很有道理!! 具体实现还是有点绕人,最后被long long卡了一下,看数据大小的时候单纯相乘了 ...
- POJ3253 Fence Repair【贪心】
我们的小伙伴Bingo真的很调皮,他在上课的路上看到树上有个鸟窝,他就想去把他捅下来,但是鸟窝很高他够不到,于是他就到处找木棍,想把这些木棍接在一起,然后去捅鸟窝.他一共找了N跟木棍 (1 ≤ N ≤ ...
- poj3253 Fence Repair
http://poj.org/problem?id=3253 Farmer John wants to repair a small length of the fence around the pa ...
- poj3253 Fence Repair【哈夫曼树+优先队列】
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
随机推荐
- QT:程序忙碌时的进度条——开启时间循环,等结束的时候再退出
当程序在执行一项(或多项)耗时比较久的操作时,界面总要有一点东西告诉用户“程序还在运行中”,那么,一个“没有终点”的进度条就是你需要的了.PS:最好把耗时的操作扔到一个子线程中去,以免他阻塞了界面线程 ...
- cdecl、pascal、stdcall、fastcall
Directive Parameter order Clean-up Passes parameters in registers?register Left-to-right ...
- unity3D 锁屏再开程序崩溃
在Uniyt3d 调用Android Plugin 的时候,会出现锁屏后再开,程序就崩溃的的现象,解决办法就是在 AndroidManifest.xml 加入 android:configChang ...
- HDU1879 继续畅通工程 (并查集)
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- leetcode Permutation
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 杭电 HDU 4608 I-number
http://acm.hdu.edu.cn/showproblem.php?pid=4608 听说这个题是比赛的签到题......无语..... 问题:给你一个数x,求比它大的数y. y的要求: 1. ...
- Endian.BIG_ENDIAN和Endian.LITTLE_ENDIAN(http://smartblack.iteye.com/blog/1129097)
Endian.BIG_ENDIAN和Endian.LITTLE_ENDIAN 在ByteArray和Socket中,能看到一个属性endain. endian : String 更改或读取数据的字节顺 ...
- MVC3.0修改jquery.validate.unobtrusive.js实现气泡提示mvc错误
CSS部分 <style type="text/css"> .hide {display:none;} .poptip { position: absolute; to ...
- HDU 4081 MST
这道题在LRJ的书上看到,今天回过头来继续看这题,发现很多东西都已经明白了. 题意:有N个城市,每个城市有一个坐标和人口. 现在要建一些边使得他们都联通,花费就是这些边的长度,然后有一条边可以免费.问 ...
- DevExpress Util HelpV3
using System; using System.Drawing; using DevExpress.XtraCharts; namespace DevExpressUtilHelpV3 { pu ...