CodeForces 681C 模拟-Heap Operations

本文介绍了CodeForces 681C问题,涉及二叉堆的数据结构和操作。题目要求通过添加最少的额外操作,使给定的已损坏操作序列变得正确。解题策略是模拟堆操作,根据给定条件更新堆并记录必要的操作。

Description

Petya has recently learned data structure named “Binary heap”.

The heap he is now operating with allows the following operations:

put the given number into the heap;
get the value of the minimum element in the heap;
extract the minimum element from the heap;

Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.

In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:

insert x — put the element with value x in the heap;
getMin x — the value of the minimum element contained in the heap was equal to x;
removeMin — the minimum element was extracted from the heap (only one instance, if there were many).

All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.

While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya’s log and used them to make paper boats.

Now Vova is worried, if he made Petya’s sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.

Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.

Input

The first line of the input contains the only integer n (1 ≤ n ≤ 100 000) — the number of the records left in Petya’s journal.

Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.

Output

The first line of the output should contain a single integer m — the minimum possible number of records in the modified sequence of operations.

Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.

Note that the input sequence of operations must be the subsequence of the output sequence.

It’s guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.

题意:


一个二叉堆,存在三种操作:
插入一个值
取最小值
去掉一个最小值(最小值可能有多个)
现在有一些操作和操作的结果,插入最少的操作使得操作的结果正确。


题解:


模拟类题目,模拟操作即可。
二叉堆的三个操作:
插入一个值:不需要处理直接插入
取最小值:分多种情况,如果此时二叉堆的最小值跟结果一样,不需要处理,如果比结果大,那么插入结果的值,如果比结果的值小,那么去掉这个值直到最小值等于结果的值,如果不存在结果的值,就插入一个。
去掉一个最小值:如果二叉堆不为空,不需要处理,如果为空,随便插入一个值。


#include <iostream>
#include <queue>
#include <vector>
#include <functional>
#include <string>
#include <string.h>
#include <cmath>
#include <map>
#include <stdio.h>
#define MAXN 1000000 + 1
using namespace std;

struct cmp1{
    bool operator ()(int &a,int &b){
        return a>b;//最小值优先
    }
};
priority_queue<int,vector<int>,cmp1>q;
int re[2][MAXN];
char s[25];

int getnum()
{
    int k = 0;
    int sum = 0;
    for(int i = strlen(s) - 1; i >= 0 && s[i] != ' '; i--)
    {
        if(s[i] != '-')
            sum += (s[i] - '0') * (int)ceil(pow(10,k++));
        else
            sum = -1 * sum;
    }
    return sum;
}

int main()
{
    int n;
    int sum;
    while(cin >> n)
    {
        sum = 0;
        memset(re, 0, sizeof(re));
        scanf("%*c");
        while(!q.empty()) q.pop();
        while(n--)
        {
            gets(s);
            if(s[0] == 'i')
            {
                int t = getnum();
                re[0][sum] = 1;
                re[1][sum++] = t;
                q.push(t);
            }
            else if(s[0] == 'r')
            {
                if(q.empty())
                {
                    re[0][sum] = 1;
                    re[1][sum++] = 0;
                    re[0][sum] = 2;
                    re[1][sum++] = 0;
                }
                else
                {
                    re[0][sum] = 2;
                    re[1][sum++] = 0;
                    q.pop();
                }
            }
            else if(s[0] == 'g')
            {
                int t = getnum();
                if(q.empty())
                {
                    re[0][sum] = 1;
                    re[1][sum++] = t;
                    re[0][sum] = 3;
                    re[1][sum++] = t;
                    q.push(t);
                }
                else
                {
                    if(q.top() > t)
                    {
                        re[0][sum] = 1;
                        re[1][sum++] = t;
                        re[0][sum] = 3;
                        re[1][sum++] = t;
                        q.push(t);
                    }
                    else if(q.top() < t)
                    {
                        while(!q.empty() && q.top() < t)
                        {
                            re[0][sum] = 2;
                            re[1][sum++] = 0;
                            q.pop();
                        }
                        if(q.empty() || q.top() > t)
                        {
                            re[0][sum] = 1;
                            re[1][sum++] = t;
                            re[0][sum] = 3;
                            re[1][sum++] = t;
                            q.push(t);
                        }
                        else if(q.top() == t)
                        {
                            re[0][sum] = 3;
                            re[1][sum++] = t;
                        }
                    }
                    else if(q.top() == t)
                    {
                        re[0][sum] = 3;
                        re[1][sum++] = t;
                    }

                }
            }
        }
        printf("%d\n", sum);
        for(int i = 0; i < sum; i++)
        {
            switch(re[0][i])
            {
            case 1:
                printf("insert %d\n", re[1][i]);
                break;
            case 2:
                printf("removeMin\n");
                break;
            case 3:
                printf("getMin %d\n", re[1][i]);
                break;
            default:
                break;
            }
        }
    }
    return 0;
}
基于51单片机,实现对直流电机的调速、测速以及正反转控制。项目包含完整的仿真文件、源程序、原理图和PCB设计文件,适合学习和实践51单片机在电机控制方面的应用。 功能特点 调速控制:通过按键调整PWM占空比,实现电机的速度调节。 测速功能:采用霍尔传感器非接触式测速,实时显示电机转速。 正反转控制:通过按键切换电机的正转和反转状态。 LCD显示:使用LCD1602液晶显示屏,显示当前的转速和PWM占空比。 硬件组成 主控制器:STC89C51/52单片机(与AT89S51/52、AT89C51/52通用)。 测速传感器:霍尔传感器,用于非接触式测速。 显示模块:LCD1602液晶显示屏,显示转速和占空比。 电机驱动:采用双H桥电路,控制电机的正反转和调速。 软件设计 编程语言:C语言。 开发环境:Keil uVision。 仿真工具:Proteus。 使用说明 液晶屏显示: 第一行显示电机转速(单位:转/分)。 第二行显示PWM占空比(0~100%)。 按键功能: 1键:加速键,短按占空比加1,长按连续加。 2键:减速键,短按占空比减1,长按连续减。 3键:反转切换键,按下后电机反转。 4键:正转切换键,按下后电机正转。 5键:开始暂停键,按一下开始,再按一下暂停。 注意事项 磁铁和霍尔元件的距离应保持在2mm左右,过近可能会在电机转动时碰到霍尔元件,过远则可能导致霍尔元件无法检测到磁铁。 资源文件 仿真文件:Proteus仿真文件,用于模拟电机控制系统的运行。 源程序:Keil uVision项目文件,包含完整的C语言源代码。 原理图:电路设计原理图,详细展示了各模块的连接方式。 PCB设计:PCB布局文件,可用于实际电路板的制作。
【四旋翼无人机】具备螺旋桨倾斜机构的全驱动四旋翼无人机:建模与控制研究(Matlab代码、Simulink仿真实现)内容概要:本文围绕具备螺旋桨倾斜机构的全驱动四旋翼无人机展开研究,重点进行了系统建模与控制策略的设计与仿真验证。通过引入螺旋桨倾斜机构,该无人机能够实现全向力矢量控制,从而具备更强的姿态调节能力和六自由度全驱动特性,克服传统四旋翼欠驱动限制。研究内容涵盖动力学建模、控制系统设计(如PID、MPC等)、Matlab/Simulink环境下的仿真验证,并可能涉及轨迹跟踪、抗干扰能力及稳定性分析,旨在提升无人机在复杂环境下的机动性与控制精度。; 适合人群:具备一定控制理论基础和Matlab/Simulink仿真能力的研究生、科研人员及从事无人机系统开发的工程师,尤其适合研究先进无人机控制算法的技术人员。; 使用场景及目标:①深入理解全驱动四旋翼无人机的动力学建模方法;②掌握基于Matlab/Simulink的无人机控制系统设计与仿真流程;③复现硕士论文级别的研究成果,为科研项目或学术论文提供技术支持与参考。; 阅读建议:建议结合提供的Matlab代码与Simulink模型进行实践操作,重点关注建模推导过程与控制器参数调优,同时可扩展研究不同控制算法的性能对比,以深化对全驱动系统控制机制的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值