题意:给定 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. 十六 在沉睡中停止(在sleep() 状态下停止线程)

    1 如果线程在sleep()状态下停止线程,会是什么效果? 答案: 如果在sleep状态下停止某一线程,会进入sleep的catch块中, 抛出InterruptedException 异常,并且清除 ...

  2. uboot环境变量的设置(未完待续)

    使用print打印当前系统环境变量. 1. SMDK2440 # print baudrate=115200 bootargs=noinitrd root=/dev/nfs nfsroot=192.1 ...

  3. Julia - 算术基本函数

    符号函数和绝对值函数 abs(x) 函数求 x 的绝对值(幅值) julia> abs(3) 3 julia> abs(-3) 3 abs2(x) 函数求 x 的绝对值(幅值)的平方 ju ...

  4. 硬盘IOPS与读写速度

    IOPS (Input/Output Per Second)即每秒的输入输出量(或读写次数),是衡量磁盘性能的主要指标之一.IOPS是指单位时间内系统能处理的I/O请求数量,一般以每秒处理的I/O请求 ...

  5. python装饰器注意事项

    内容: 1.装饰器基本结构复习 2.装饰器注意事项 python装饰器详细内容:http://www.cnblogs.com/wyb666/p/8748102.html 1.装饰器基本结构复习 装饰器 ...

  6. vue打包静态资源路径不正确的解决办法

    vue打包静态资源路径不正确的解决办法 vue项目完成打包上线的时候会碰到静态资源找不到的问题,常见的有两个 1.js,css路径不对 解决办法:打开config/index.js,将其中的asset ...

  7. RabbitMQ双活实践(转)

    有货RabbitMQ双活实践   消息服务中间件在日常工作中用途很多,如业务之间的解耦,其中 RabbitMQ 是比较容易上手且企业使用比较广泛的一种,本文主要介绍有货在使用 RabbitMQ 的一些 ...

  8. 变体类型 Variant VARIANT

    变体类型 Variant VARIANT class RTL_DELPHIRETURN Variant: public TVarData typedef struct    tagVARIANT  V ...

  9. Unity脚本开发基础 C#

    1. MonoBehaviour 类 常用事件响应函数: 2. 访问游戏对象 (1) 通过名称来查找 (2) 通过标签来查找 上述函数比较费时,应避免在 Update 函数调用. 3. 访问组件 对于 ...

  10. mybatis与springdata的一些简单比较与思考

    主题 最近在用mybatis做项目,有一些感触想记录下,主要是mybatis(以及它的一些插件)相比较于Spring data(或者jpa,hibernate等)的优势地方. 感触 我觉得mybati ...