PAT (Advanced Level) Practice 1016 Phone Bills (25 分)
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
思路&吐槽
- 先吐槽, 题目说的很不清楚, 从检测点来看, 应该是一个用户出现的全部有效记录都出现在同一个月,不会出现一个人出现两个月的记录要求分开计算
- 也不会出现一个电话从前一个月打到后一个月的情况
- 总体思路是把所有的记录放在一个
vector
中,先根据name
排序,把所有人分开, 再根据时间前后排序 - 合格的记录是: 相邻两个记录的
name
相等, 前一个是on-line
,后一个是off-line
(从柳神思路借鉴来的); 非常巧妙,避免了很多错误情况: 比如两个on-line
后面跟着off-line
,那么只有最近的是所求的 - 利用
map<string,vector<Timelog> >
存储合格记录, 因为map
默认是按照key
进行排序,即string
的字母序排序 - 计算费用时, 可以计算从
0时刻
到当前的所花的费用,再作差, 这样可以避免很多麻烦
AC code
#include<iostream>
#include<map>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int toll[25];
int callLength=0;
struct Timelog{
int month,day,hour,minute;
int status; //online:1,offline:0
string name;
};
bool cmp(const Timelog& a,const Timelog& b){
if(a.name!=b.name) return a.name<b.name;
if(a.month==b.month and a.day==b.day and a.hour==b.hour) return a.minute<b.minute;
if(a.month==b.month and a.day==b.day) return a.hour<b.hour;
if(a.month==b.month) return a.day<b.day;
return a.month<b.month;
}
double billFromZero(const Timelog& a){
double ret = toll[a.hour]*a.minute+toll[24]*60*a.day;
for(int i=0;i<a.hour;i++){
ret+=(toll[i]*60);
}
return ret/100.0;
}
double getCost(const Timelog& on,const Timelog& off){
//返回金额,并改变callLength,题目默认月份是相同的
callLength = (off.day-on.day)*24*60+(off.hour-on.hour)*60+(off.minute-on.minute);
return billFromZero(off)-billFromZero(on);
}
int main(){
for(int i=0;i<24;i++){
cin >> toll[i];
toll[24]+=toll[i];
}
int nRecord=0;
string logType;
cin >> nRecord;
getchar();
vector<Timelog> data(nRecord);
map<string,vector<Timelog> > customer; //字母序排列
//read logger
for(int i=0;i<nRecord;i++){
cin >> data[i].name;
scanf("%d:%d:%d:%d",&data[i].month,&data[i].day,&data[i].hour,&data[i].minute);
cin >> logType;
if(logType[1]=='n'){ //online
data[i].status=1;
}else{
data[i].status=0;
}
}
//sort
sort(data.begin(),data.end(),cmp);
//去除无效数据
for(int i=0;i<data.size()-1;i++){
if(data[i].name==data[i+1].name and data[i].status==1 and data[i+1].status==0){
customer[data[i].name].push_back(data[i]);
customer[data[i].name].push_back(data[i+1]);
}
}
//calculate the bill
for(auto& [k,v]:customer){
printf("%s %02d\n",k.c_str(),v[0].month);
double total=0;
for(int i=0;i<v.size();i+=2){
double cost = getCost(v[i],v[i+1]);
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n",v[i].day,v[i].hour,v[i].minute,v[i+1].day,v[i+1].hour,v[i+1].minute,callLength,cost);
total+=cost;
}
printf("Total amount: $%.2f\n", total);
}
return 0;
}