博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uva-10954-贪心
阅读量:6938 次
发布时间:2019-06-27

本文共 1153 字,大约阅读时间需要 3 分钟。

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

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

#include 
#include
#include
#include
#include
#include
#include
#include
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
, greater
>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;}

 

posted on
2018-11-10 23:01 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/shuiyonglewodezzzzz/p/9941001.html

你可能感兴趣的文章
linux剪切拷贝
查看>>
isinstance, type, issubclass
查看>>
[扫雷][游戏] 交互*2
查看>>
Python函数
查看>>
python 开发技巧(4)-- 用PyCharm实用技巧(我自己常用的)
查看>>
Path Sum II
查看>>
[转载]STM32高级定时器(TIM1和TIM8)、通用定时器(TIMx) 、 基本定时器(TIM6和TIM7)的区别...
查看>>
初入前端2
查看>>
python ----列表、字符串、元组之间转换小结
查看>>
python基础之socket编程
查看>>
NYOJ 45( 分治,大数)
查看>>
网络语音技术
查看>>
【酷熊科技】工作积累 ----------- C#自动添加using引用命名空间
查看>>
通达信公式-涨幅限制
查看>>
VMware-Linux(RedHat 6.7)增加数据库使用空间(Linux&Linux LVM)
查看>>
左神算法进阶班3_1构造数组的MaxTree
查看>>
SQL Server中的锁类型及用法(转载)
查看>>
CodeVS 1008 选数(DFS)
查看>>
SQL Server日常积累
查看>>
C++强制类型转换
查看>>