您的位置:首页 > 其它

PAT(甲级)1016

2015-09-23 16:55 267 查看


1016. Phone Bills (25)

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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
#include <iostream>
#include <vector>
#include <map>
#include <utility>
#include <algorithm>

using namespace std;

struct Date{
int month;
int day;
int hour;
int min;
bool flag;
};

bool cmp(const Date &d1,const Date &d2){
if(d1.day == d2.day){
if(d1.hour == d2.hour){
return d1.min < d2.min;
}else
return d1.hour <d2.hour;
}else
return d1.day < d2.day;
}

map <string, vector<struct Date> > m;

void display(vector<Date> &v1){
vector<Date>::iterator it = v1.begin();
while(it != v1.end()){
cout <<it->month <<':' <<it->day <<':' <<it->hour <<':' <<it->min <<' ' <<it->flag <<endl;
it++;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// CALCULATE COST SEPERATELY
void process(map <string,vector<Date> >::iterator &it,int rate[],int &cost){
int size = (it->second).size();
if(size){
sort((it->second).begin(),(it->second).end(),cmp);
// display(it->second);
vector <Date> ::iterator vit1 = (it->second).begin();
vector <Date> ::iterator vit2 = vit1+1;
int month = vit1->month;
float sum=0.0;
size--;
bool print_flag = false;
while(size){
float part=0.0;

if(!(vit1->flag && !vit2->flag)) //next iterator
goto label;
if(!print_flag){
printf("%s %02d\n",it->first.c_str(),month);
print_flag =true;
}
int time;

if(vit2->day > vit1->day){ //across 00:00
time = 60 - vit1->min;
part += time*1.0*rate[vit1->hour];
for(int i=vit1->hour+1;i<24;i++){
time +=60;
part += rate[i]*60;
}
for(int i =vit1->day+1;i<vit2->day;i++){
time += 60*24;
part += cost;
}
for(int i=0;i<vit2->hour;i++){
time+= 60;
part += rate[i]*60;
}
part += vit2->min*1.0*rate[vit2->hour];
time += vit2->min;
}else{ //in the same day
if(vit1->hour == vit2->hour){
time = vit2->min - vit1->min;
part += time*1.0*rate[vit1->hour];
}else{
time = 60 - vit1->min;
part += time*1.0*rate[vit1->hour];
for(int i=vit1->hour+1;i<vit2->hour;i++){
time +=60;
part += rate[i]*60;
}
part += vit2->min*1.0*rate[vit2->hour];
time += vit2->min;
}
}
part /=100;
sum += part;
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",vit1->day,vit1->hour,vit1->min,
vit2->day,vit2->hour,vit2->min,time,part);
label: vit1++;
vit2++;
size--;
}
if(print_flag)
printf("Total amount: $%.2f\n",sum);
}

}

//////////////////////////////////////////////////////////////////////////////////////////////
//USING COUNTINT TO CALCULATE MONEY
float compute_money(vector <Date> ::iterator &vvit1,vector <Date> ::iterator it2,int rate[]){
int hour1=vvit1->hour;
int day1 = vvit1->day;
int min1 = vvit1->min;
// float sum =0.0;
int part = 0;
int time=0;
while(hour1 != it2->hour || min1 != it2->min ||day1 != it2->day){
part += rate[hour1];
min1++;
time++;
if(min1 == 60){
min1=0;
hour1++;
if(hour1 == 24){
hour1 = 0;
day1++;
}
}
}
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",vvit1->day,vvit1->hour,vvit1->min,
it2->day,it2->hour,it2->min,time,part*1.0/100);
return part*1.0/100;
}

void process(map <string,vector<Date> >::iterator &it,int rate[]){
int size = (it->second).size();
if(size){
sort((it->second).begin(),(it->second).end(),cmp);
// display(it->second);
vector <Date> ::iterator vit1 = (it->second).begin();
vector <Date> ::iterator vit2 = vit1+1;
int month = vit1->month;
float sum=0.0;
size--;
bool print_flag = false;
while(size){
float part=0.0;
if(!(vit1->flag && !vit2->flag)) //next iterator
goto label;
if(!print_flag){
printf("%s %02d\n",it->first.c_str(),month);
print_flag =true;
}
sum +=compute_money(vit1,vit2,rate) ;

label: vit1++;
vit2++;
size--;
}
if(print_flag)
printf("Total amount: $%.2f\n",sum);
}

}
//////////////////////////////////////////////////////////////////////////////

int main()
{
char name[25],status[10];
int rate[24],N,month,tmp,cost_day=0;
freopen("test.txt","r",stdin);
for(int i=0;i<24;i++){
scanf("%d",&rate[i]);
cost_day += rate[i];
}
cost_day *=60;
scanf("%d",&N);
for(int i=0;i<N;i++){
Date *d = new(Date);
scanf("%s%02d:%02d:%02d:%02d %s",name,&d->month,&d->day,&d->hour,&d->min,status);
if(status[1] == 'n')
d->flag = true;
else
d->flag = false;
if(m.find(name) != m.end()){
(m[name]).push_back(*d);
}else {
// vector <Date> *v = new (vector <Date>);
vector <Date> v;
v.push_back(*d);
m.insert(make_pair(name,v));
}
}
map <string,vector<Date> >::iterator iter= m.begin();
while(iter != m.end()){
// process(iter,rate); //version 2
process(iter,rate,cost_day);
iter++;
}

fclose(stdin);
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: