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
先将所有记录排序,排序时先按用户名排序再按时间排序,使得同一用户的所有记录依次排列;然后顺序处理同一个用户的所有记录,需要判断各用户是否有有效记录;对于每个用户的有效通话记录根据费率计算费用(美元),并得出总费用,其中计算时长可以由起始时间不断+1直到终止时间即可。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
class Record implements Comparable<Record>{
String id;
int mon,day,hour,min;
boolean status;//true为online
public Record(String id,String time,String status) {
this.id=id;
String[] t=time.split(":");
mon=Integer.valueOf(t[0]);
day=Integer.valueOf(t[1]);
hour=Integer.valueOf(t[2]);
min=Integer.valueOf(t[3]);
this.status=status.equals("on-line");
}
@Override
public int compareTo(Record o) {
if(!id.equals(o.id))//按用户名字典序排序
return id.compareTo(o.id);
//同一人所有记录按时间升序排序
else if(day!=o.day)
return day-o.day;
else if(hour!=o.hour)
return hour-o.hour;
else
return min-o.min;
}
}
public class Main {
static int[] toll=new int[24];//记录费率
//处理一对记录
static double solve(Record r1,Record r2) {
int d=r1.day,h=r1.hour,m=r1.min;//起始时间
int time=0;//总时间
double money=0;
//计算两条记录时间差,不断加一
while(d<r2.day||h<r2.hour||m<r2.min) {
money+=toll[h];//加上当前时间费率
m++;time++;
if(m==60) {
m=0;h++;
}
if(h==24) {
h=0;d++;
}
}
money/=100.0;//换算成美元
System.out.format("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",
r1.day,r1.hour,r1.min,r2.day,r2.hour,r2.min,time,money);
return money;
}
public static void main(String[] args) throws IOException {
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
String[] line=reader.readLine().split(" ");
for(int i=0;i<24;i++) {
toll[i]=Integer.valueOf(line[i]);
}
int N=Integer.valueOf(reader.readLine());
Record[] records=new Record[N];
for(int i=0;i<N;i++) {
line=reader.readLine().split(" ");
Record r=new Record(line[0], line[1], line[2]);
records[i]=r;
}
//排序
Arrays.sort(records);
int cur=0;//指向当前处理的用户第一条记录
while(cur<N) {
boolean flag=false;//标记该用户是否有匹配记录
boolean on=records[cur].status;//标记是否有online等待匹配,初值为第一条记录的status
double totalAmount=0.0;//总金额
int i;//检查该用户其他记录
for(i=cur+1;i<N&&records[i].id.equals(records[cur].id);i++) {
Record r=records[i];
if(!r.status&&on==true) {//当前记录为offline且前面已有online等待匹配
if(!flag) {//该用户第一组匹配记录
System.out.println(r.id+" "+String.format("%02d", r.mon));
flag=true;
}
totalAmount+=solve(records[i-1],r);
on=false;
}else if(r.status) {//当前记录为online,不处理
on=true;
}
}
if(flag) {//存在已匹配记录
System.out.format("Total amount: $%.2f\n", totalAmount);
}
cur=i;//指向下一条用户第一条记录
}
}
}