Hoax or what
题意是询问一个动态序列的最小值和最大值。
可以用multiset来实现。
#include <stdio.h>
#include <set>
using namespace std; int main() {
freopen("h.in", "r", stdin);
freopen("h.ans", "w", stdout);
int n;
while (scanf("%d", &n) && n) {
multiset<int> bills;
int sum = ;
for (int i = ; i < n; i++) {
int cnt;
scanf("%d", &cnt);
for (int j = ; j < cnt; j++) {
int t;
scanf("%d", &t);
bills.insert(t);
} sum += (*bills.rbegin() - *bills.begin()); bills.erase(bills.begin());
bills.erase(--bills.rbegin().base());
}
printf("%d\n", sum);
}
}
这里给出一个最小堆的做法,通过维护一个最小堆,一个最大堆,当push元素时,将这两个元素连接到一起。当pop最大或者最小的元素时,对应删除另一个堆中对应的元素。
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std; const int MAXN = 1e6 + ;
const int INF = 1e9; int min_heap[MAXN], max_heap[MAXN];
int min_heap_no[MAXN], max_heap_no[MAXN];
int min_heap_no_rev[MAXN], max_heap_no_rev[MAXN]; class MinHeap {
protected:
int count, *heap_;
public:
MinHeap() {}
MinHeap(int *heap) : heap_(heap), count() {}
void push(int x) {
++count;
heap_[count] = x;
up(count);
}
int top() {
return heap_[];
}
void pop() {
my_swap(, count--);
heapfy();
}
/*{{{ del(int i)*/
void del(int i) {
heap_[i] = -INF; up(i);
pop();
}
/*}}}*/
virtual void my_swap(int i, int j) {
swap(heap_[i], heap_[j]);
}
/*{{{ heapfy(int i) */
void heapfy(int i) {
while (i <= count) {
int smallest = i;
if ( * i <= count && heap_[ * i] < heap_[smallest]) {
smallest = * i;
}
if ( * i + <= count && heap_[ * i + ] < heap_[smallest]) {
smallest = * i + ;
}
if (i != smallest) {
my_swap(i, smallest);
i = smallest;
} else {
break;
}
}
}
/*}}}*/
/*{{{ up(int i) */
void up(int i) {
while (i > ) {
if (heap_[i] < heap_[i / ]) {
my_swap(i, i / );
i /= ;
} else {
break;
}
}
}
/*}}}*/
}; class MinHeapWithMap : public MinHeap {
private:
int id, *no_, *no_rev_;
public:
MinHeapWithMap() {}
MinHeapWithMap(int *heap, int *no, int *no_rev) : MinHeap(heap), no_(no), no_rev_(no_rev), id() {}
void push(int x) {
no_[count + ] = id;
no_rev_[no_[count + ]] = count + ;
id++;
MinHeap::push(x);
}
virtual void my_swap(int i, int j) {
MinHeap::my_swap(i, j);
swap(no_[i], no_[j]);
no_rev_[no_[i]] = i;
no_rev_[no_[j]] = j;
}
int top_no() {
return no_[];
}
int no_rev(int i) {
return no_rev_[i];
}
}; class Solution {
private:
MinHeapWithMap minHeap_, maxHeap_;
public:
Solution() {
minHeap_ = MinHeapWithMap(min_heap, min_heap_no, min_heap_no_rev);
maxHeap_ = MinHeapWithMap(max_heap, max_heap_no, max_heap_no_rev);
}
void push(int x) {
minHeap_.push(x);
maxHeap_.push(-x);
}
int getMaxMinDiff() {
int res = -maxHeap_.top() - minHeap_.top();
minHeap_.del(minHeap_.no_rev(maxHeap_.top_no()));
maxHeap_.del(maxHeap_.no_rev(minHeap_.top_no()));
minHeap_.pop();
maxHeap_.pop();
return res;
}
}; int main() {
freopen("h.in", "r", stdin);
freopen("h.out", "w", stdout);
int n;
while ( scanf("%d", &n), n) {
Solution s;
int sum = ;
for (int i = ; i < n; i++) {
int k;
scanf("%d", &k);
for (int j = ; j < k; j++) {
int t;
scanf("%d", &t);
s.push(t);
}
sum += s.getMaxMinDiff();
} printf("%d\n", sum);
}
}
Hoax or what的更多相关文章
- UVA 11136 - Hoax or what (可以提交了,不会Submission error了)
看题传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- set UVA 11136 Hoax or what
题目传送门 题意:训练指南P245 分析:set维护,查询删除最大最小值 #include <bits/stdc++.h> using namespace std; typedef lon ...
- 优化Hoax or what的思考
在抽取MinHeap的时候,涉及到重载,覆盖,虚函数等,有几点之前没注意到的问题在这里总结下: 1. 覆盖(override)只要是函数同名就会被覆盖,子类指针若调用父类的同名不同参数的函数的话,会在 ...
- uva 11136 - Hoax or what
用两个优先队列来实现,因为队列只能从一头出去: 所以维护一个数组,来标记这个队列的已经出列而另外一个队列没有出列的元素: 到时候再把他们删了就行: #include<cstdio> #in ...
- UVA 11136 Hoax or what (multiset)
题目大意: 超时进行促销.把账单放入一个箱子里 每次拿取数额最大的和最小的,给出 最大-最小 的钱. 问n天总共要给出多少钱. 思路分析: multiset 上直接进行模拟 注意要使用long lo ...
- UVa 11136 Hoax or what (STL)
题意:有 n 天,每天有m个数,开始的前一天没有数据,然后每天从这个里面拿出一个最大的和最小的,求 n 天的最大的和最小的差值相加. 析:一看就知道用set啊,多简单的STL,不过要注意,开long ...
- Hoax or what UVA - 11136(multiset的应用)
刚开始把题意理解错了,结果样例没过,后来发现每天只处理最大和最小的,其余的不管,也就是说昨天的元素会影响今天的最大值和最小值,如果模拟的话明显会超时,故用multiset,另外发现rbegin()的功 ...
- SQL Server 2008 R2——VC++ ADO 操作 存储过程 向datetime类型参数传入空值
==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...
- 越狱Season 1-Episode 15: By the Skin and the Teeth
Season 1, Episode 15: By the Skin and the Teeth -Pope: doctor...you can leave. 医生你得离开 -Burrows: It's ...
随机推荐
- 仿《雷霆战机》飞行射击手游开发--GameObject
转载请注明:http://www.cnblogs.com/thorqq/p/5646509.html 在上一篇中,我们介绍了各种游戏对象的功能及类的集成关系,现在我们来看看GameObject的源代码 ...
- Item47
STL迭代器分类:input迭代器.output迭代器.forward迭代器.bidirectional迭代器.random access迭代器. Input迭代器:只能向前移动,一次一步,客户只读取 ...
- Shell 循环读取文件
使用Shell将Windows环境下的文件拷贝到Linux下面的用法. 在linux下,将dos文件格式转换成linux文件格式的用法,vi打开,然后转到命令格式,执行,然后保存,就可以转换成linu ...
- lex&yacc5--YYSTYPE
yacc里的YYSTYPE默认是int型的,当然也可以勇%union来定义联合但是由于程序需要,我要将YYSTYPE定义为我自己定义的一个struct的指针然后作为一个全局变量,让lex在扫描的时候, ...
- 小黑的镇魂曲(HDU2155:贪心+dfs+奇葩解法)
题目:点这里 题目的意思跟所谓的是英雄就下100层一个意思……在T秒内能够下到地面,就可以了(还有一个板与板之间不能超过H高). 接触这题目是在昨晚的训练赛,当时拍拍地打了个贪心+dfs,果断跟我想的 ...
- linux gcc 和 g++ 编译
gcc编译 gcc -o test.out test.c g++ 编译 g++ -o test.out test.cpp
- 发送邮件(遵循smtp协议即简单的邮件发送协议)
HandleSendEmail.aspx逻辑 protected void Page_Load(object sender,EventArgs e) { foreach(var item in Req ...
- 编码错误设置错误报 "SyntaxError: Non-ASCII character '/xe6' "
无意中碰到键盘导致一段处理中文拼音的 python 代码跑起来报了个错 “SyntaxError: Non-ASCII character ‘/xe6' " 看了下是注释 # coding: ...
- H264学习第一篇(编码结构分析)
学习H264之前,最好阅读一下维基百科中有关H264的相关介绍,里面包含了其的发展历程.主要特点.参考文献.参考网站等. 研究H264的主要文件包括两份参考手册(一份是语法结构参考手册,一份是JM开发 ...
- 题目1444:More is better
时间限制:3 秒 内存限制:100 兆 特殊判题:否 提交:1362 解决:640 题目描述: Mr Wang wants some boys to help him with a project. ...