1 条题解
-
0
题意分析:
-
顾客行为模拟:
- 字母序列中,每个字母首次出现表示到达,第二次出现表示离开。
- 若顾客到达时无空床位,则直接离开(计入统计)。
- 离开的顾客优先级高于正在使用床位的顾客(即先处理离开事件)。
-
关键点:
- 维护当前占用床位的顾客集合和等待队列。
- 按顺序处理字母序列,区分到达和离开事件。
解题步骤:
-
初始化:
- 读取床位数量
beds
和字母序列sequence
。 - 使用哈希表
status
记录每个字母的状态(0
未到达,1
已到达,2
已离开)。
- 读取床位数量
-
模拟过程:
- 遍历
sequence
,对每个字母c
:- 若
status[c] == 0
(到达):- 若有空床位,标记为占用(
status[c] = 1
),占用数used++
。 - 否则,离开数
left++
。
- 若有空床位,标记为占用(
- 若
status[c] == 1
(离开):释放床位(used--
)。
- 若
- 遍历
-
输出结果:
- 根据
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
- 上传者