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 Sequentially in time
  • analysis:

Calculated monthly bill calls are given a bunch of phone records, including the record: 用户名字 月:日:时:分 状态(开始 / 结束)according to records obtained bill calls for each user
[Note 1]: a user has recorded only on-off pairs of time to be effective. If after the first on no off (and vice versa empathy), was invalid records
[Note 2]: It is guaranteed that at least one call is well paired in the input.these words I began to understand wrong, that is to ensure that each user has a legitimate recording (on-off), actually All users say that users may not have valid data should not be output, led Wrong 1

  • Pit:

Wrong 1: Sample 2,3 wrong, there is no valid user record is not output

Wrong 2: Sample 4 fault if (i <right), and the next person may just matched (two invalid information),
Test Data:
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
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

Error output:

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

Correct output:

a 01
01:00:00 01:00:01 1 $0.10
Total amount: $0.10
  • Ideas 1: Double Finger
    step1: Initialization: Rate [], record []
    step2: the record sorting, press increments names dictionary, the same person has time records ( time unified into minutes : ) day * 24 * 60 + hour * 60 + minSort
    step3: two cursor left, right, [left, right ) of a user belonging to the same record, the [left, right traverse the sorted records discharge) inside, if the matching is successful, according to the required output
    [Note] costing, for each pair recording successfully matched, the time from the oN: start (in minutes), the off time to traverse every minute, every minute of the weights corresponding (rate [m% DAY / HOUR ]) accumulated into the CHARGE, i.e. calls for a pair of records, the total call charge is added to the tired 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;
}
Published 271 original articles · won praise 5 · Views 6557

Guess you like

Origin blog.csdn.net/qq_42347617/article/details/104032071