poj 3253 fence repair

来源:互联网 发布:php里面怎么写html 编辑:程序博客网 时间:2024/05/16 02:01

      FJ需要修补牧场的围栏,他需要 N 块长度为 Li 的木头(N planks of woods)。开始时,FJ只有一块无限长的木板,因此他需要把无限长的木板锯成 N 块长度为 Li 的木板,<span style="margin:0px; padding:0px; widows:2; orphans:2">Farmer Don提供FJ锯子,但必须要收费的,收费的标准是对应每次据出木块的长度,比如说测试数据中 5 8 8,一开始,FJ需要在无限长的木板上锯下长度 21 的木板(5+8+8=21),第二次锯下长度为 5 的木板,第三次锯下长度为 8 的木板,至此就可以将长度分别为 5 8 8 的木板找出。求锯完木板的最小花费。
>#include<iostream>#include<vector>#include<queue>using namespace std;struct cmp{bool operator()(const long long a,const long long b)const//后面的const必须加{return a>b;}};int main(void){int n;while(cin>>n){priority_queue<long long,vector<long long>,cmp>Queue;for(int i=1;i<=n;i++){long long temp;scanf("%I64d",&temp);Queue.push(temp);}long long mincost=0;while(Queue.size()>1){long long a=Queue.top();Queue.pop();long long b=Queue.top();Queue.pop();Queue.push(a+b);mincost+=a+b;}printf("%ld\n",mincost);while(!Queue.empty())Queue.pop();}return 0;}


0 0
原创粉丝点击