TOJ-1303 CDVII

Roman roads are famous for their longevity and sound engineering. Unfortunately, sound engineering does not come cheap, and a number of neo-Caesars have decided to recover the costs through automated tolling.

A particular toll highway, the CDVII, has a fare structure that works as follows: travel on the road costs a certain amount per km travelled, depending on the time of day when the travel begins. Cameras at every entrance and every exit capture the license numbers of all cars entering and leaving. Every calendar month, a bill is sent to the registered owner for each km travelled (at a rate determined by the time of day), plus one dollar per trip, plus a two dollar account charge. Your job is to prepare the bill for one month, given a set of license plate photos.

Input

Standard input has two parts: the fare structure, and the license photos. The fare structure consists of a line with 24 non-negative integers denoting the toll (cents/km) from 00:00 - 00:59, the toll from 01:00 - 00:59, and so on for each hour in the day. Each photo record consists of the license number of the vehicle (up to 20 alphanumeric characters), the time and date (mm:dd:hh:mm), the word "enter" or "exit", and the location of the entrance or exit (in km from one end of the highway). All dates will be within a single month. Each "enter" record is paired with the chronologically next record for the same vehicle provided it is an "exit" record. "enter" records that are not paired with an "exit" record are ignored, as are "exit" records not paired with an "enter" record. You may assume that no two records for the same vehicle have the same time. Times are recorded using a 24-hour clock. There are not more than 1000 photo records.

Output

Print a line for each vehicle indicating the license number, and the total bill, in alphabetical order by license number.

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
ABCD123 01:01:06:01 enter 17
765DEF 01:01:07:00 exit 95
ABCD123 01:01:08:03 exit 95
765DEF 01:01:05:59 enter 17

Sample Output

765DEF $10.80
ABCD123 $18.60


摘一下POJ同仁的细节分析:
(1)按照时间排序;(我起初按照location排序,忽略了一个license可能有多个trip,显然错了)
(2)把相连的enter和exit看成一次trip,其余的enter和exit ignore;
(3)每次trip的单价取enter的小时对应的单价值;
(4)每次trip的费用是两部分:固定的100美分s和abs(exit的location-enter的location)*单价;
(5)最后,如果前面的总费用是0,表示没有trip,那么总的费用是0;(这个不用输出,他的分析还是有些问题)
      否则,总的费用还需要再加上200美分。

#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <cstdlib>
    
using namespace std;
struct license_photos{
    string license;
    char status;
    int time;
    int loc;
    int fare;
};
bool cmp(license_photos x, license_photos y)
{
    if (x.license != y.license)
        return x.license < y.license;
    return x.time < y.time;
}
void bill(string license, int fee)
{
    if (fee > 0)
    {
        fee += 200;
        cout.precision(2);
        cout.setf(ios::fixed | ios::showpoint);
        cout << license << " $"<< (fee / 100.00) << endl;
    }
}
void ds(license_photos photos[], int c){
    for(int i=0;i<c;i++){
        cout << photos[i].license<<" "<<photos[i].status<<" "<<photos[i].time<<" "<<photos[i].loc<<" "<<photos[i].fare<<endl;
    }
}
void photodeal(license_photos photos[], int c){
    sort(photos, photos + c, cmp);
    //ds(photos,c);
    int current = 0;
    while (current < c)
    {
        string license = photos[current].license;
        int fee = 0;
        bool entered = false;
        
        // 找到一辆车的进入/离开记录对。不成对记录忽略。
        for (; current < c && (photos[current].license == license); current++)
        {
            // 找到一条进入记录。
            if (!entered)
            {
                if (photos[current].status == 'e')
                    entered = true;
                else
                    continue;
            }
            
            if (entered)
            {
                if (photos[current].status == 'l')
                {
                    entered = false;
                    fee += 100;
                    fee += photos[current - 1].fare * 
                        abs(photos[current].loc - photos[current - 1].loc);
                }
                else
                    continue;
            }
        }
        
        bill(license, fee);
    }
}

int main(){
    int toll[24];
    license_photos photos[1001];
    int i,c;
    string line;
    for(i=0;i<24;i++){
        cin>>toll[i];
    }
    cin.ignore();
    c = 0;
    while (getline(cin, line), line.length() > 0)
    {
        istringstream iss(line);
        string date;
        string state;    
        iss >> photos[c].license >> date;
        //根据时间得收费标准 
        photos[c].time = atoi(date.substr(3, 2).data()) * 24 * 60;
        photos[c].time += atoi(date.substr(6, 2).data()) * 60;
        photos[c].time += atoi(date.substr(9, 2).data());
        photos[c].fare = toll[atoi(date.substr(6, 2).data())];                        
        iss >> state >> photos[c].loc;
        if(state[1] == 'n') 
            photos[c].status = 'e';
        else if(state[1] == 'x') 
            photos[c].status = 'l';
            
        c++;
    }
    photodeal(photos,c);
    return 0;
}

sstream真的很有用,要好好学习一下。

posted @ 2017-01-19 15:50  DGSX  阅读(155)  评论(0编辑  收藏  举报