您的位置:首页 > 其它

UVa Problem Solution: 10138 - CDVII

2008-11-21 13:33 477 查看
Sort the the photo records according to license number, then time. If a license has no cost, do not output it.

Code:
/*************************************************************************
* Copyright (C) 2008 by liukaipeng *
* liukaipeng at gmail dot com *
*************************************************************************/

/* @JUDGE_ID 00000 10138 C++ "CDVII" */

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int const linesize = 256;
int const recordcount = 1000;

struct record
{
char license[21];
int month;
int day;
int hour;
int minute;
char status[6];
int location;
};

struct recordcmp
{
bool operator()(record const& r1, record const& r2)
{
int c = strcmp(r1.license, r2.license);
if (c < 0) return true;
if (c > 0) return false;
if (r1.day < r2.day) return true;
if (r1.day > r2.day) return false;
if (r1.hour < r2.hour) return true;
if (r1.hour > r2.hour) return false;
if (r1.minute < r2.minute) return true;
if (r1.minute > r2.minute) return false;
return false;
}
};

struct bill
{
char *license;
int cost;
};

int get_bills(int *fares, record *records, int nrecords, bill *bills)
{
sort(records, records + nrecords, recordcmp());
int nbills = 0;
for (int i = 0; i < nrecords;) {
char *license = records[i].license;
bool entered = false;
int cost = 0;
for (; i < nrecords && strcmp(license, records[i].license) == 0; ++i) {
if (!entered) {
if (records[i].status[1] == 'n') {
entered = true;
} else continue;
}
if (entered) {
if (records[i].status[1] == 'x') {
entered = false;
cost += 100 + fares[records[i-1].hour] *
abs(records[i].location - records[i-1].location);
} else continue;
}
}
if (cost > 0) {
cost += 200;
bills[nbills].license = license;
bills[nbills].cost = cost;
++nbills;
}
}
return nbills;
}

int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
freopen((string(argv[0]) + ".in").c_str(), "r", stdin);
freopen((string(argv[0]) + ".out").c_str(), "w", stdout);
#endif

int ncases;
scanf("%d/n/n", &ncases);
while (ncases-- > 0) {
char buf[linesize];
fgets(buf, linesize, stdin);
int fares[24];
sscanf(buf, "%d %d %d %d %d %d %d %d %d %d %d "
"%d %d %d %d %d %d %d %d %d %d %d %d %d/n",
&fares[0], &fares[1], &fares[2], &fares[3], &fares[4],
&fares[5], &fares[6], &fares[7], &fares[8], &fares[9],
&fares[10], &fares[11], &fares[12], &fares[13], &fares[14],
&fares[15], &fares[16], &fares[17], &fares[18], &fares[19],
&fares[20], &fares[21], &fares[22], &fares[23]);
record records[recordcount];
int nrecords = 0;
for (; fgets(buf, linesize, stdin) && buf[0] != '/n'; ++nrecords) {
sscanf(buf, "%s %2d:%2d:%2d:%2d %s %d/n",
records[nrecords].license, &records[nrecords].month,
&records[nrecords].day, &records[nrecords].hour,
&records[nrecords].minute, records[nrecords].status,
&records[nrecords].location);
}
bill bills[recordcount];
int nbills = get_bills(fares, records, nrecords, bills);
for (int i = 0; i < nbills; ++i) {
printf("%s $%d.%02d/n", bills[i].license, bills[i].cost / 100,
bills[i].cost % 100);
}
if (ncases > 0) putchar('/n');
}

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