题意:给定 n 个按顺序的命令,但是可能有的命令不全,让你补全所有的命令,并且要求让总数最少。

析:没什么好说的,直接用优先队列模拟就行,insert,直接放入就行了,removeMin,就得判断一下队列是不是空的,然后再考虑getMin,这个是不是对应的值,如果队列中首元素比它大,那么就加上一个,

如果相等直接取出,如果小于就不断取队列中最小元素。

代码如下:

#include <bits/stdc++.h>

using namespace std;
char s[15], t[30];
vector<string> ans; int main(){
int n, x;
while(cin >> n){
ans.clear();
priority_queue<int, vector<int>, greater<int> > q;
for(int i = 0; i < n; ++i){
scanf("%s", s); if(s[0] == 'i'){
scanf("%d", &x);
sprintf(t, "insert %d", x);
ans.push_back(string(t));
q.push(x);
}
else if(s[0] == 'r'){
if(q.empty()){
ans.push_back("insert 1");
q.push(1);
}
ans.push_back("removeMin");
q.pop();
}
else{
scanf("%d", &x);
while(true){
if(q.empty() || q.top() > x){
q.push(x);
sprintf(t, "insert %d", x);
ans.push_back(string(t));
}
else if(q.top() == x){ break; }
else{
ans.push_back("removeMin");
q.pop();
}
}
sprintf(t, "getMin %d", x);
ans.push_back(string(t));
}
}
printf("%d\n", ans.size());
for(int i = 0; i < ans.size(); ++i)
cout << ans[i] << endl;
}
return 0;
}

CodeForces 681C Heap Operations (模拟题,优先队列)的更多相关文章

  1. Codeforces 681C. Heap Operations 优先队列

    C. Heap Operations time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  2. CodeForces 681C Heap Operations(模拟)

    比较简单的模拟,建议使用STL优先队列. 代码如下: #include<iostream> #include<cstdio> #include<cstring> # ...

  3. Codeforces Round #357 (Div. 2) C. Heap Operations 模拟

    C. Heap Operations 题目连接: http://www.codeforces.com/contest/681/problem/C Description Petya has recen ...

  4. Codeforces 767B. The Queue 模拟题

    B. The Queue time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  5. Codeforces 691C. Exponential notation 模拟题

    C. Exponential notation time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...

  6. CodeForces - 344B Simple Molecules (模拟题)

    CodeForces - 344B id=46665" style="color:blue; text-decoration:none">Simple Molecu ...

  7. CodeForces - 344D Alternating Current (模拟题)

    id=46667" style="color:blue; text-decoration:none">CodeForces - 344D id=46667" ...

  8. CodeForces - 344E Read Time (模拟题 + 二分法)

    E. Read Time time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  9. Heap Operations(模拟题)

     Heap Operations time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. 配置IVR实现语音

    1.新建系统录音档   登入FreePBX,在Admin面板下选择System Recordings,如下图:   在这个页面可以上传制作好的一些录音,例如像“欢迎致电XXX,按1中文,按2英文... ...

  2. Shell教程快速入门

    Shell即是一种命令语言,又是一种程序设计语言,使用者可以通过Shell访问操作系统的内核服务. Shell编程和java.python.C一样,只要一个能编写代码的文本编辑器和一个能解释执行的脚本 ...

  3. USB驱动程序之USB设备驱动程序2鼠标用作键盘学习笔记

    1.usbmouse.c (1)probe函数 在这个probe函数后判断是不是一个鼠标,先得到usb_host_interface结构体,除了端点0外,端点个数如果不是1,返回错误,表示不是自己能支 ...

  4. MyBatis的适用场景和生命周期

    MyBatis使用场景 对比Hibernate和MyBatis是我们常见的话题,Hibernate作为常用的ORM框架,它使用起来简单易懂,对于SQL语言的封装,让对于SQL并不是很熟练的程序员也可以 ...

  5. Servlet的复习

    Servlet概述 在JavaWeb阶段,使用Servlet是很经常的是事情,Servlet作为MVC中控制器(C)的存在,是不可缺少的一部分.当然Servlet作为JavaWeb的三大组件之一(其他 ...

  6. 常见的HTTP Header

    文件信息: Content-Type: application/x-javascript Content-Length: 2000 Content-Type:指定请求和响应的内容类型,如果未指定即为t ...

  7. python与冒泡排序

    上一篇文章,介绍了一个非常快的排序算法--桶排序,但是它的缺点就是太耗资源了,这次要实现的算法就不用太耗资源了,它就是冒泡排序. 问题提出: 将以下数据升序排列:9, 2, 8, 6, 4 冒泡排序原 ...

  8. 10_java之继承和抽象类

    01继承的概述 *A:继承的概念 *a:继承描述的是事物之间的所属关系,通过继承可以使多种事物之间形成一种关系体系 *b:在Java中,类的继承是指在一个现有类的基础上去构建一个新的类, 构建出来的新 ...

  9. 如何将Excel日期快速转化为文本格式?

    Excel表中日期格式其实是数值,有时候需要原样转成文本,有时候也要将文本转成日期. 我发现了一个方法,估计是最快的了.不需要用那一堆year() month()之类的函数. 快速将日期格式转化为文本 ...

  10. WCF配置Tcp协议

    注意点: 1,<serviceMetadata httpGetEnabled="false"/>   2,       <services>         ...