【队列】poj1250 Tanning Salon

来源:互联网 发布:网络社交媒体 编辑:程序博客网 时间:2024/06/15 12:58

传送门:http://poj.org/problem?id=1250


题目描述不太清晰……看了好久的题目才看懂。

就是说顾客按照给定字符串来或者离开,第一次出现是到来,第二次是离开,总共只有n个位置,如果没有空余位置就要等候。

最后统计离开的人数。


思路:由于有个排队的问题,所以我们就用队列来完成这个操作,进行一下模拟就好了

#include <iostream>#include <cstdio>#include <cstring>using namespace std;int n;string s;int list[100],l,r,boo[100];int main(){    while (scanf("%d",&n),n){        cin>>s;        int ans=0,cnt=0;        memset(boo,0,sizeof(boo));        l=0;r=0;        for (int i=0;i<s.length();i++){            if (boo[s[i]-'A']==2) {ans++; boo[s[i]-'A']=0; continue;}            if (boo[s[i]-'A']==0) {                list[++r]=s[i]-'A';                boo[s[i]-'A']=2;            }                        if (boo[s[i]-'A']==1) {                cnt--;                 boo[s[i]-'A']=0;             }            while (cnt<n && l<r){                l++;                if (boo[list[l]]!=2) continue;                boo[list[l]]=1;                cnt++;            }        }        if (ans==0){            cout<<"All customers tanned successfully.\n";        }        else {            cout<<ans<<" customer(s) walked away.\n";        }    }    return 0;}


0 0
原创粉丝点击