1016 Phone Bills (25 point(s))

A long-distance telephone company charges its customers by the following rules:

Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a long-distance call, the time will be recorded, and so will be the time when the customer hangs up the phone. Every calendar month, a bill is sent to the customer for each minute called (at a rate determined by the time of day). Your job is to prepare the bills for each month, given a set of phone call records.

Input Specification:

Each input file contains one test case. Each case has two parts: the rate structure, and the phone call records.

The rate structure consists of a line with 24 non-negative integers denoting the toll (cents/minute) from 00:00 - 01:00, the toll from 01:00 - 02:00, and so on for each hour in the day.

The next line contains a positive number N (≤1000), followed by N lines of records. Each phone call record consists of the name of the customer (string of up to 20 characters without space), the time and date (​​mm:dd:hh:mm​​​), and the word ​​on-line​​​ or ​​off-line​​.

For each test case, all dates will be within a single month. Each ​​on-line​​​ record is paired with the chronologically next record for the same customer provided it is an ​​off-line​​​ record. Any ​​on-line​​​ records that are not paired with an ​​off-line​​​ record are ignored, as are ​​off-line​​​ records not paired with an ​​on-line​​ record. It is guaranteed that at least one call is well paired in the input. You may assume that no two records for the same customer have the same time. Times are recorded using a 24-hour clock.

Output Specification:

For each test case, you must print a phone bill for each customer.

Bills must be printed in alphabetical order of customers' names. For each customer, first print in a line the name of the customer and the month of the bill in the format shown by the sample. Then for each time period of a call, print in one line the beginning and ending time and date (​​dd:hh:mm​​), the lasting time (in minute) and the charge of the call. The calls must be listed in chronological order. Finally, print the total charge for the month in the format shown by the sample.

Sample Input:

10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
10
CYLL 01:01:06:01 on-line
CYLL 01:28:16:05 off-line
CYJJ 01:01:07:00 off-line
CYLL 01:01:08:03 off-line
CYJJ 01:01:05:59 on-line
aaa 01:01:01:03 on-line
aaa 01:02:00:01 on-line
CYLL 01:28:15:41 on-line
aaa 01:05:02:24 on-line
aaa 01:04:23:59 off-line

Sample Output:

CYJJ 01
01:05:59 01:07:00 61 $12.10
Total amount: $12.10
CYLL 01
01:06:01 01:08:03 122 $24.40
28:15:41 28:16:05 24 $3.85
Total amount: $28.25
aaa 01
02:00:01 04:23:59 4318 $638.80
Total amount: $638.80

Experiential Summing-up

This question seems very difficult. As a matter of effect, The hard point lie in phone bill's calculation and understand the method of match two record. Really, it's not so hard. Here I mainly talk about the way of judging whether the record legal or not.
Each ​​​on-line​​​ record is paired with the chronologically next record for the same customer provided it is an ​​off-line​​​ record. Any ​​on-line​​​ records that are not paired with an ​​off-line​​​ record are ignored, as are ​​off-line​​​ records not paired with an ​​on-line​​​ record. This description test our comprehension of English. Firstly, we need to sort out the record of the same customer chronologically ( that is by time). Then, the first legal record's status is on-line, if the next record's status is also on-line. The former will become illegal and abandoned, but if the next record's status is off-line. The former and the latter will be matched.
If the first legal record's status is off-line, you can discard it straightly and judge the next. I hope I explain it clearly. The other thing just easy to overcome. But the premise is you can utilize STL proficiently~~

(The purpose of using English to portray my solution is that to exercise the ability of my expression of English and accommodate PAT advanced level's style.We can make progress together by reading and comprehending it. Please forgive my basic grammar's and word's error. Of course, I would appreciated it if you can point out my grammar's and word's error in comment section.( •̀∀•́ ) Furthermore, Big Lao please don't laugh at me because I just a English beginner settle for CET6    _(:з」∠)_  )

Accepted Code

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int rate[25];
struct bill
{
string start,end;
int time;
double cost;
};
bool cmp1(pair<string,bool> a,pair<string,bool> b)
{
return a.first<b.first;
}
bool cmp2(bill a,bill b)
{
return a.start<b.start;
}
int convert(string str)
{
int x;
sscanf(str.c_str(),"%d",&x);
return x;
}
double compute(string str1,string str2,int &time)
{
int d1=convert(str1.substr(3,2)),h1=convert(str1.substr(6,2)),m1=convert(str1.substr(9,2));
int d2=convert(str2.substr(3,2)),h2=convert(str2.substr(6,2)),m2=convert(str2.substr(9,2));
double cost=0;
time=0;
while(d1<d2||h1<h2||m1<m2)
{
time++;
cost+=rate[h1];
++m1;
if(m1>=60)
{
m1=0;
++h1;
}
if(h1>=24)
{
h1=0;
++d1;
}
}
return cost;
}
int main()
{
int n;
for(int i=0;i<24;++i)
scanf("%d",&rate[i]);
scanf("%d",&n);
string name,time,flag;
map<string, vector<pair<string,bool> > >mp;
for(int i=0;i<n;++i)
{
cin>>name>>time>>flag;
if(flag=="on-line")
mp[name].push_back(make_pair(time,true));
else
mp[name].push_back(make_pair(time,false));
}
map<string,vector<bill> >mp1;
for(map<string, vector<pair<string,bool> > >::iterator it=mp.begin();it!=mp.end();++it)
{
int f=1,pre;
sort(it->second.begin(),it->second.end(),cmp1);
for(int i=0;i<it->second.size();++i)
{
if(it->second[i].second==f)
{
if(f==0)
{
bill a;
a.start=it->second[pre].first;
a.end=it->second[i].first;
a.cost=compute(a.start,a.end,a.time);
mp1[it->first].push_back(a);
}
else
{
pre=i;
}
f=f?0:1;
}
else
{
if(it->second[i].second==1)
{
pre=i;
}
}
}
}
for(map<string,vector<bill> >::iterator it=mp1.begin();it!=mp1.end();++it)
{
sort(it->second.begin(),it->second.end(),cmp2);
printf("%s %s\n",it->first.c_str(),it->second[0].start.substr(0,2).c_str());
double sum=0;
for(int i=0;i<it->second.size();++i)
{
double temp=it->second[i].cost/100;
printf("%s %s %d $%.2f\n",it->second[i].start.substr(3,8).c_str(),it->second[i].end.substr(3,8).c_str(),it->second[i].time,temp);
sum+=temp;
}
printf("Total amount: $%.2f\n",sum);
}
return 0;
}