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 ...
随机推荐
- Ubuntu将软件(Sublime Text 2为例)锁定到启动器
Ubuntu中打开某安装好的软件,然后右击启动器(Launcher)上打开的图标就可以将该软件锁定到启动器或者从启动器解锁. 然而,有许多软件下载后直接解压就能用,不需要安装,这种情况采用上述方法锁定 ...
- InnoDB 离线转储工具
一,应用场景; 1,表空间严重损坏,无法恢复;2,数据库表空间文件丢失后从磁盘上打捞出部分数据页面;3,恢复删除记录; 二,功能; 从数据页中直接转储出文本格式的行数据,从而可以后期用 LOAD DA ...
- C++运用SDK截屏
引言 最近有一个需要截取当前屏幕,并保存成BMP文件的需求.整个需求,拆分成三步:1.截取屏幕,获得位图数据.2.配合bmp文件结构信息,将数据整合.3.对整合后的数据做操作,如保存在本地.通过网络传 ...
- Java使用基本JDK操作ZIP文件以及zip文件的加密、解密等功能
Java使用基本JDK操作ZIP文件 http://blog.csdn.net/zhyh1986/article/details/7723649 Java解压和压缩带密码的zip文件 http://b ...
- c# 类型初始值设定项引发异常
今天使用VS2010编译c#程序,编译顺利通过,点击运行启动程序,弹框提示如题错误.断点调试,程序甚至都没有进入main函数!!查阅网上资料,几种分析如下(1)反射机制 (2)app.config文件 ...
- jQuery ui 中文日历
jQuery ui 中文日历 <link href="css/jquery-ui-1.10.4.custom.min.css" rel="stylesheet&qu ...
- spring-cloud-hystrix熔断
依赖pom <dependencyManagement> <dependencies> <dependency> <groupId>org.spring ...
- laravel5.1关于lists函数的bug
查询语句为: class DateAttrModel extends BaseModel{ -- static function getDays(--){ $days = self::lists('d ...
- PHP获取IP及地区信息(纯真IP数据库)
昨天在写程序的时候,发现在用户的时候记录IP和地区信息也许以后用得上,去网上找了找,发现实现的方式有好多好多,因为我用的ThinkPHP,后来又去TP官网找了找,最后采用了下面这种方法. <?p ...
- putty 代理设置
代理分 http 和 socket 代理. IE Internet属性-> 连接 -> 局域网设置 普通HTTP代理 直接输入IP,端口 socket 代理 点高级 套接字设置 安装好P ...