PAT A1016 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-linerecord. 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
  • wordlist
word meaning
chronological 时间顺序上地
  • 分析:

计算话费月账单,给出一堆电话记录,记录包括:用户名字 月:日:时:分 状态(开始 / 结束), 根据这些记录求出各个用户的话费账单
【注1】:一个用户只有on-off按时间先后成对出现的记录才有效。若先出现on后无off(反之同理),则为无效记录
【注2】:It is guaranteed that at least one call is well paired in the input. 这句话我开始理解错了,以为是保证每个用户都有合法的记录(on-off),实际是说所有用户,即有用户可能没有有效数据不应被输出,导致了Wrong 1

  • 坑点:

Wrong 1:样例2、3错, 有用户没有有效记录,不被输出

Wrong 2:样例4错,如果(i < right),可能刚好和下一个人匹配上(两条无效信息),
测试数据:
输入:

10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
4
a 01:01:00:00 on-line
a 01:01:00:01 off-line
a 01:01:00:03 on-line 
b 01:01:00:04 off-line

错误输出:

a 01
01:00:00 01:00:01 1 $0.10
01:00:03 01:00:04 1 $0.10
Total amount: $0.20

正确输出:

a 01
01:00:00 01:00:01 1 $0.10
Total amount: $0.10
  • 思路1: 双指针法
    step1:初始化:rate[],record[]
    step2:将record排序,先按人名字典递增,同一人按记录的时间先后(时间统一转化为分钟day * 24 * 60 + hour * 60 + min)排序
    step3:用两个指针left, right,[left, right)为属于同一个用户的记录,在[left, right)内遍历排好序的记录,若匹配成功,按要求输出
    【注】 费用的计算,对每一对匹配成功的记录,从on的时间(单位:分钟)开始,到off的时间,遍历每一分钟,将每一分钟对应的权值(rate[m % DAY / HOUR])累加到charge上,即得到一对记录的话费,再累加到总话费上sum_charge

  • code1:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010, DAY = 1440, HOUR = 60;
int n, month;
double rate[25];
struct Record{
	string name;
	int	d, h, m, time, sign;
}record[maxn];

bool cmp(Record a, Record b){
	return a.name != b.name ? a.name < b.name : a.time < b.time;
}
double GetCharge(int start, int end){
	double c = 0.0;
	for(int i = start; i < end; ++i){
		c += rate[i % DAY / HOUR];
	}
	return c;
}
void GetBill(int left, int right){
	//		cout << record[left].name; printf(" %02d\n", month);
		int i = left, flag = 0;	//Wrong:1: 样例2、3错, 有用户没有有效记录,不被输出 
		double sum_charge = 0;
		while(i + 1 < right){	 //Wrong 2:样例4错,如果(k < right),可能刚好和下一个人匹配上(两条无效信息) 
			if(record[i].sign == 0 && record[i+1].sign == 1){
				//匹配成功
				if(flag == 0){
					cout << record[i].name; printf(" %02d\n", month);
					flag = 1;
				}
				int start = record[i].time, end = record[i+1].time;
				double charge = GetCharge(start, end);	//求一次通话的花费 
				sum_charge += charge;
				printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n", record[i].d, record[i].h, record[i].m, record[i+1].d, record[i+1].h, record[i+1].m, end - start, charge);
				i += 2; 
			}else i++;
		}
		if(flag == 1) printf("Total amount: $%.2lf\n", sum_charge);
}
int main(){
//step1
	for(int i = 0; i < 24; ++i){
		int t_rate;
		scanf("%d", &t_rate);
		rate[i] = 0.01 * t_rate;
	}
 	scanf("%d", &n);
	for(int i = 0; i < n; ++i){
		string t_name, t_sign; 
		cin >> t_name; scanf("%d:%d:%d:%d", &month, &record[i].d, &record[i].h, &record[i].m); cin >> t_sign;
		record[i].name = t_name;
		record[i].sign = t_sign[1] == 'n' ? 0 : 1;	//0 == on & 1 == off
		record[i].time = record[i].d * DAY + record[i].h * HOUR + record[i].m;
	}
//step2
	sort(record, record+n, cmp);
//step3:
	int left = 0, right = 1;
	while(left < n){
		while(record[right].name == record[right-1].name) right++;
		GetBill(left, right);
		left = right++; 
	}
	return 0;
}
发布了271 篇原创文章 · 获赞 5 · 访问量 6557

猜你喜欢

转载自blog.csdn.net/qq_42347617/article/details/104032071