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
原创粉丝点击