2013年8月12日

UVa 10138 - CDVII

#include <cstdio>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#define PB push_back
using namespace std;

struct Record {
  Record(string time, int pos, bool leave) : time(time), pos(pos), leave(leave) {}
  bool operator<(const Record & other) const {
    return time < other.time;
  }
  string time;
  int pos;
  bool leave;
};

typedef vector<Record> Records;

int main() {
  char temp[9999];
  gets(temp);
  int T;
  sscanf(temp, "%d", &T);
  gets(temp);
  while (T--) {
    gets(temp);
    int price[24];
    istringstream ss(temp);
    for (int i = 0; i < 24; i++) {
      ss >> price[i];
    }
    map<string, Records> records;
    while (gets(temp) && temp[0]) {
      char name[29], time[29], state[29];
      int pos;
      sscanf(temp, "%s%s%s%d", name, time, state, &pos);
      records[name].PB(Record(time + 3, pos, state[1] == 'x' ? true : false));
    }
    for (map<string, Records>::iterator it = records.begin(); it != records.end(); it++) {
      Records & v = it->second;
      sort(v.begin(), v.end());
      int cost = 0;
      for (int i = 1; i < v.size(); i++) {
        if (v[i].leave && !v[i - 1].leave) {
          int hours = (v[i - 1].time[3] - '0') * 10 + v[i - 1].time[4] - '0';
          cost += (v[i].pos > v[i - 1].pos ? (v[i].pos - v[i - 1].pos) : (v[i - 1].pos - v[i].pos)) * price[hours] + 100;
        }
      }
      if (cost) {
        printf("%s $%.2lf\n", (it->first).c_str(), cost / 100.0 + 2);
      }
    }
    if (T) {
      puts("");
    }
  }
  return 0;
}

沒有留言:

張貼留言