平衡堆+贪心 hdu1588 合并果子
2015-12-03 19:15
337 查看
传送门:点击打开链接
题意:贪心任意石子合并
思路:平衡堆的模板和代码测试
题意:贪心任意石子合并
思路:平衡堆的模板和代码测试
#include<map> #include<set> #include<cmath> #include<ctime> #include<stack> #include<queue> #include<cstdio> #include<cctype> #include<string> #include<vector> #include<cstring> #include<iostream> #include<algorithm> #include<functional> #define fuck(x) cout<<"["<<x<<"]" #define FIN freopen("input.txt","r",stdin) #define FOUT freopen("output.txt","w+",stdout) using namespace std; typedef long long LL; typedef pair<int, int>PII; const int MX = 1e3 + 5; int rear; LL S[MX << 2]; void push(LL x) { int now = ++rear, pre = now >> 1; S[now] = x; while(pre && S[pre] > S[now]) { swap(S[now], S[pre]); now = pre, pre = now >> 1; } } void pop() { S[1] = S[rear--]; int now = 1; while((now << 1) <= rear) { int lt = now << 1, rt = now << 1 | 1; if(rt <= rear) { if(S[lt] >= S[now] && S[rt] >= S[now]) break; if(S[now] >= S[lt] && S[rt] >= S[lt]) swap(S[now], S[lt]), now = lt; else swap(S[now], S[rt]), now = rt; } else { if(S[lt] > S[now]) break; swap(S[now], S[lt]), now = lt; } } } int main() { int T, n; //FIN; scanf("%d", &T); while(T--) { rear = 0; scanf("%d", &n); for(int i = 1; i <= n; i++) { int t; scanf("%d", &t); push(t); } LL ans = 0, sum; for(int i = 1; i <= n - 1; i++) { sum = S[1]; pop(); sum += S[1]; pop(); ans += sum; push(sum); } printf("%lld\n", ans); } return 0; }
相关文章推荐
- 有限自动机
- jackson.ObjectMapper在序列化Timestamp类型的值时比实际时间少8个小时
- jackson.ObjectMapper在序列化Timestamp类型的值时比实际时间少8个小时
- 使用py2exe生成独立的exe文件
- 1203正规式转换为有穷自动机
- nginx配置:登录使用https
- 深入CSS,让网页开发少点“坑”
- The beginning iOS8 Programming with Swift 中文翻译 - 3
- 《代码阅读方法与实践》阅读笔记一
- solrcoloud
- 理解GDT全局描述符的作用
- iOS_UITabBarController 标签视图控制器
- 12月3号条件控制语句和循环语句
- flexPaper +swftools实现文档在线阅读
- Memcached概念、作用、运行原理、特性、不足简单梳理(1)
- Struts2接收参数的三种方式
- TestNG Eclipse Plugin Installation - Offline
- 我的2015最后一个挣扎月~~~~
- 随笔(2015.11)
- nodejs杂谈二:jshint