题意:给定 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. 【洛谷】P1388 算式(dp)

    题目描述 给出N个数字,不改变它们的相对位置,在中间加入K个乘号和N-K-1个加号,(括号随便加)使最终结果尽量大.因为乘号和加号一共就是N-1个了,所以恰好每两个相邻数字之间都有一个符号.例如: N ...

  2. ubuntu apt-get常用命令

    apt-cache search package 搜索包apt-cache show package 获取包的相关信息,如说明.大小.版本等sudo apt-get install package 安 ...

  3. <转>Python3.x和Python2.x的区别

    这个星期开始学习Python了,因为看的书都是基于Python2.x,而且我安装的是Python3.1,所以书上写的地方好多都不适用于Python3.1,特意在Google上search了一下3.x和 ...

  4. python3.5+flask+mysql

    该篇博客配置环境为:python版本3.5,flask2.0,python3中已经不再支持MySQLdb模块,所有这里我用了pymysql,所有使用前应该 安装pymysql:pip install ...

  5. 一个不明觉厉的貌似包含很多linux资料索引的网页

    http://man.lupaworld.com/content/other/Linux/linuxmanage/node108.html 貌似是个官方的doc之类的...

  6. spring-boot-actuator健康监控

    #健康监控 management.security.enabled=false health.mail.enabled =false http://localhost:54001/autoconfig ...

  7. 导出ppt中所有文本框

    打开PPT,按ALT+F11打开VBA编辑器,(部分电脑FN+ALT+F11)在左面的工程视图里点击右键,选择插入->模块,添加一个模块,名字都不用改. 然后点击顶部的"工具" ...

  8. flutter android keystore

      keytool -genkey -v -keystore E:/key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key     k ...

  9. Adjacent Bit Counts(uvalive)

    For a string of n bits x1, x2, x3,…, xn, the adjacent bit count of the string (AdjBC(x)) is given by ...

  10. python:一个轻松的递归逻辑

    #递归 age = 10 def dig(n): global age#函数dig引用全局变量age age += 2 n -= 1 if n != 1:#如果满足条件,则调用本身 dig(n) di ...