Codeforces 681C Heap Operation 【模拟】【堆】
来源:互联网 发布:中日海军对比2017知乎 编辑:程序博客网 时间:2024/06/06 03:05
题目链接:http://codeforces.com/contest/681/problem/C
题意:
堆有 3 种操作,分别是:
insert x: 往堆中添加一个数 x
getMin x:寻找堆中的最小值且为 x 的值。如果堆为空或堆中最小值不是 x , 则操作非法。
removeMin :删除这个堆中最小的数(如果有多个,只删除一个,堆为空则操作非法)
先给出一些操作,让你用最少的步数是的原先的操作合法,步数包括原本的和新添加的。
题解:
恶心模拟题。。
刚开始我就想直接对命令进行判断,然而WA掉了。。(WA 代码?)
更悲催的还是大数据WA掉了。
后来,我进行了改进,并参考了 xena 的代码,就过了。
TA 的思路:
还是每个命令单独判断,只不过使用了 vector<pair> 和 multiset<int> 的存储技巧就过了,而我就没有想到 TAT。
代码:
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int INF = 1e9;multiset<int> st;vector<pair<string, int> > v;int main(){ios::sync_with_stdio(false);int n;cin >> n;while (n--){string s;int a;cin >> s;if (s[0] == 'i'){cin >> a;st.insert(a);}if (s[0] == 'r'){if (st.empty())v.push_back({"insert", -INF});elsest.erase(st.begin());}if (s[0] == 'g'){cin >> a;while (st.size() && *st.begin() < a){v.push_back({"removeMin", 0});st.erase(st.begin());}if (st.empty() || *st.begin() > a){v.push_back({"insert", a});st.insert(a);}}v.push_back({s, a});}cout << v.size() << "\n";for (auto x : v){cout << x.first;if (x.first[0] != 'r')cout << " " << x.second;cout << "\n";}return 0;}
0 0
- Codeforces 681C Heap Operation 【模拟】【堆】
- CodeForces 681C 模拟-Heap Operations
- 【codeforces 681C】【优先队列+模拟】 Heap Operations
- codeforces 681C. Heap Operations(优先队列+模拟)
- CodeForces 681C Heap Operations
- codeforces 681C Heap Operations
- CodeForces 681C Heap Operations
- CodeForces 681C Heap Operations
- Codeforces Round #357 (Div. 2) C Heap Operations(模拟)
- Codeforces Round #357 (Div. 2) C Heap Operations (模拟)
- C实现heap堆排序
- Codeforces Round #357 (Div. 2) -- C. Heap Operations (优先队列模拟)
- Codeforces Round #357 (Div. 2) C. Heap Operations(模拟,优先队列)
- 【c++】模拟实现堆
- CodeForces 414 C. Mashmokh and Reverse Operation
- 编程算法 - 堆(heap) 代码(C)
- CodeForces 382C【模拟】
- Codeforces 749C【模拟】
- Linux Kernel 4.9中TCP BBR算法的科普解释
- 增强学习系列之(一):增强学习介绍
- 共同学习Java源代码-数据结构-TreeSet(一)
- 策略模式和Spring的结合在项目中的应用
- 371. Sum of Two Integers
- Codeforces 681C Heap Operation 【模拟】【堆】
- cf 341C (容斥原理)
- Win10 独立音量调整
- 3.1入门模拟B1008. 数组元素循环右移问题 (20)
- Javascript异步编程的4种方法
- 机器学习基础 第六章 模型选择
- 为什么要重写hashcode() 方法
- java.io.Serializable浅析
- Hibernate复合主键映射