POJ
来源:互联网 发布:恐怖黎明物品数据官网 编辑:程序博客网 时间:2024/06/06 16:37
链接上一篇博文,这个是任意两个木棍拼接的题目
这个题是经典的最优二叉树(百科连接:https://baike.baidu.com/item/%E5%93%88%E5%A4%AB%E6%9B%BC%E6%A0%91/2305769?fr=aladdin&fromid=1792010&fromtitle=%E6%9C%80%E4%BC%98%E4%BA%8C%E5%8F%89%E6%A0%91)问题
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cctype>#include<string>#include<queue>#include<set>#include<map>#include<math.h>#include<string>using namespace std;const int INF = 1000000000;const int maxn = 20000 + 5;int n;int a[maxn];priority_queue<unsigned long long, vector<unsigned long long>, greater<unsigned long long> > qu;void init(){ while(!qu.empty()) qu.pop(); int x; for(int i = 0; i < n; ++i) { scanf("%d", &x); qu.push(x); }}unsigned long long solve(){ unsigned long long sum = 0; while(!qu.empty()) { unsigned long long a = qu.top(); qu.pop(); if(qu.empty()) break; unsigned long long b = qu.top(); qu.pop(); //cout << a <<" " << b << endl; unsigned long long c = a + b; sum += c; qu.push(c); } return sum;}int main(){ scanf("%d", &n); init(); unsigned long long ans = solve(); printf("%lld", ans); return 0;}
阅读全文
1 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- B
- JAVA SE基础总结(可以作为有一定基础的参考)
- 爬虫——使用BeautifulSoup4的爬虫
- ASP.NET MVC入门到精通——搭建项目框架
- 第16章 数组总结
- POJ
- 【Linux基础系列之】gpio系统
- leetcode__Longest Substring Without Repeating Characters
- 扫雷游戏
- 位运算
- 子串查找
- Google Gson的使用方法,实现Json结构的相互转换
- maven项目名称出现红叉但是project下又看不到错误在哪儿
- 常用的几个正则表达式式