zoukankan      html  css  js  c++  java
  • uva-10954-贪心

    题意:俩个数相加,产生的和就是这次加法的代价,问,所有数都加起来,最小代价是多少

    解题思路:贪心,每次都选取最小俩个数相加,如果只有一个数,计算完毕,注意,加法的和要再次入队列。

    #include <string>
    #include<iostream>
    #include<map>
    #include<memory.h>
    #include<vector>
    #include<algorithm>
    #include<queue>
    #include<vector>
    
    
    namespace cc
    {
        using std::cout;
        using std::endl;
        using std::cin;
        using std::map;
        using std::vector;
        using std::string;
        using std::sort;
        using std::priority_queue;
        using std::greater;
        using std::vector;
    
        constexpr int N = 5000;
    
        priority_queue<int,vector<int>, greater<int> >q;
        void read(int n)
        {
            while (q.empty() == false)
                q.pop();
            int k = 0;
            while (n--)
            {
                cin >> k;
                q.push(k);
            }
        }
    
        void solve()
        {
    
            int n;
            while (cin >> n && n)
            {
                read(n);
                int cost = 0;
                
                while (q.empty() == false)
                {
                    int sum = 0;
                    int i = q.top();
                    q.pop();
                    if (q.empty() == true)
                        break;
                    int k = q.top();
                    q.pop();
                    sum = i + k;
                    cost =cost+sum;
                    q.push(sum);
                }
                cout << cost << endl;
            }
        }
    
    };
    
    
    int main()
    {
    
    #ifndef ONLINE_JUDGE
        freopen("d://1.text", "r", stdin);
    #endif // !ONLINE_JUDGE
        cc::solve();
    
        return 0;
    }
  • 相关阅读:
    Java: 数据类型
    数据结构是什么
    数据结构:进制转换
    数据结构:堆与栈
    class的写法
    Java:异常体系
    数据结构: 先进后出——堆栈
    tomcat:web容器
    Windows: Dos命令
    面向函数范式编程
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/9941001.html
Copyright © 2011-2022 走看看