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 (≤), 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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
#include <string>
#include <set>
#include <map>
using namespace std;
const int maxn = ;
int n;
string name,line;
int m, d, h, mm, all = ;
int cost[] = { };
struct person {
string name;
int time;
int d;
int h;
int mm;
int status;
};
bool cmp(person p1, person p2) {
return p1.name == p2.name ? p1.time < p2.time : p1.name < p2.name;
}
float cal_cost(person p) {
float c = ;
c += p.d * all * ;
for (int j = ; j < p.h; j++) {
c += cost[j] * ;
}
c += cost[p.h] * p.mm;
return c;
}
vector<person> v;
map<string, vector<person> > mp;
int main() {
for (int i = ; i < ; i++) {
scanf("%d", &cost[i]);
all += cost[i];
}
scanf("%d", &n);
for (int i = ; i < n; i++) {
cin >> name;
scanf("%d:%d:%d:%d", &m, &d, &h, &mm);
cin >> line;
person p;
p.name = name;
if (line == "on-line") {
p.status = ;
}
else {
p.status = ;
}
p.d = d;
p.h = h;
p.mm = mm;
p.time = * d + * h + mm;
v.push_back(p);
}
sort(v.begin(), v.end(), cmp);
for (int i = ; i < n; i++) {
if (v[i].name == v[i - ].name && (v[i].status == && v[i - ].status == )) {
mp[v[i].name].push_back(v[i - ]);
mp[v[i].name].push_back(v[i]);
}
}
for (auto it = mp.begin(); it != mp.end(); it++) {
string name = it->first;
cout << name;
printf(" %02d\n", m);
float total = ;
vector<person> pv = it->second;
for (int i = ; i < pv.size(); i += ) {
float cost_1 = , cost_2 = ;
cost_1 = cal_cost(pv[i - ]);
cost_2 = cal_cost(pv[i]);
float total_cost = (cost_2 - cost_1) / ;
total += total_cost;
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", pv[i - ].d, pv[i - ].h, pv[i - ].mm, pv[i].d, pv[i].h, pv[i].mm, pv[i].time - pv[i - ].time, total_cost);
}
printf("Total amount: $%.2f\n", total);
}
system("pause");
return ;
}
注意点:逻辑很简单的一道题,我却做了4个小时多才AC,实在太菜了。
一开始没想到这道题本质是个排序题,看到要按从小到大排列,就想到了set和map自动排序,然后就进去出不来了,一直纠结在怎么判断两个通话记录是一对上。
第二个坑是计算花费,看了大佬的思路才发现原来可以通过相对值的差来得到,而不一定非要把两个时间的差求出来,把每个时间点相对0的价格算出来以后的差就是所要的价格。
第三点是最后输出结果,对每个人还要输出一个总和,自己只想到了用各种if判断,看了大佬思路,用map的vector实在是太方便了,而且还准确。
PAT A1016 Phone Bills (25 分)——排序,时序的更多相关文章
- PAT A1016 Phone Bills (25)
题目描述 A long-distance telephone company charges its customers by the following rules: Making a long-d ...
- A1016 Phone Bills (25 分)
A long-distance telephone company charges its customers by the following rules: Making a long-distan ...
- PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)
1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following r ...
- A1016 Phone Bills (25)(25 分)
A1016 Phone Bills (25)(25 分) A long-distance telephone company charges its customers by the followin ...
- PTA PAT排名汇总(25 分)
PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- PAT 1051 Pop Sequence (25 分)
返回 1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ...
随机推荐
- 数据机构-折半查找法(二分查找法)-Python实现
Python实现二分查找法(基于顺序表) class List: elem=[] #存储顺序表元素 last=-1 #设置初始为-1 SeqList = List() #创建一个顺序表 print(& ...
- ELK日志分析平台系统windows环境搭建和基本使用
ELK(ElasticSearch, Logstash, Kibana),三者组合在一起就可以搭建实时的日志分析平台啦! Logstash主要用来收集.过滤日志信息并将其存储,所以主要用来提供信息. ...
- hash table (youtube 2)
https://www.youtube.com/watch?v=jEdaduyLLqY
- 微软Azure AspNetCore微服务实战 第二期
2018新年快乐!继第1期微服务技术交流活动后(http://www.huodongxing.com/event/4403161781600),本次我们将与上海的小伙伴们继续深入分享微服务技术的应用实 ...
- JSP内置对象——pageContext对象和config对象
它对应的常用方法有: 现在,我新建一个“pageContext.jsp”页面,可以获得“session_page1.jsp”这个页面中保存的用户名: pageContext.jap: session_ ...
- 移动端不利用HTML5和echarts开发一样可以实现大数据展示及炫酷统计系统(产品技术综合)
一.由于项目需要进行手机看板展示设计及开发展示效果图如下:
- Android根据图片Uri获取图片path绝对路径的几种方法【转】
在Android 编程中经常会用到Uri转化为文件路径,如我们从相册选择图片上传至服务器,一般上传前需要对图片进行压缩,这时候就要用到图片的绝对路径. 下面对我开发中uri转path路径遇到的问题进行 ...
- Java并发编程(六)volatile关键字解析
由于volatile关键字是与Java的内存模型有关的,因此在讲述volatile关键之前,我们先来了解一下与内存模型相关的概念和知识. 一.内存模型的相关概念 Java内存模型规定所有的变量都是存在 ...
- C# 异步编程4 async与await 异步程序开发
随着C#异步程序开发系列的深入,你会发现编写异步程序越发简单.事物的发展就是这样的规律,从简单到复杂再到简单. 在C# 5.0中我们可以通过async与await关键字实现快捷的异步程序开发,如下: ...
- Azure 标准与高级托管磁盘存储的相互转换
托管磁盘提供两种存储选项:高级(基于 SSD)和标准(基于 HDD). 它允许基于性能需求在这两个选项之间轻松切换,并保障最短停机时间. 非托管磁盘不具备此功能. 但可以轻松转换为托管磁盘,以便在这两 ...