【1016 25 排序】 Phone Bills

传送门

题意

给定 \(24\) 小时每个时间段的话费金额,给定 \(n\) 个电话订单,保证一个人的订单都是同一个月的,电话开始是 \(on-line\) ,结束是 \(off-line\) ,计算有效的电话订单时间,并求出话费,记录配对的订单,输出按照用户 \(id\) 字典序第一关键字,时间第二关键字

数据范围

\(1\leq n\leq 1000\)

题解

  • 结构体存储信息
    • 分别存储月、天、小时、分钟
    • 将天、小时转化为分钟后的绝对时间,便于按照时间进行排序以及计算通话时间
    • 开始和结束的标志
  • 费用计算都计算从 \(00:00:00\) 开始的费用值,然后作差即可
  • 钱是美分 ,需要转化为美元,即 \(/100\)
  • 利用 mapkey 作为名字可以直接对名字按照字母序进行排序

Code

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second

struct Bill {
	int month, day, hour, minute;
	int status;
	int time;
	Bill(int mon, int d, int h, int m, int st) : month(mon), day(d), hour(h), minute(m), status(st) {
		time = day * 24 * 60 + hour * 60 + minute;
	} 
};

vector<double> fee(25);

double Cal(Bill x) {
	double res = 0;
	res += x.day * 60 * fee[24] + fee[x.hour] * x.minute;
	for (int i = 0; i < x.hour; i++) {
		res += fee[i] * 60;
	}
	return res;
}

int main() {
	for (int i = 0; i < 24; i++) {
		cin >> fee[i];
		fee[i] /= 100.0;
		fee[24] += fee[i];
	}
	int n; cin >> n;
	map<string, vector<Bill>> customers;
	for (int i = 0; i < n; i++) {
		string name; cin >> name;
		char c;
		int mon, d, h, m, st; cin >> mon >> c >> d >> c >> h >> c >> m;
		string tmp; cin >> tmp;
		customers[name].push_back(Bill(mon, d, h, m, tmp == "on-line" ? 0 : 1));
	}
	
	cout << fixed << setprecision(2);
	for (auto& c : customers) {
		string name = c.fi;
		auto& bills = c.se;

		sort(bills.begin(), bills.end(), 
			 [](const Bill& a, const Bill& b){ return a.time < b.time; });
		double tot = 0;
		bool out = 0;
		for (int j = 1; j < bills.size(); j++) {
			const auto& now = bills[j], pre = bills[j - 1];
			if (pre.status == 0 && now.status == 1) {
				if (not out) {
					cout << name << ' ' << setfill('0') << setw(2) << now.month << endl;
					out = 1;
				}
				double cost = Cal(now) - Cal(pre);
				cout << setfill('0') << setw(2) << pre.day << ':' 
				<< setfill('0') << setw(2) << pre.hour << ':'
				<< setfill('0') << setw(2) << pre.minute << ' '
				<< setfill('0') << setw(2) << now.day << ':' 
				<< setfill('0') << setw(2) << now.hour << ':'
				<< setfill('0') << setw(2) << now.minute << ' '
				<< now.time - pre.time<< " $" << cost << endl;
				tot += cost;
				j++;
			}
		}
		if (out) cout << "Total amount: $" << tot << endl;
	}
}
posted @ 2021-01-28 01:50  Hyx'  阅读(37)  评论(0)    收藏  举报