1016. Phone Bills (+)-vector sorting (sort function)

Source: Internet
Author: User
Tags time and date

The topics are as follows:

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


Making a long-distance call costs a certain amount per minute, and depending on the time of day when the call is made. When a customer starts connecting a long-distance call, the time would be recorded, and so would be the time when the custom Er 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, and given a set of phone call records.


Input Specification:


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


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


The next line contains a positive number N (<=), followed by N lines of records. Each phone call record consists of the name of the customer (string of characters without space), the time and Da Te (mm:dd:hh:mm), and the word "on-line" or "off-line".


For the test case, all dates would be within a single month. Each "on-line" record was paired with the chronologically next record for the same customer provided it's an "off-line" re Cord. Any "on-line" records that is not paired with an "off-line" record is ignored, as is "off-line" records not paired with An "on-line" record. It is guaranteed, at least one, call was well paired in the input. Assume that no, records for the same customer has the same time. Times is recorded using a 24-hour clock.


Output Specification:


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


Bills must is 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 SAM Ple. Then for each time period of a call, print on 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 is listed in chronological order. Finally, print the total charge for the month of 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


This problem requires that more than one months of the number of people in the call log statistics, the date of each call is calculated to get the bill, and output according to the format.

The difficulty of the topic is mainly in the selection of storage structure, the elimination of useless data and the output order of three aspects.

The first thing I thought of was to use containers to store names and records separately, and then to deal with the corresponding records, sorting names, and encountering many problems.

Later, referring to Sunbaigui's solution, he stored the name and record in the vector, and then sorted the vector with the sort function, prioritizing the names in dictionary order, and, if the names were the same, in chronological order (measured by time, minute, second, plus). This sort of records after the completion of the name from small to large, time from the front to the back of the order, for statistics is very convenient.

Sunbaigui's blog mentions the method of excluding useless records is very ingenious, because the order has the time from the front to the back of the sequence, and a person's record is continuous distribution, so only need to look at the front to the back of the first, then find off, create a new vector, If there is no on in the case of a direct press-in, if you encounter on the case of on the first pop up an invalid on, and then put this on, if you hit the off after, and the name is the same (indicating that the person's record), the off press. Here's a question, if the last one is on, then this invalid record cannot be ruled out (no chance of encountering the next on to eject it), so it should be judged on the previous basis whether the last element in the vector is on, or if it pops up directly, otherwise the description is a valid record.

Next only to the new vector (all valid, orderly records) to traverse the output, starting from the first element, each of the two adjacent elements must constitute a call record, the output is more cumbersome, need to be handled with care.

about the date of the calculation, to consider a comprehensive, such as telephone cross-day, so in consideration of time should be days, hours, minutes all add up, calculated as the sum of minutes, not only convenient sorting, in the calculation of the price can also be convenient to deal with, the calculation of the price of the method is the beginning of the month as the benchmark, Calculate the off and on charges for a call log separately and then make the difference .

The following algorithm is directly extracted on the Sunbaigui blog, I added a few comments on the basis of understanding to understand, here thanks to Sunbaigui to provide many good solutions.

#include <iostream>using namespace std #include <vector> #include <string> #include <algorithm > #include <stdio.h>struct call{string name;int month;int date;int hour;int minute;int total;//overall time is for date, hour, The sum of minute is converted to minutes, which facilitates the operation of sorting and time. string status;}; int charge[24];vector<call> all_calls; The input of the original record vector<call> format_calls; After the input record is processed bool compare (call A,call B)//To the Sort function to specify the comparison rules, first the name is sorted in dictionary order, if the name is the same, in chronological order {if (A.name < b.name) return 1;else if (A.name = = B.name && a.total < b.total) return 1;elsereturn 0;} Calculate money from the beginning of month to the time givenint chargebytime (int time)//For a given number of minutes relative to a month, the cost difference between off and on is a call Record the charges. {int hours = time/60;int minutes = Time%60;int Money = 0;int i;for (i = 0;i


1016. Phone Bills (+)-vector sorting (sort function)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.