PAT 1016 Phone Bills 排序

11 篇文章 0 订阅

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-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

先将所有记录排序,排序时先按用户名排序再按时间排序,使得同一用户的所有记录依次排列;然后顺序处理同一个用户的所有记录,需要判断各用户是否有有效记录;对于每个用户的有效通话记录根据费率计算费用(美元),并得出总费用,其中计算时长可以由起始时间不断+1直到终止时间即可。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;

class Record implements Comparable<Record>{
	String id;
	int mon,day,hour,min;
	boolean status;//true为online
	public Record(String id,String time,String status) {
		this.id=id;
		String[] t=time.split(":");
		mon=Integer.valueOf(t[0]);
		day=Integer.valueOf(t[1]);
		hour=Integer.valueOf(t[2]);
		min=Integer.valueOf(t[3]);
		this.status=status.equals("on-line");
	}
	@Override
	public int compareTo(Record o) {
		if(!id.equals(o.id))//按用户名字典序排序
			return id.compareTo(o.id);
		//同一人所有记录按时间升序排序
		else if(day!=o.day)
			return day-o.day;
		else if(hour!=o.hour)
			return hour-o.hour;
		else 
			return min-o.min;
	}
}
public class Main {

	static int[] toll=new int[24];//记录费率

	//处理一对记录
	static double solve(Record r1,Record r2) {
		int d=r1.day,h=r1.hour,m=r1.min;//起始时间
		int time=0;//总时间
		double money=0;
		//计算两条记录时间差,不断加一
		while(d<r2.day||h<r2.hour||m<r2.min) {
			money+=toll[h];//加上当前时间费率
			m++;time++;
			if(m==60) {
				m=0;h++;
			}
			if(h==24) {
				h=0;d++;
			}
		}
		money/=100.0;//换算成美元
		System.out.format("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",
				r1.day,r1.hour,r1.min,r2.day,r2.hour,r2.min,time,money);
		return money;
	}
    public static void main(String[] args) throws IOException {
    	BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));

    	String[] line=reader.readLine().split(" ");    	
    	for(int i=0;i<24;i++) {
    		toll[i]=Integer.valueOf(line[i]);
    	}
    	
    	int N=Integer.valueOf(reader.readLine());
    	Record[] records=new Record[N];
    	
    	for(int i=0;i<N;i++) {
    		line=reader.readLine().split(" ");
    		Record r=new Record(line[0], line[1], line[2]);
    		records[i]=r;
    	}
    	//排序
    	Arrays.sort(records);
    	int cur=0;//指向当前处理的用户第一条记录
    	while(cur<N) {
    		boolean flag=false;//标记该用户是否有匹配记录
    		boolean on=records[cur].status;//标记是否有online等待匹配,初值为第一条记录的status
    		double totalAmount=0.0;//总金额
    		int i;//检查该用户其他记录
    		for(i=cur+1;i<N&&records[i].id.equals(records[cur].id);i++) {
    			Record r=records[i];
    			if(!r.status&&on==true) {//当前记录为offline且前面已有online等待匹配
    				if(!flag) {//该用户第一组匹配记录
    					System.out.println(r.id+" "+String.format("%02d", r.mon));
    					flag=true;
    				}
    				totalAmount+=solve(records[i-1],r);
    				on=false;
    			}else if(r.status) {//当前记录为online,不处理
    				on=true;
    			}
    		}
    		if(flag) {//存在已匹配记录
    			System.out.format("Total amount: $%.2f\n", totalAmount);
    		}
    		cur=i;//指向下一条用户第一条记录
    	}
    }
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值