10954 - Add All
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1895
Yup!! The problem name reflects your task; just add a set of numbers. But you may feel yourselves condescended, to write a C/C++ program just to add a set of numbers. Such a problem will simply question your erudition. So, let’s add some flavor of ingenuity to it.
Addition operation requires cost now, and the cost is the summation of those two to be added. So, to add 1 and 10, you need a cost of 11. If you want to add 1, 2 and 3. There are several ways –
纯练手。
完整代码:
/*0.035s*/ #include<cstdio> #include<queue> #include<vector> #include<functional> using namespace std; typedef long long ll; priority_queue<ll, vector<ll>, greater<ll> > q; int main() { int n; ll x, sum; while (scanf("%d", &n), n) { while (!q.empty()) q.pop(); while (n--) { scanf("%lld", &x); q.push(x); } sum = 0; while (true) { x = q.top(); q.pop(); x += q.top(); q.pop(); sum += x; if (!q.empty()) q.push(x); else break; } printf("%lld\n", sum); } return 0; }
作者署名:csdn博客 synapse7
查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索include
, while
, sum
, add
, to
, Sum Problem
, problem
, priority_queue
, priority_queue详解
, priority_queue实例
, 哈弗曼算法
哈弗曼编码
uva 10954、ea109540592nl、,以便于您获取更多的相关知识。