zoukankan      html  css  js  c++  java
  • TYVJ 1066 合并果子【优先队列】

    题意:给出n堆果子,需要将n堆果子合并成一堆,问将所有堆的果子合成一堆所需要花费的最少的力气

    因为要使耗费力气最小,即需要每次搬动的那堆重量小,所以可以选取两堆最轻的合并,合并之后再插入还没有合并的堆中,重复这个过程

     1 #include<iostream>  
     2 #include<cstdio>  
     3 #include<cstring> 
     4 #include <cmath> 
     5 #include<stack>
     6 #include<vector>
     7 #include<map> 
     8 #include<queue> 
     9 #include<algorithm>  
    10 #define mod=1e9+7;
    11 using namespace std;
    12 
    13 typedef long long LL;
    14 
    15 int main(){
    16     priority_queue<int,vector<int>,greater<int> > pq;
    17     int n,a,ans=0;
    18     scanf("%d",&n);
    19     for(int i=1;i<=n;i++){
    20         scanf("%d",&a);
    21         pq.push(a);
    22     }
    23     
    24     while(pq.size()>1){
    25         int x=pq.top();pq.pop();
    26         int y=pq.top();pq.pop();
    27         ans+=x+y;
    28         pq.push(x+y);
    29     }
    30     printf("%d
    ",ans);
    31     return 0;
    32 }
    View Code

    话说手写二叉堆还是木有写出来,还是用的优先队列写的,在RQNOJ上交的,AC100是过了的意思么= =

  • 相关阅读:
    第二篇:服务消费者Feign
    第一篇:服务的注册与发现Eureka(Finchley版本)
    记一次包扫描的犯错
    0.简单工厂-simplefactory(非23之一)
    设计模式基础
    设计模式--六大设计原则
    Java中的包
    Java内部类
    Java多线程
    Java同步
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4355547.html
Copyright © 2011-2022 走看看