UVA11136 Hoax or what STL 或者 HEAP
来源:互联网 发布:2017网络效应判断题 编辑:程序博客网 时间:2024/06/05 19:26
这道题题意不解释,《算法竞赛入门经典训练指南》里都有翻译,不知道的去看看吧。
本来博主写的堆:
/************************************************************************* > File Name: \LJF\UVa\UVa_11136.cpp > Author: ljf_cnyali > Mail: 2724424647@qq.com > Last modifiedz: 2016-08-27 14:59 > Description: This is a large group of God's program information. ************************************************************************/#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<ctime>#include<algorithm>#include<queue>#include<map>#include<set>#include<ctime>using namespace std;#define REP(i, a, b) for(long long i = (a), _end_ = (b);i <= _end_; ++i)const long long maxn = 10010000;long long max_heap[maxn], min_heap[maxn];long long max_len, min_len;void min_insert(long long x) { min_heap[++min_len] = x; long long k = min_len, t; while(k > 1 && min_heap[k] < min_heap[k / 2]) { t = min_heap[k]; min_heap[k] = min_heap[k / 2]; min_heap[k / 2] = t; k /= 2; } } void max_insert(long long x) { max_heap[++max_len] = x; long long k = max_len, t; while(k > 1 && max_heap[k] > max_heap[k / 2]) { t = max_heap[k]; max_heap[k] = max_heap[k / 2]; max_heap[k / 2] = t; k /= 2; }}int main() { long long n, t, m, f, k; while(1) { scanf("%lld", &f); memset(max_heap, 0, sizeof(max_heap)); memset(min_heap, 0, sizeof(min_heap)); max_len = 0; min_len = 0; if(f == 0) return 0; long long ans = 0; REP(l, 1, f) { scanf("%lld", &n); REP(i, 1, n){ scanf("%lld", &k); max_insert(k); min_insert(k); } ans += max_heap[1] - min_heap[1]; min_heap[1] = min_heap[min_len--]; k = 1; while((2 * k <= min_len && min_heap[k] > min_heap[2 * k]) || (2 * k <= min_len && min_heap[k] > min_heap[2 * k + 1])) { m = 2 * k; if(m + 1 <= min_len && min_heap[m] > min_heap[m + 1]) m++; t = min_heap[k]; min_heap[k] = min_heap[m]; min_heap[m] = t; k = m; } max_heap[1] = max_heap[max_len--]; k = 1; while((2 * k <= max_len && max_heap[k] < max_heap[2 * k]) || (2 * k + 1 <= max_len && max_heap[k] < max_heap[2 * k + 1])) { m = 2 * k; if(m + 1 <= max_len && max_heap[m] < max_heap[m + 1]) m++; t = max_heap[k]; max_heap[k] = max_heap[m]; max_heap[m] = t; k = m; } } printf("%lld\n", ans); } return 0; }
WA了好久,都是Wrong answer,自己都觉得烦了。本来想写堆练下手,最后还是被迫选择了STL。
STL的代码在此:
/************************************************************************* > File Name: \LJF\UVa\UVa_11136.cpp > Author: ljf_cnyali > Mail: 2724424647@qq.com > Last modifiedz: 2016-08-27 14:59 > Description: This is a large group of God's program information. ************************************************************************/#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<ctime>#include<algorithm>#include<queue>#include<map>#include<set>#include<ctime>using namespace std;#define REP(i, a, b) for(long long i = (a), _end_ = (b);i <= _end_; ++i)multiset<long long> q;int main() { long long n, t, m, f, k; while(1) { q.clear(); scanf("%lld", &f); if(f == 0) return 0; long long ans = 0; REP(l, 1, f) { scanf("%lld", &n); REP(i, 1, n){ scanf("%lld", &k); q.insert(k); } auto s = q.begin(), e = q.end(); --e; ans += *e - *s; q.erase(e); q.erase(q.begin()); } printf("%lld\n", ans); } return 0; }
0 0
- UVA11136 Hoax or what STL 或者 HEAP
- UVA11136 Hoax or what STL 或者 HEAP
- UVa11136 - Hoax or what
- UVA11136 Hoax or what
- uva 11136 - Hoax or what(STL)
- Uva-11136-Hoax or what
- uva 11136 Hoax or what
- UVaOJ 11136 Hoax or what
- uva 11136 Hoax or what
- UVA - 11136 Hoax or what
- UVa:11136 Hoax or what
- UVA 11136 Hoax or what
- UVa 11136 - Hoax or what
- UVA - 11136 Hoax or what
- UVa 11136 - Hoax or what
- UVA - 11136 Hoax or what
- uva 11136 Hoax or what
- uva Hoax or what 11136(multiset)
- Java中日期时间
- [LeetCode]Power of Four/Power of Three/Power of Two(Java)
- mysql 索引和orderby
- java快速排序
- web开发中遇到的导致网页开头空白
- UVA11136 Hoax or what STL 或者 HEAP
- 强势回归!poj 1950
- win10系统登陆提示“你的设备处于脱机状态..“解决方案
- 退役
- Xfermode基本使用
- QLibrary 动态加载外部库文件
- java身份证识别
- Android基础类之BaseAdapter
- android 出现 cannot resolve symbol r