1016. Phone Bills (25)

来源:互联网 发布:模型仓 淘宝 编辑:程序博客网 时间:2024/06/16 10:14

题目地址

https://www.patest.cn/contests/pat-a-practise/1016

题目描述

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 1010CYLL 01:01:06:01 on-lineCYLL 01:28:16:05 off-lineCYJJ 01:01:07:00 off-lineCYLL 01:01:08:03 off-lineCYJJ 01:01:05:59 on-lineaaa 01:01:01:03 on-lineaaa 01:02:00:01 on-lineCYLL 01:28:15:41 on-lineaaa 01:05:02:24 on-lineaaa 01:04:23:59 off-line

Sample Output:

CYJJ 0101:05:59 01:07:00 61 $12.10Total amount: $12.10CYLL 0101:06:01 01:08:03 122 $24.4028:15:41 28:16:05 24 $3.85Total amount: $28.25aaa 0102:00:01 04:23:59 4318 $638.80Total amount: $638.80

ac 1

  • 计算时间和费用(采用与0点相减 计算的办法)
  • 如何剔除无效的记录 (原纪录排序后逐一判断)

ac代码(未优化)

#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <sstream>#include <stack> #include <map> #include <set> #include <unordered_map> #include <unordered_set> using namespace std;const int INF = 0x7fffffff;const int MIN_INF = - INF -1;typedef long long int LL;int fees[24];int dayfee;int n;struct record{  string name;  int mon;  int day, h,m;  int timeInt;  bool on_off;};vector<record> records;int getTimeInt(int day, int h,int m){    return day * 24 * 60 + h * 60 + m;}// 以秒为单位 - 0:0:0的费用int getCost(int day,int h,int m){    int cost = 0;    for(int i = 0; i < h; i++) // h    {        cost += fees[i] * 60;    }    cost += m * fees[h]; // m    return cost + day * dayfee; // day}// 持续时间int lastMin(int day1, int h1,int m1,int day2 , int h2,int m2){    int t1 = getTimeInt(day1, h1, m1);    int t2 = getTimeInt(day2, h2, m2);    return t2 - t1;}// 所需花费int lastCost(int day1, int h1,int m1,int day2, int h2,int m2){    int c1 = getCost(day1,h1,m1);    int c2 = getCost(day2,h2,m2);    return c2 - c1;}struct data{  int dd;  int hh;  int mm;  int dd2;  int hh2;  int mm2;  int lasttime;  double cost;};bool cmp1(record r1, record r2){  if(r1.name < r2.name)  {    return true;  }else if(r1.name == r2.name)  {    if(r1.timeInt < r2.timeInt)    {      return true;    }  }  return false;}int main() {    // freopen("in.txt","r",stdin);    dayfee = 0;    for(int i=0;i<24;i++)    {        scanf("%d",&fees[i]);        dayfee += 60 * fees[i];    }    scanf("%d",&n);    record rt;    for(int i=0;i<n;i++)    {        cin >> rt.name;        scanf("%d:%d:%d:%d",&rt.mon, &rt.day, &rt.h, &rt.m);        string r_on_off;        cin >> r_on_off;        if(r_on_off == "on-line")        {            rt.on_off = true;        }else{            rt.on_off = false;        }        rt.timeInt = getTimeInt(rt.day, rt.h,rt.m);        records.push_back(rt);    }    sort(records.begin(), records.end(),cmp1);    int len = records.size();    // 得到有效的record    vector<record> vr;    int index = 0; // 按顺序找到第一个on-line    while(index < len)    {        if(records[index].on_off == false)            index ++;        else            break;    }    vr.push_back(records[index]);    string lastName = records[index].name;    bool lastLine = true;    for(int i= index + 1; i < len;i++)    {        if(records[i].name == lastName) // 与上一name相同        {            if(lastLine)            {                if(records[i].on_off == true){                    vr.pop_back();                    vr.push_back(records[i]);                }else{                    vr.push_back(records[i]);                    lastLine = false;                }            }else{                if(records[i].on_off == true)                {                    vr.push_back(records[i]);                    lastLine = true;                }            }        }else{ // 与上一个name不同            if(records[i].on_off == false)                continue;            if(lastLine)                vr.pop_back();            vr.push_back(records[i]);            lastName = records[i].name;            lastLine = true;        }    }// endfor    int vrLen = vr.size();    if(vr[vrLen - 1].on_off == true)    {        vr.pop_back();        vrLen--;    }    // 计算结果   int mon = vr[0].mon;  lastName = "";  vector<data> ans;  for(int i=0;i<vrLen;i+=2)  {    record r1 = vr[i];    record r2 = vr[i+1];    if(r1.name != lastName)    {      if(lastName != "")      {        cout << lastName;        printf(" %02d\n",mon);        double rtt = 0;        int ansLen = ans.size();        for(int j= 0;j<ansLen;j++)        {          double cc = 0.01 * ans[j].cost;          rtt += cc;          printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n",            ans[j].dd,ans[j].hh,ans[j].mm,            ans[j].dd2,ans[j].hh2,ans[j].mm2,            ans[j].lasttime,cc            );        }        printf("Total amount: $%.2lf\n",rtt);        ans.clear();      }      data dt;      dt.dd = r1.day;      dt.hh = r1.h;      dt.mm = r1.m;      dt.dd2 = r2.day;      dt.hh2 = r2.h;      dt.mm2 = r2.m;      dt.lasttime = lastMin(r1.day, r1.h,r1.m, r2.day, r2.h, r2.m);      dt.cost = lastCost(r1.day, r1.h,r1.m, r2.day, r2.h, r2.m);      ans.push_back(dt);      lastName = r1.name;    }else{      data dt;      dt.dd = r1.day;      dt.hh = r1.h;      dt.mm = r1.m;      dt.dd2 = r2.day;      dt.hh2 = r2.h;      dt.mm2 = r2.m;      dt.lasttime = lastMin(r1.day, r1.h,r1.m, r2.day, r2.h, r2.m);      dt.cost = lastCost(r1.day, r1.h,r1.m, r2.day, r2.h, r2.m);      ans.push_back(dt);    }  }  if(!ans.empty())  {      cout << lastName;      printf(" %02d\n",mon);      double rtt = 0;      int ansLen = ans.size();      for(int j= 0;j<ansLen;j++)      {        double cc = 0.01 * ans[j].cost;        rtt += cc;        printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n",          ans[j].dd,ans[j].hh,ans[j].mm,          ans[j].dd2,ans[j].hh2,ans[j].mm2,          ans[j].lasttime,cc          );      }      printf("Total amount: $%.2lf\n",rtt);      ans.clear();  }  return 0;}

使用map结构 在处理结果是更加清楚

#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <sstream>#include <stack> #include <map> #include <set> #include <unordered_map> #include <unordered_set> using namespace std;const int INF = 0x7fffffff;const int MIN_INF = - INF -1;typedef long long int LL;int fees[24];int dayfee;int n;struct record{  string name;  int mon;  int day, h,m;  int timeInt;  bool on_off;};bool cmp1(record r1, record r2) // 记录排序规则{    if(r1.name < r2.name)        return true;    else if(r1.name == r2.name)    {        if(r1.timeInt < r2.timeInt)            return true;    }    return false;}vector<record> records;int getTimeInt(int day, int h,int m){    return day * 24 * 60 + h * 60 + m;}// 以秒为单位 - 0:0:0的费用int getCost(int day,int h,int m){    int cost = 0;    for(int i = 0; i < h; i++) // h    {        cost += fees[i] * 60;    }    cost += m * fees[h]; // m    return cost + day * dayfee; // day}// 持续时间int lastMin(int day1, int h1,int m1,int day2 , int h2,int m2){    int t1 = getTimeInt(day1, h1, m1);    int t2 = getTimeInt(day2, h2, m2);    return t2 - t1;}// 所需花费int lastCost(int day1, int h1,int m1,int day2, int h2,int m2){    int c1 = getCost(day1,h1,m1);    int c2 = getCost(day2,h2,m2);    return c2 - c1;}// 记录结果的结构体struct data{    int dd;    int hh;    int mm;    int dd2;    int hh2;    int mm2;    int lasttime;    double cost;};int main() {    freopen("in.txt","r",stdin);    dayfee = 0;    for(int i=0;i<24;i++)    {        scanf("%d",&fees[i]);        dayfee += 60 * fees[i];    }    scanf("%d",&n);    record rt;    for(int i=0;i<n;i++)    {        cin >> rt.name;        scanf("%d:%d:%d:%d",&rt.mon, &rt.day, &rt.h, &rt.m);        string r_on_off;        cin >> r_on_off;        if(r_on_off == "on-line")        {            rt.on_off = true;        }else{            rt.on_off = false;        }        rt.timeInt = getTimeInt(rt.day, rt.h,rt.m);        records.push_back(rt);    }    sort(records.begin(), records.end(),cmp1);    int len = records.size();    // 得到有效的record    vector<record> vr;    int index = 0; // 按顺序找到第一个on-line    while(index < len)    {        if(records[index].on_off == false)            index ++;        else            break;    }    vr.push_back(records[index]);    string lastName = records[index].name;    bool lastLine = true;    for(int i= index + 1; i < len;i++)    {        if(records[i].name == lastName) // 与上一name相同        {            if(lastLine)            {                if(records[i].on_off == true){                    vr.pop_back();                    vr.push_back(records[i]);                }else{                    vr.push_back(records[i]);                    lastLine = false;                }            }else{                if(records[i].on_off == true)                {                    vr.push_back(records[i]);                    lastLine = true;                }            }        }else{ // 与上一个name不同            if(records[i].on_off == false)                continue;            if(lastLine)                vr.pop_back();            vr.push_back(records[i]);            lastName = records[i].name;            lastLine = true;        }    }// endfor    int vrLen = vr.size();    if(vr[vrLen - 1].on_off == true)    {        vr.pop_back();        vrLen--;    }    // 计算结果 使用map 存储    int mon = vr[0].mon;    map<string,vector<data>> mp;    map<string,double> mpTotal;    for(int i=0;i<vrLen;i+=2) // 每次取两条记录    {        record r1 = vr[i];        record r2 = vr[i+1];        data dt;        dt.dd = r1.day;        dt.hh = r1.h;        dt.mm = r1.m;        dt.dd2 = r2.day;        dt.hh2 = r2.h;        dt.mm2 = r2.m;        dt.lasttime = lastMin(r1.day, r1.h,r1.m, r2.day, r2.h, r2.m);        dt.cost = lastCost(r1.day, r1.h,r1.m, r2.day, r2.h, r2.m);        mp[r1.name].push_back(dt);        mpTotal[r1.name] += dt.cost;     }    // 打印出结果    map<string,vector<data>>::iterator it = mp.begin();    while(it != mp.end())    {        cout << it->first;        printf(" %02d\n",mon);        int ansLen = it->second.size();        for(int j= 0;j<ansLen;j++)        {            double cc = 0.01 * it->second[j].cost;            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n",            it->second[j].dd, it->second[j].hh, it->second[j].mm,            it->second[j].dd2, it->second[j].hh2, it->second[j].mm2,            it->second[j].lasttime,cc            );        }        printf("Total amount: $%.2lf\n",mpTotal[it->first] * 0.01);        ++it;    }    return 0;}

ac3

#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <sstream>#include <stack> #include <map> #include <set> #include <unordered_map>using namespace std;const int INF = 0x7fffffff;const int MIN_INF = - INF -1;typedef long long int LL;const int N  = 10005;int day24[24];int daycost;int n;struct record{    string name;    int mon;    int dd;    int hh;    int mm;    bool onoff;    int tTime;};vector<record> vr;int mon;bool cmp(record r1, record r2){    if(r1.name < r2.name)        return true;    else if(r1.name == r2.name)    {        if(r1.tTime < r2.tTime)            return true;    }    return false;}int getCost(int d,int h,int m){    double ans = 0;    for(int i=0;i<h;i++)    {        ans += day24[i] * 60;    }    ans += day24[h] * m;    ans += d * daycost;    return ans;}struct data{    int d1,h1,m1;    int d2,h2,m2;    int min;    double money;};int main(){    //freopen("in.txt","r",stdin);    daycost = 0;    for(int i=0;i<24;i++)    {        scanf("%d", &day24[i]);        daycost += day24[i] * 60;    }    scanf("%d", &n);    record rt;    string on_off;    for(int i=0;i<n;i++)    {        cin >> rt.name;        scanf("%d:%d:%d:%d", &rt.mon, &rt.dd, &rt.hh, &rt.mm);        mon = rt.mon;        cin >> on_off;        if(on_off == "on-line")        {            rt.onoff = true;        }else{            rt.onoff = false;        }        rt.tTime = rt.dd * 24 * 60 + rt.hh * 60 +  rt.mm;        vr.push_back(rt);    }    sort(vr.begin(), vr.end(),cmp);    vector<record> filter;    int index=0;    while(index < n && vr[index].onoff != true)        index ++;    filter.push_back(vr[index]);    string lastName =  vr[index].name;    bool lastOnoff = true;    for(int i = index + 1; i< n;i++)    {        if(vr[i].name == lastName)        {            if(lastOnoff)            {                   if(vr[i].onoff)                {                    filter.pop_back();                    filter.push_back(vr[i]);                }else{                    filter.push_back(vr[i]);                    lastOnoff = false;                }            }else{                if(vr[i].onoff)                {                    filter.push_back(vr[i]);                    lastOnoff = true;                }else{                }            }        }else{            if(lastOnoff)            {                if(vr[i].onoff)                {                    filter.pop_back();                    filter.push_back(vr[i]);                    lastName = vr[i].name;                    lastOnoff = true;                }else{                }            }else{                if(vr[i].onoff)                {                    filter.push_back(vr[i]);                    lastName = vr[i].name;                    lastOnoff = true;                }else{                }            }        }    }    int flen = filter.size();    if(filter[flen -1].onoff == true)    {        filter.pop_back();        flen --;    }    map<string,vector<data>> mp;    map<string,int> mpTcost;    for(int i =0;i<flen;i+=2)    {        record c1 = filter[i];        record c2 = filter[i+1];        data dt;        dt.d1 = c1.dd;        dt.h1 = c1.hh;        dt.m1 = c1.mm;        dt.d2 = c2.dd;        dt.h2 = c2.hh;        dt.m2 = c2.mm;        dt.min = c2.tTime - c1.tTime;        dt.money = getCost(c2.dd, c2.hh, c2.mm) - getCost(c1.dd, c1.hh, c1.mm);        mp[c1.name].push_back(dt);        mpTcost[c1.name] += dt.money;    }    map<string,vector<data>>::iterator it = mp.begin();    while(it != mp.end())    {        cout << it ->first << " ";        printf("%02d\n",mon);        vector<data> dt2 = it->second;        int len2 = dt2.size();        for(int i=0;i<len2;i++)        {            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n",                dt2[i].d1, dt2[i].h1, dt2[i].m1,dt2[i].d2, dt2[i].h2, dt2[i].m2,                dt2[i].min, (0.01 * dt2[i].money));        };        printf("Total amount: $%.2lf\n", 0.01 * mpTcost[it->first]);        ++it;    }    //printf("\n");    return 0;}
0 0
原创粉丝点击