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;
}
时间: 2024-12-09 13:13:34

CodeForces 681C Heap Operations (模拟题,优先队列)的相关文章

CodeForces 681C Heap Operations(模拟)

比较简单的模拟,建议使用STL优先队列. 代码如下: #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; #define N 1000010 char outs[N][20]; int outn[N]; priority_queue<int,vector<int>,greater<int> >q

Codeforces 48C The Race 模拟题

题目链接:点击打开链接 题意: 给定n个加油站,一辆车由A点跑到B点,每个100m有一个加油站,每开100m需要10升油. 在每个车站会检查一下油量,若车子若开不到下一个加油站则加x升油. 开始有x升油 下面给出加油的记录. 问下一次加油在哪一站.若答案唯一输出具体哪站. 油箱容量无限 思路: 水模拟.. #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>

CodeForces - 344B Simple Molecules (模拟题)

CodeForces - 344B Simple Molecules Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Mad scientist Mike is busy carrying out experiments in chemistry. Today he will attempt to join three atoms into

CodeForces - 344D Alternating Current (模拟题)

CodeForces - 344D Alternating Current Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! H

CodeForces - 427B (模拟题)

Prison Transfer Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description The prison of your city has n prisoners. As the prison can't accommodate all of them, the city mayor has decided to transfer c of t

Problem E: 穷游中国在统题 优先队列 + 模拟

http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=4 Problem E: 穷游中国在统题 Description Travel_poorly队是广工大目前最年轻的金牌队伍,队内成员分别是tmk.YFQ.Maple.这天他们接到教练的order要给新生杯统题,统题是个不轻松的工作,要评估各个题目的难度,设计出一套有梯度的套题,使得做题的情况有区分度.tmk很快想出了解决的办法,他给每道题目都设置了一个难度值,然后按照难度值进行筛选题

经典算法题每日演练——第九题 优先队列

原文:经典算法题每日演练--第九题 优先队列 前端时间玩小爬虫的时候,我把url都是放在内存队列里面的,有时我们在抓取url的时候,通过LCS之类的相似度比较,发现某些url是很重要的, 需要后端解析服务器优先处理,针对这种优先级比较大的url,普通的队列还是苦逼的在做FIFO操作,现在我们的需求就是优先级大的优先服务,要做 优先队列,非堆莫属. 一:堆结构 1:性质 堆是一种很松散的序结构树,只保存了父节点和孩子节点的大小关系,并不规定左右孩子的大小,不像排序树那样严格,又因为堆是一种完全二叉

poj 3087 Shuffle&#39;m Up(模拟题)

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6143   Accepted: 2880 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of

codeforces round#259 div2 B题(KMP)

先上链接:http://codeforces.com/contest/454/problem/B B. Little Pony and Sort by Shift time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day, Twilight Sparkle is interested in how to sort a se