CodeForces 681C Heap Operations (模拟题,优先队列)
题意:给定 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 (模拟题,优先队列)的更多相关文章
- Codeforces 681C. Heap Operations 优先队列
C. Heap Operations time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- CodeForces 681C Heap Operations(模拟)
比较简单的模拟,建议使用STL优先队列. 代码如下: #include<iostream> #include<cstdio> #include<cstring> # ...
- Codeforces Round #357 (Div. 2) C. Heap Operations 模拟
C. Heap Operations 题目连接: http://www.codeforces.com/contest/681/problem/C Description Petya has recen ...
- Codeforces 767B. The Queue 模拟题
B. The Queue time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...
- Codeforces 691C. Exponential notation 模拟题
C. Exponential notation time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...
- CodeForces - 344B Simple Molecules (模拟题)
CodeForces - 344B id=46665" style="color:blue; text-decoration:none">Simple Molecu ...
- CodeForces - 344D Alternating Current (模拟题)
id=46667" style="color:blue; text-decoration:none">CodeForces - 344D id=46667" ...
- CodeForces - 344E Read Time (模拟题 + 二分法)
E. Read Time time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Heap Operations(模拟题)
Heap Operations time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
随机推荐
- Tkinter Entry
Python - Tkinter输入(Entry): 用于接受用户Entry小窗口部件单行文本字符串. 用于接受用户Entry小窗口部件单行文本字符串. 如果你想显示多行文本可以编辑,那么你应该使 ...
- CentOS 修改源为163和指定epel源和docker安装
首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-B ...
- Spring之导入和混合配置
在典型的Spring应用中,我们可能会同时使用自动化和显式配置.即便你更喜欢通过JavaConfig实现显式配置,但有的时候XML却是最佳的方案.幸好在Spring中,这些配置方案都不是互斥的.你尽可 ...
- Django 获取时间 和Linux 本地 系统时间 不一致
问题描述 Django 中获取的本地时间 ,和系统时间不一致 错误原因 Django在配置文件settings.py 中 默认配置 UTC世界标准时间,而北京时间是东八区,比UTC时间早8个小时. T ...
- mybatis 学习记录1
起因 以前刚学习java三大框架的时候持久层框架我是自学的是hibernate..感觉蛮好用的,so easy..后来大三实习公司用的是jpa(hibernate外包装一层)...再后来工作1年多用的 ...
- js添加对象数组
json 数组也是数组 var jsonstr="[{'name':'a','value':1},{'name':'b','value':2}]"; var jsonarray ...
- 使用net.sf.fjep.fatjar插件将第三方JAR包打包进自已的JAR包中
一般单个工程,在没有应用别人的jar包时导出为jar很简单,只要设置一个Main-Class就行了,也就是选择程序入口(main所在类).但是涉及到了数据库或需要用到第三方的JAR,就需要用到相应的数 ...
- aop中的顾问
通知只能指定织入的时间点,目标方法之前,之后,环绕,还是异常时. 要想指定切入点就要使用顾问
- 获取web.xml配置文件中的初始化值
TestServletConfig.java package com.huawei.config; import java.io.IOException;import java.util.Enumer ...
- mysql存储过程(procedure)
#创建带参数的存储过程 delimiter // ),out p int) begin ; end // delimiter call pro_stu_name_pass(@n,@p); select ...