Codeforces 681C:Heap Operations
Heap Operations
题目链接:
http://codeforces.com/contest/681/problem/C
题意:
有个堆,有三种操作
- insert x — 把x加到堆里
- getMin x — 得到堆里的最小值,且最小值等于x 当堆为空或者最小值不等于x时操作违法
- removeMin — 删除堆里的最小值 当堆为空时操作违法
题目给出了一些操作,不一定合法,往里再添加一些操作使得所有的操作都合法,求添加操作最少的情况并按序输出全部操作(新添加的和已存在的)
题解:
优先队列模拟操作
当前操作为insert x时向队列中加入x
当前操作为removeMin时将队列的top()删除,若队列为空则在removeMin前先insert一个在x的定义域内的任意值就好了
当前操作为getMin x时:如果队列为空,则先insert x
如果队列不为空,则比较队列的top()和x的大小关系,只要top()小于x就一直removeMin,在此之后若top()大于x或队列为空,则insert x
代码
#include<stdio.h>
#include<map>
#include<queue>
using namespace std;
struct Node
{
char ss;
int x;
}t[1000005];
char s[20];
struct node
{
int x;
node(int a=0)
{
x=a;
}
bool friend operator<(node a,node b)
{
return a.x>b.x;
}
};
priority_queue<node>w;
int main()
{
int n,x,num=0;
while(!w.empty())w.pop();
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%s",s);
if(s[0]=='i')
{
scanf("%d",&x);
t[num].ss='i';
t[num++].x=x;
w.push(node(x));
}
else if(s[0]=='g')
{
scanf("%d",&x);
while(!w.empty())
{
if(w.top().x<x)
{
t[num++].ss='r';
w.pop();
}
else break;
}
if(w.empty()||w.top().x!=x)
{
t[num].ss='i';
t[num++].x=x;
w.push(node(x));
}
t[num].ss='g';
t[num++].x=x;
}
else
{
if(!w.empty())
{
t[num++].ss='r';
w.pop();
}
else
{
t[num].ss='i';
t[num++].x=1;
t[num++].ss='r';
}
}
}
printf("%d\n",num);
for(int i=0;i<num;++i)
if(t[i].ss=='r')
printf("removeMin\n");
else if(t[i].ss=='g')printf("getMin %d\n",t[i].x);
else printf("insert %d\n",t[i].x);
}
转载于:https://www.cnblogs.com/kiuhghcsc/p/5596790.html
Codeforces 681C:Heap Operations相关推荐
- 【C++】C++11 STL算法(五):设置操作(Set operations)、堆操作(Heap operations)
目录 设置操作(Set operations) 一.includes 1.原型: 2.说明: 3.官方demo 二.set_difference 1.原型: 2.说明: 3.官方demo 三.set_ ...
- Codeforces 768E:Game of Stones
Codeforces 768E:Game of Stones 题目链接:http://codeforces.com/contest/768/problem/E 题目大意:给定$n$堆石子,初始每堆$s ...
- Codeforces 798C:Mike and gcd problem
Codeforces 798C:Mike and gcd problem 题目链接:http://codeforces.com/contest/798/problem/C 题目大意:给出一个大小为$n ...
- Codeforces 754E:Dasha and cyclic table
Codeforces 754E:Dasha and cyclic table 题目链接:http://codeforces.com/problemset/problem/754/E 题目大意:$A$矩 ...
- 溢出科普:heap overflow溢出保护和绕过
pr0mise · 2016/04/11 9:50 0x00 第一部分:heap overflow 接上文,来看另外一种溢出方式:堆溢出.相对于栈溢出来说,稍微麻烦一点 本文算是一个笔记,技术有限,难 ...
- java direct memory_第一讲 JVM内存四大类型:Heap,Stack,Contant,DirectMemory等
第一讲JVM内存四大类型:Heap,Stack,Contant,DirectMemory等 Stack属于栈的区域,属于每条线程私有的. 方法区和本地方法栈有很大的不同,方法区是用Java级别角度做的 ...
- Codeforces 348C:Subset Sums
Codeforces 348C:Subset Sums 题目链接:http://codeforces.com/contest/348/problem/C 题目大意:给出$n$个数,$m$个集合(每个集 ...
- myBatis连接MySQL报错误:No operations allowed after conn
为什么80%的码农都做不了架构师?>>> myBatis连接MySQL报错误:No operations allowed after connection closed.Con ...
- 今天项目报错: No operations allowed after connection closed
今天项目报错: No operations allowed after connection closed 错误原因:MySQL服务器默认的"wait_timeout"是28800 ...
最新文章
- oracle mod结果不正确,日常问题解决记录三:记一次Win10安装Oracle11g后遇到的问题...
- Keras之DNN:利用DNN算法【Input(8)→12+8(relu)→O(sigmoid)】利用糖尿病数据集训练、评估模型(利用糖尿病数据集中的八个参数特征预测一个0或1结果)
- apache camel_Apache Camel –从头开始开发应用程序(第2部分/第2部分)
- 【C语言进阶深度学习记录】三十八 C/C++语言中的函数声明与函数定义
- LightweightCTI开发实录(5)板卡适配器概述
- 【动态规划】牛客网:礼物的最大价值(数塔问题)
- [WCF编程]8.服务实例的生命周期
- 【VC++类型转换】CString类型到Char[]类型的转换
- 第一篇博客:A+B Problem
- Ubuntu系统打不开windows磁盘文件
- 孙空空的Vue之路-Day03-Class与Style的绑定操作
- 一个快速视频剪辑编辑神器 — Moviepy
- php中UNIX时间戳转换为日期
- 基于MPLAB X IDE配置位设置讲解
- 计算机科学领域的任何问题都可以通过增加一个间接的中间层来解决
- Redis学习2之redis-benchmark使用方法总结
- Atmel推出业内首款面向智能能源和自动化应用的IEEE 802.15.4g-2012双频段收发器
- BeautifulSoup解析豆瓣即将上映的电影信息
- unity死循环,无限内存增长的调试方法
- 一款强大的云笔记——notion