Description
There is a restaurant and a waitress who's task is to record in a card the time whenever a concumer enter or exit the restaurant int the format HH:MM:SS C (C is the event which can be letter 'E'-represent entry or letter "X"-represent exit ,or letter '?'-represent either 'E' or 'X').One card only record one client's entering or exiting message.
The restaurant start in the morning and end in the evening .No client exist in the restaurant before the opening and after the ending moment.
After the business, the waitress hava to deal with the cards ,but they are not in particular order and need sorting. Besides, the waitress forget to write the event C(mentioned above) in some cards.
Now, Given a set of cards with times and the events ,write a program to determine the maximum number of clients that can possibly be inside the restaurant at a specific moment during the business hours.
Input
The input contains several test cases. The first line of a test case contains a integer N-the number of cards collected after the business (N greater or equal to 2 and smaller or equal to 64800). Each of the next N lines contains the information written in a card, consisting of a time specification, followed by a single space, followed by an event specification. A time specification has the format HH:MM:SS, where HH represents hours (06<=HH<=23), MM represents minutes (00<=MM<=59) and SS represents seconds (00<=SS<=59). Within a test case, no two cards have the same time. An event specification is a single character: uppercase `E' for entry, uppercase `X' for exit and `?' for unknown. Information may be missing, but the information given is always correct. That is, the times noted in all cards are valid. Also, if a card describes an entry, then a client did enter a hall at the informed time; if a card describes an exit, then a client did leave a hall at the informed time; and if a card describes an unknown event, then a client did enter or leave a hall at the informed time.
The input ends up with a zero in a line.
Output
For each test case in the input, your program must print a single line, containing one single integer, the maximum total number of clients that could have been inside the dinner halls in a given instant of time.
Sample Input
4
07:22:03 X
07:13:22 E
08:30:51 E
21:59:02 X
4
09:00:00 E
20:00:01 X
09:05:00 ?
20:00:00 ?
8
10:21:00 E
10:25:00 X
10:23:00 E
10:24:00 X
10:26:00 X
10:27:00 ?
10:22:00 ?
10:20:00 ?
0
Sample Output
1
2
4
题目中给出了进入、离开以及不确定的记录,让我们求大厅中最多同时有多少人。我们假设所有的记录都是明确的即没有?记录,则我们可以在输入将各个时间点按时间流逝的顺序排序,从第一时间点开始,E则将时间点入栈,X则将上一时间点出栈,而此时大厅内人数即为栈的size,每访问一个时间点,我们进行一次比较,如果此时栈的size大于count,则将count值赋为该size的值。最后输出count即可。现在考虑?的情况,首先考虑一点就是?情况中有几项要被视为E,几项被视为X,通过分析可以得出这个值。另外若使大厅内的人数尽可能多,则?项应该考虑E优先于X,即以入为先,最后考虑X,根据这个思路可以得出以下做法:
#include<iostream>
#include<string>#include<queue>
#include<map>
#include<stack>
#inlcude<cstdio>
using namespace std;int main()
{
int n,i,hh,mm,ss,temp,count,countx,counte,countwenx,countwene;
//count即为最终结果值,counte为输入中E的数量,countx为输入中X的数量,countwenx为输入为?中视为X的项数,countwene为?中视为E的项数
char biaoshi;//输入的动作表示,E,X或者?stack<int> st;//栈
map<int,int> map1;//用来存储时间点与标识的对应关系
priority_queue<int,vector<int>,greater<int> > q;//小顶堆存储时间点
while(cin>>n&&n!=0)
{
countx=counte=0;
for(i=0;i<n;i++)
{//数据输入
cin>>hh;
getchar();
cin>>mm;
getchar();
cin>>ss;
getchar();
cin>>biaoshi;
getchar();
temp=hh*3600+mm*60+ss;//将时间换算为秒级的时间点
map1[temp]=biaoshi;
q.push(temp);
if(biaoshi=='X')
countx++;
else
{
if(biaoshi=='E')
counte++;
}
}
count=0;
countwenx=(n-2*countx)/2;//计算?中视为X的项数
countwene=(n-countx-counte-countwenx);//计算?中视为E的项数
while(q.size())
{
if(map1[q.top()]=='X')
st.pop();//出栈
if(map1[q.top()]=='E')
st.push(q.top());//入栈
if(map1[q.top()]=='?')
{
if(countwene)//优先考虑E的情况
{st.push(q.top());
countwene--;//?中视为E的项数减1
}
else
{
if(countwenx)//当E的情况考虑完毕后即将剩余?项作为X处理
{
st.pop();
countwenx--;//?中视为X的项数减1
}
}
}
q.pop();
if(st.size()>count)
count=st.size();
}
cout<<count<<endl;
}
return 0;
}