Keep In Line_via牛客网
阅读原文时间:2023年07月08日阅读:3

题目

链接:https://ac.nowcoder.com/acm/contest/28537/H
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

又到饭点了,SK同学靠着惯性走到了食堂,但长长的队伍顿时让他失去了食欲。突然,他注意到某个窗口前的队伍里明显存在插队的现象,于是他默默记录下了同学们进队和出队的变化。

对于进队,SK同学只知道队伍里多了一个人,并不知道新来的人是老老实实站到了队尾还是插到了队伍里的某个位置;对于出队,SK同学能确定是队伍里站在最前面的人出队了。

初始时队伍为空,给出n条队伍进出的信息,保证已经出队的同学不会再入队,并且最终队伍也为空,现在SK同学想知道有多少不插队的好同学。

第一行是一个正整数T(≤ 5),表示测试数据的组数, 对于每组测试数据, 第一行是一个整数n(1≤ n ≤ 100000),表示这个队伍进出的信息数, 接下来n行,每行是两个字符串Opt Name,其中Opt为"in"代表进队,"out"代表出队,Name为进队或出队的人的名字, 所有信息按照时间顺序给出,名字由英文字母和阿拉伯数字组成,长度不超过10,保证每个人的名字各不相同。


对于每组测试数据,输出一行,包含一个整数,表示不插队的人数。

示例1

1
6
in quailty
in hwq1352249
out hwq1352249
in zhuaiballl
out quailty
out zhuaiballl


2

题解

这道题目说实话感觉比较难

从以下分析入手:

#mermaid-svg-lbhoyhpuHWMy1dM6 .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 .label text{fill:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 .node rect,#mermaid-svg-lbhoyhpuHWMy1dM6 .node circle,#mermaid-svg-lbhoyhpuHWMy1dM6 .node ellipse,#mermaid-svg-lbhoyhpuHWMy1dM6 .node polygon,#mermaid-svg-lbhoyhpuHWMy1dM6 .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-lbhoyhpuHWMy1dM6 .node .label{text-align:center;fill:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 .node.clickable{cursor:pointer}#mermaid-svg-lbhoyhpuHWMy1dM6 .arrowheadPath{fill:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-lbhoyhpuHWMy1dM6 .flowchart-link{stroke:#333;fill:none}#mermaid-svg-lbhoyhpuHWMy1dM6 .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-lbhoyhpuHWMy1dM6 .edgeLabel rect{opacity:0.9}#mermaid-svg-lbhoyhpuHWMy1dM6 .edgeLabel span{color:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-lbhoyhpuHWMy1dM6 .cluster text{fill:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-lbhoyhpuHWMy1dM6 .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-lbhoyhpuHWMy1dM6 text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-lbhoyhpuHWMy1dM6 .actor-line{stroke:grey}#mermaid-svg-lbhoyhpuHWMy1dM6 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 .sequenceNumber{fill:#fff}#mermaid-svg-lbhoyhpuHWMy1dM6 #sequencenumber{fill:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 #crosshead path{fill:#333;stroke:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 .messageText{fill:#333;stroke:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-lbhoyhpuHWMy1dM6 .labelText,#mermaid-svg-lbhoyhpuHWMy1dM6 .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-lbhoyhpuHWMy1dM6 .loopText,#mermaid-svg-lbhoyhpuHWMy1dM6 .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-lbhoyhpuHWMy1dM6 .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-lbhoyhpuHWMy1dM6 .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-lbhoyhpuHWMy1dM6 .noteText,#mermaid-svg-lbhoyhpuHWMy1dM6 .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-lbhoyhpuHWMy1dM6 .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-lbhoyhpuHWMy1dM6 .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-lbhoyhpuHWMy1dM6 .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-lbhoyhpuHWMy1dM6 .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-lbhoyhpuHWMy1dM6 .section{stroke:none;opacity:0.2}#mermaid-svg-lbhoyhpuHWMy1dM6 .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-lbhoyhpuHWMy1dM6 .section2{fill:#fff400}#mermaid-svg-lbhoyhpuHWMy1dM6 .section1,#mermaid-svg-lbhoyhpuHWMy1dM6 .section3{fill:#fff;opacity:0.2}#mermaid-svg-lbhoyhpuHWMy1dM6 .sectionTitle0{fill:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 .sectionTitle1{fill:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 .sectionTitle2{fill:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 .sectionTitle3{fill:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-lbhoyhpuHWMy1dM6 .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-lbhoyhpuHWMy1dM6 .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-lbhoyhpuHWMy1dM6 .grid path{stroke-width:0}#mermaid-svg-lbhoyhpuHWMy1dM6 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-lbhoyhpuHWMy1dM6 .task{stroke-width:2}#mermaid-svg-lbhoyhpuHWMy1dM6 .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-lbhoyhpuHWMy1dM6 .taskText:not([font-size]){font-size:11px}#mermaid-svg-lbhoyhpuHWMy1dM6 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-lbhoyhpuHWMy1dM6 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-lbhoyhpuHWMy1dM6 .task.clickable{cursor:pointer}#mermaid-svg-lbhoyhpuHWMy1dM6 .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-lbhoyhpuHWMy1dM6 .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-lbhoyhpuHWMy1dM6 .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-lbhoyhpuHWMy1dM6 .taskText0,#mermaid-svg-lbhoyhpuHWMy1dM6 .taskText1,#mermaid-svg-lbhoyhpuHWMy1dM6 .taskText2,#mermaid-svg-lbhoyhpuHWMy1dM6 .taskText3{fill:#fff}#mermaid-svg-lbhoyhpuHWMy1dM6 .task0,#mermaid-svg-lbhoyhpuHWMy1dM6 .task1,#mermaid-svg-lbhoyhpuHWMy1dM6 .task2,#mermaid-svg-lbhoyhpuHWMy1dM6 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-lbhoyhpuHWMy1dM6 .taskTextOutside0,#mermaid-svg-lbhoyhpuHWMy1dM6 .taskTextOutside2{fill:#000}#mermaid-svg-lbhoyhpuHWMy1dM6 .taskTextOutside1,#mermaid-svg-lbhoyhpuHWMy1dM6 .taskTextOutside3{fill:#000}#mermaid-svg-lbhoyhpuHWMy1dM6 .active0,#mermaid-svg-lbhoyhpuHWMy1dM6 .active1,#mermaid-svg-lbhoyhpuHWMy1dM6 .active2,#mermaid-svg-lbhoyhpuHWMy1dM6 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-lbhoyhpuHWMy1dM6 .activeText0,#mermaid-svg-lbhoyhpuHWMy1dM6 .activeText1,#mermaid-svg-lbhoyhpuHWMy1dM6 .activeText2,#mermaid-svg-lbhoyhpuHWMy1dM6 .activeText3{fill:#000 !important}#mermaid-svg-lbhoyhpuHWMy1dM6 .done0,#mermaid-svg-lbhoyhpuHWMy1dM6 .done1,#mermaid-svg-lbhoyhpuHWMy1dM6 .done2,#mermaid-svg-lbhoyhpuHWMy1dM6 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-lbhoyhpuHWMy1dM6 .doneText0,#mermaid-svg-lbhoyhpuHWMy1dM6 .doneText1,#mermaid-svg-lbhoyhpuHWMy1dM6 .doneText2,#mermaid-svg-lbhoyhpuHWMy1dM6 .doneText3{fill:#000 !important}#mermaid-svg-lbhoyhpuHWMy1dM6 .crit0,#mermaid-svg-lbhoyhpuHWMy1dM6 .crit1,#mermaid-svg-lbhoyhpuHWMy1dM6 .crit2,#mermaid-svg-lbhoyhpuHWMy1dM6 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-lbhoyhpuHWMy1dM6 .activeCrit0,#mermaid-svg-lbhoyhpuHWMy1dM6 .activeCrit1,#mermaid-svg-lbhoyhpuHWMy1dM6 .activeCrit2,#mermaid-svg-lbhoyhpuHWMy1dM6 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-lbhoyhpuHWMy1dM6 .doneCrit0,#mermaid-svg-lbhoyhpuHWMy1dM6 .doneCrit1,#mermaid-svg-lbhoyhpuHWMy1dM6 .doneCrit2,#mermaid-svg-lbhoyhpuHWMy1dM6 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-lbhoyhpuHWMy1dM6 .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-lbhoyhpuHWMy1dM6 .milestoneText{font-style:italic}#mermaid-svg-lbhoyhpuHWMy1dM6 .doneCritText0,#mermaid-svg-lbhoyhpuHWMy1dM6 .doneCritText1,#mermaid-svg-lbhoyhpuHWMy1dM6 .doneCritText2,#mermaid-svg-lbhoyhpuHWMy1dM6 .doneCritText3{fill:#000 !important}#mermaid-svg-lbhoyhpuHWMy1dM6 .activeCritText0,#mermaid-svg-lbhoyhpuHWMy1dM6 .activeCritText1,#mermaid-svg-lbhoyhpuHWMy1dM6 .activeCritText2,#mermaid-svg-lbhoyhpuHWMy1dM6 .activeCritText3{fill:#000 !important}#mermaid-svg-lbhoyhpuHWMy1dM6 .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-lbhoyhpuHWMy1dM6 g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-lbhoyhpuHWMy1dM6 g.classGroup text .title{font-weight:bolder}#mermaid-svg-lbhoyhpuHWMy1dM6 g.clickable{cursor:pointer}#mermaid-svg-lbhoyhpuHWMy1dM6 g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-lbhoyhpuHWMy1dM6 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-lbhoyhpuHWMy1dM6 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-lbhoyhpuHWMy1dM6 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-lbhoyhpuHWMy1dM6 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-lbhoyhpuHWMy1dM6 .dashed-line{stroke-dasharray:3}#mermaid-svg-lbhoyhpuHWMy1dM6 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-lbhoyhpuHWMy1dM6 #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-lbhoyhpuHWMy1dM6 #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-lbhoyhpuHWMy1dM6 #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-lbhoyhpuHWMy1dM6 #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-lbhoyhpuHWMy1dM6 #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-lbhoyhpuHWMy1dM6 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-lbhoyhpuHWMy1dM6 #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-lbhoyhpuHWMy1dM6 .commit-id,#mermaid-svg-lbhoyhpuHWMy1dM6 .commit-msg,#mermaid-svg-lbhoyhpuHWMy1dM6 .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-lbhoyhpuHWMy1dM6 .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-lbhoyhpuHWMy1dM6 .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-lbhoyhpuHWMy1dM6 g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-lbhoyhpuHWMy1dM6 g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-lbhoyhpuHWMy1dM6 g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-lbhoyhpuHWMy1dM6 g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-lbhoyhpuHWMy1dM6 g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-lbhoyhpuHWMy1dM6 .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-lbhoyhpuHWMy1dM6 .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-lbhoyhpuHWMy1dM6 .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-lbhoyhpuHWMy1dM6 .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-lbhoyhpuHWMy1dM6 .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-lbhoyhpuHWMy1dM6 .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-lbhoyhpuHWMy1dM6 .edgeLabel text{fill:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-lbhoyhpuHWMy1dM6 .node circle.state-start{fill:black;stroke:black}#mermaid-svg-lbhoyhpuHWMy1dM6 .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-lbhoyhpuHWMy1dM6 #statediagram-barbEnd{fill:#9370db}#mermaid-svg-lbhoyhpuHWMy1dM6 .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-lbhoyhpuHWMy1dM6 .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-lbhoyhpuHWMy1dM6 .statediagram-state .divider{stroke:#9370db}#mermaid-svg-lbhoyhpuHWMy1dM6 .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-lbhoyhpuHWMy1dM6 .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-lbhoyhpuHWMy1dM6 .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-lbhoyhpuHWMy1dM6 .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-lbhoyhpuHWMy1dM6 .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-lbhoyhpuHWMy1dM6 .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-lbhoyhpuHWMy1dM6 .note-edge{stroke-dasharray:5}#mermaid-svg-lbhoyhpuHWMy1dM6 .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-lbhoyhpuHWMy1dM6 .error-icon{fill:#522}#mermaid-svg-lbhoyhpuHWMy1dM6 .error-text{fill:#522;stroke:#522}#mermaid-svg-lbhoyhpuHWMy1dM6 .edge-thickness-normal{stroke-width:2px}#mermaid-svg-lbhoyhpuHWMy1dM6 .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-lbhoyhpuHWMy1dM6 .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-lbhoyhpuHWMy1dM6 .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-lbhoyhpuHWMy1dM6 .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-lbhoyhpuHWMy1dM6 .marker{fill:#333}#mermaid-svg-lbhoyhpuHWMy1dM6 .marker.cross{stroke:#333}

:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}
#mermaid-svg-lbhoyhpuHWMy1dM6 {
color: rgba(0, 0, 0, 0.75);
font: ;
}

判断有没有插队

将每一个人按照入队顺序进行编号,按照出队顺序写出编号

某一处编号是不是小于所有的它的后面的编号

yes

这个人没有插队

no

这个人插队了

考虑使用单调栈进行快速判断

1.用于求数列中每一个数往右数直到大于它的数的个数问题以及推广

2.用于根据剩余的结果来求有没有插队的问题

代码

#include <iostream>
#include <string>
#include <map>
#include <stack>
using namespace std;
int out[50009];
int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while (T--)
    {
        map<string, int>mp;
        int no = 0;//入队时候的编号
        int cnt = 0;//出队时候的计数
        int n;
        cin >> n;
        for (int i = 0; i < n; i++)
        {
            string name, option;
            cin >> option >> name;
            if (option == "in")
            {
                mp[name] = no++;
            }
            else
            {
                out[cnt++] = mp[name];
            }

        }
        mp.clear();
        stack<int>st;
        for (int i = 0, pos = 0; i < n / 2; i++)
        {

            if (st.empty() || out[pos] > st.top())
            {
                st.push(out[pos]);
            }
            else
            {
                while (!st.empty() && out[pos] <= st.top())
                {
                    st.pop();
                }
                st.push(out[pos]);
            }
            pos++;
        }
        cout << st.size() << endl;
    }
    return 0;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章