1 条题解

  • 0
    @ 2025-5-6 11:20:32

    题意分析

    1. 顾客行为模拟

      • 字母序列中,每个字母首次出现表示到达,第二次出现表示离开。
      • 若顾客到达时无空床位,则直接离开(计入统计)。
      • 离开的顾客优先级高于正在使用床位的顾客(即先处理离开事件)。
    2. 关键点

      • 维护当前占用床位的顾客集合和等待队列。
      • 按顺序处理字母序列,区分到达和离开事件。

    解题步骤

    1. 初始化

      • 读取床位数量beds和字母序列sequence
      • 使用哈希表status记录每个字母的状态(0未到达,1已到达,2已离开)。
    2. 模拟过程

      • 遍历sequence,对每个字母c
        • status[c] == 0(到达):
          • 若有空床位,标记为占用(status[c] = 1),占用数used++
          • 否则,离开数left++
        • status[c] == 1(离开):释放床位(used--)。
    3. 输出结果

      • 根据left的值选择输出格式。

    #include <math.h>
    #include <vector>
    #include <algorithm>
     
    using namespace std;
     
     
     
    int main()
    {
        int num=0;
     
        while(1){
            cin>>num;
     
            if(num!=0){
                string str;
                cin>>str;
                int sum=0;
                vector<char> temp;
     
                for(int i=0;i<str.size();i++){
                    char cos=str[i];
     
                    int have=0;
                    for(int j=0;j<temp.size();j++){
                        if(temp[j]==cos){
                            temp.erase(temp.begin()+j);
                            have=1;
                            break;
                        }
                    }
     
     
     
                    if(!have)
                        if(temp.size()>=num){
                            sum++;
     
     
                        }
                        else{
                            temp.push_back(cos);
     
     
                        }
                }
     
                if(sum==0)
                    cout<<"All customers tanned successfully."<<endl;
                else
                    cout<<sum/2<<" customer(s) walked away."<<endl;
     
            }
            else
                break;
     
        }
     
        return 0;
    }
    
    • 1

    信息

    ID
    251
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    2
    已通过
    0
    上传者