Thursday, April 23, 2015

UVa 11816 - HST

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
using namespace std;

#define MAX 100000

string category_name[MAX];
long long pst[MAX], gst[MAX], hst[MAX];
map<string, int> id;

inline long long getCents(long long tax) {
	return tax / 10000 + (tax % 10000 >= 5000 ? 1 : 0);
}

// include <algorithm> and <string>
void removeCharFromString(string & str, char ch) {
	str.erase(std::remove(str.begin(), str.end(), ch), str.end());
}

void removeDot(string & str) {
	removeCharFromString(str, '%');
	removeCharFromString(str, '$');
	int pos = str.find('.');
	if (pos == string::npos || pos == str.length() - 1)
		str = str + "00";
	else if (pos == str.length() - 2)
		str = str + "0";
	removeCharFromString(str, '.');
}

int main() {
	int cases;
	for (cin >> cases; cases; cases--) {
		int categories, purchases;
		string category_name;
		id.clear();
		cin >> categories >> purchases;
		for (int i = 0; i < categories; i++) {
			string pst_s, gst_s, hst_s;
			cin >> category_name >> pst_s >> gst_s >> hst_s;
			removeDot(pst_s);
			removeDot(gst_s);
			removeDot(hst_s);
			pst[i] = strtoll(pst_s.c_str(), NULL, 10);
			gst[i] = strtoll(gst_s.c_str(), NULL, 10);
			hst[i] = strtoll(hst_s.c_str(), NULL, 10);
			id[category_name] = i;
		}
		long long total_hst = 0, total_pg = 0;
		for (int i = 0; i < purchases; i++) {
			string price;
			cin >> category_name >> price;
			removeDot(price);

			long long cents = strtol(price.c_str(), NULL, 10);
			int category_id = id[category_name];

			total_hst += getCents(hst[category_id] * cents);
			total_pg += getCents(pst[category_id] * cents) + getCents(gst[category_id] * cents);
		}

		double sol = (total_hst - total_pg) / 100.0;
		cout << setprecision(2) << fixed << sol << endl;
	}
	return 0;
}

No comments:

Post a Comment