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 (≤), 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

题意:

  计算每一个用户每一个月用在打电话上的开销。

思路:

  模拟

Code:

  1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 vector<int> costs(24);
6 int costOfDay;
7
8 struct People {
9 string name;
10 vector<string> on_line;
11 vector<string> off_line;
12 };
13
14 bool cmp1(People a, People b) { return a.name < b.name; }
15 bool cmp2(string s1, string s2) { return s1 < s2; }
16
17 pair<int, double> parse(string str1, string str2) {
18 int day1 = stoi(str1.substr(3, 2));
19 int day2 = stoi(str2.substr(3, 2));
20 pair<int, int> time1 = {stoi(str1.substr(6, 2)), stoi(str1.substr(9, 2))};
21 pair<int, int> time2 = {stoi(str2.substr(6, 2)), stoi(str2.substr(9, 2))};
22 int numOfDays = 0;
23 if (time1.first * 60 + time1.second < time2.first * 60 + time2.second) {
24 numOfDays = day2 - day1;
25 } else {
26 numOfDays = day2 - day1 - 1;
27 }
28 double cost = 0.0;
29 int totalMinute = 0;
30 if (time1.first == time2.first) {
31 cost += (time2.second - time1.second) * costs[time1.first];
32 totalMinute += time2.second - time1.second;
33 } else {
34 cost += (60 - time1.second) * costs[time1.first] +
35 time2.second * costs[time2.first];
36 totalMinute += 60 - time1.second + time2.second;
37 for (int i = time1.first + 1; i < time2.first; ++i) {
38 cost += 60 * costs[i];
39 totalMinute += 60;
40 }
41 }
42 totalMinute += numOfDays * 24 * 60;
43 cost += numOfDays * costOfDay;
44 double dollar = cost / 100.0;
45 return {totalMinute, dollar};
46 }
47
48 int main() {
49 for (int i = 0; i < 24; ++i) {
50 cin >> costs[i];
51 costOfDay += costs[i] * 60;
52 }
53 int N;
54 cin >> N;
55 getchar();
56 string record;
57 map<string, People> m;
58 for (int i = 0; i < N; ++i) {
59 getline(cin, record);
60 int space1, space2;
61 space1 = record.find(' ');
62 space2 = record.find(' ', space1 + 1);
63 string name = record.substr(0, space1);
64 string date = record.substr(space1 + 1, space2 - space1 - 1);
65 string status = record.substr(space2 + 1);
66 if (status == "on-line") {
67 m[name].on_line.push_back(date);
68 } else {
69 m[name].off_line.push_back(date);
70 }
71 }
72 vector<People> peoples;
73 for (auto it : m) {
74 it.second.name = it.first;
75 peoples.push_back(it.second);
76 }
77 sort(peoples.begin(), peoples.end(), cmp1);
78 for (People p : peoples) {
79 sort(p.on_line.begin(), p.on_line.end(), cmp2);
80 sort(p.off_line.begin(), p.off_line.end(), cmp2);
81 vector<string> start, end;
82 int index = 0, len1 = p.on_line.size(), len2 = p.off_line.size();
83 for (int i = 0; i < len2 && index < len1; ++i) {
84 while (index < len1 && p.on_line[index] < p.off_line[i]) index++;
85 if (index > 0) {
86 start.push_back(p.on_line[index - 1]);
87 end.push_back(p.off_line[i]);
88 }
89 }
90 cout << p.name << " " << start[0].substr(0, 2) << endl;
91 double totalCost = 0.0;
92 for (int i = 0; i < start.size(); ++i) {
93 cout << start[i].substr(3) << " " << end[i].substr(3) << " ";
94 pair<int, double> ans = parse(start[i], end[i]);
95 totalCost += ans.second;
96 cout << ans.first << " $" << fixed << setprecision(2) << ans.second
97 << endl;
98 }
99 cout << "Total amount: $" << totalCost << endl;
100 }
101
102 return 0;
103 }

写了好久,最后通过了一组测试点,溜了……(待我刷完PAT最后一题,再来补你)

1016 Phone Bills的更多相关文章

  1. PAT 1016 Phone Bills[转载]

    1016 Phone Bills (25)(25 分)提问 A long-distance telephone company charges its customers by the followi ...

  2. 1016 Phone Bills (25 分)

    1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following rul ...

  3. PAT甲级1016. Phone Bills

    PAT甲级1016. Phone Bills 题意: 长途电话公司按以下规定向客户收取费用: 长途电话费用每分钟一定数量,具体取决于通话时间.当客户开始连接长途电话时,将记录时间,并且客户挂断电话时也 ...

  4. PAT 1016 Phone Bills(模拟)

    1016. Phone Bills (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A long-di ...

  5. 1016. Phone Bills (25)——PAT (Advanced Level) Practise

    题目信息: 1016. Phone Bills (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A l ...

  6. PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

    1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following r ...

  7. PAT A 1016. Phone Bills (25)【模拟】

    题目:https://www.patest.cn/contests/pat-a-practise/1016 思路:用结构体存储,按照名字和日期排序,然后先判断是否有效,然后输出,时间加减直接暴力即可 ...

  8. PAT 1016. Phone Bills

    A long-distance telephone company charges its customers by the following rules: Making a long-distan ...

  9. 1016. Phone Bills (25) -vector排序(sort函数)

    题目如下: A long-distance telephone company charges its customers by the following rules: Making a long- ...

  10. 1016 Phone Bills (25)(25 point(s))

    problem A long-distance telephone company charges its customers by the following rules: Making a lon ...

随机推荐

  1. PAT-1043(Is It a Binary Search Tree)JAVA实现

    Is It a Binary Search Tree PAT-1043 主要涉及到根据前序遍历序列片段是否是一颗二叉树,这里有一个小tip就是插入序列就是二叉树的前序遍历序列. 第二个是会对排序二叉树 ...

  2. redis基础:redis下载安装与配置,redis数据类型使用,redis常用指令,jedis使用,RDB和AOF持久化

    知识点梳理 课堂讲义 课程计划 1. REDIS 入 门 (了解) (操作)   2. 数据类型 (重点) (操作) (理解) 3. 常用指令   (操作)   4. Jedis (重点) (操作) ...

  3. 因MemoryCache闹了个笑话

    前言 是这么一回事: 我正在苦思一个业务逻辑,捋着我还剩不多的秀发,一时陷入冥想中...... 突然聊天图标一顿猛闪,打开一看,有同事语音: 大概意思是:同事把项目中Redis部分缓存换成Memory ...

  4. Microsoft Teams 最新功能发布:协作篇

    正在进行的2021年的Microsoft Ignite大会,发布了一系列跟Microsoft Teams相关的新功能,英文介绍请参考 https://techcommunity.microsoft.c ...

  5. Vue.js 实现的 3D Tab菜单

    今天给大家带来一款基于VueJS的3D Tab菜单,它跟我们之前分享的许多CSS3 Tab菜单不同的是,它可以随着鼠标移动呈现出3D立体的视觉效果,每个tab页面还可以通过CSS自定义封面照片.它的核 ...

  6. 为什么要从 Linux 迁移到 BSD 4

    为什么要从 Linux 迁移到 BSD 4 许可证问题 Linux GPL 许可证对开发者的要求比较严格,它是一种开源的反模式,因为它强制发布所有修改过的源代码,并且阻止其他开源项目的集成,例如 GP ...

  7. MyBatis(九):MyBatis类型处理器(TypeHandler)详解

    TypeHandler简介 TypeHandler,顾名思义类型转换器,就是将数据库中的类型与Java中的类型进行相互转换的处理器. MyBatis 在设置预处理语句(PreparedStatemen ...

  8. yolo训练数据集

    最近了解了下yolov3的训练数据集部分,总结了以下操作步骤:(基于pytorch框架,请预先装好pytorch的相关组件) 1.下载ImageLabel软件对图片进行兴趣区域标记,每张图片对应一个x ...

  9. vue 快速入门 系列 —— vue 的基础应用(上)

    其他章节请看: vue 快速入门 系列 vue 的基础应用(上) Tip: vue 的基础应用分上下两篇,上篇是基础,下篇是应用. 在初步认识 vue一文中,我们已经写了一个 vue 的 hello- ...

  10. java面试-集合类不安全问题及解决方案

    一.List 1.代码演示 public class ArrayListNotSafeDemo { public static void main(String[] args) { List<S ...