zoukankan      html  css  js  c++  java
  • swun 1612 合并果子

     
    //思路:这题思路似乎很简单,每次取出最小的两个堆合并,
    //但是由于数据太大,不能采取每次进行排序的方式,所以
    //想到用优先队列,以数据小的优先级更高为标准,但是
    //优先队列中的数据默认情况下是由大到小弹出的,所以多一步
    //操作,具体如下:
    #include<cstdio>
    #include<queue>
    using namespace std;
    #define LL long long
    
    struct node{
        long long  x;
        friend bool operator < (node a, node b){
            return a.x < b.x;    //默认情况下从大到下弹出。
        }
    };
    
    priority_queue<LL,vector<LL>,greater<LL> > q;    //从小到大弹出。
        //你现在觉得太复杂的话,记住就行了。有兴趣自己查资料。对了,
        //注意最后两个>之间是有个空格的,不然会编译错误。
    
    int  main()
    {
        int n;
        long long x, sum, tmp;
        while(~scanf("%d", &n))
        {
            while(!q.empty()) q.pop();    //一般加上这步比较保险,虽然这题不加也是对的。
            while(n--)
            {
                scanf("%I64d", &x);
                q.push(x);
            }
            if(n == 1)
            {
                printf("%I64d", q.top()); //如果只有一个数,直接弹出,输出即可。
                continue;
            }
            sum = 0;
            while(1)
            {
                tmp = 0;
                tmp += q.top(), q.pop(); //弹出最小的两个。
                tmp += q.top(), q.pop();
                sum += tmp;
                if(q.empty()) break;     //如果是空队列,则跳出。
                q.push(tmp); //将相加之后的结果压入队列中。
            }
            printf("%I64d
    ", sum);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    css3 动画
    jQuery toast 淡入淡出提示
    JavaScript事件——拖拉事件
    Vue -- element-ui 限制只能输入number
    css 移动端页面,在ios中,margin-bottom 没有生效
    django 快速搭建blog
    python 正则表达式口诀
    [转]python os模块 常用命令
    【转】scapy 构造以太网注入帧
    【转】关于Scapy
  • 原文地址:https://www.cnblogs.com/loveprincess/p/4795956.html
Copyright © 2011-2022 走看看