Advertisement
STANAANDREY

jane street swe inter

Mar 16th, 2025
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using Fact = tuple<string, double, string>;
  4. using Query = tuple<double, string, string>;
  5. using Edge = pair<int, double>;
  6. using UMapStrToInt = unordered_map<string, int>;
  7. using Graph = vector<list<Edge>>;
  8. using Answer = pair<double, bool>;
  9.  
  10. void computeIds(const vector<Fact> &facts, UMapStrToInt &idOfUnit) {
  11.     int id = 0;
  12.     for (const Fact &fact : facts) {
  13.         string unit1 = get<0>(fact);
  14.         string unit2 = get<2>(fact);
  15.         if (!idOfUnit.count(unit1)) idOfUnit[unit1] = id++;
  16.         if (!idOfUnit.count(unit2)) idOfUnit[unit2] = id++;
  17.     }
  18. }
  19.  
  20. void parseFacts(const vector<Fact> &facts, Graph &graph, UMapStrToInt idOfUnit) {
  21.     graph.resize(idOfUnit.size());
  22.     for (const Fact &fact : facts) {
  23.         string unit1 = get<0>(fact);
  24.         string unit2 = get<2>(fact);
  25.         int id1 = idOfUnit[unit1];
  26.         int id2 = idOfUnit[unit2];
  27.         double w = get<1>(fact);
  28.         graph[id1].push_back(make_pair(id2, w));
  29.         graph[id2].push_back(make_pair(id1, 1. / w));
  30.     }
  31. }
  32.  
  33. bool dfs(const Graph &graph, int node, double curr, vector<bool> &vis, const int target, double &ans) {
  34.     if (node == target) {
  35.         ans = curr;
  36.         return true;
  37.     }
  38.     bool ok = false;
  39.     for (const Edge &e : graph[node]) {
  40.         if (vis[e.first]) {
  41.             continue;
  42.         }
  43.         vis[e.first] = true;
  44.         ok |= dfs(graph, e.first, e.second * curr, vis, target, ans);
  45.     }
  46.     return ok;
  47. }
  48.  
  49. double answerQuery(const Graph &graph, const UMapStrToInt &idOfUnit, const Query &q) {
  50.     vector<bool> vis(graph.size());
  51.     string unitFrom = get<1>(q);
  52.     string unitTo = get<2>(q);
  53.     int start = idOfUnit.at(unitFrom);
  54.     int target = idOfUnit.at(unitTo);
  55.     double ans;
  56.     bool ok = dfs(graph, start, 1, vis, target, ans);
  57.     if (!ok) {
  58.         throw runtime_error("CANT_CONVERT");
  59.     }
  60.     return get<0>(q) * ans;
  61. }
  62.  
  63. vector<Answer> answerQueries(const vector<Fact> &facts, const vector<Query> &queries) {
  64.     UMapStrToInt idOfUnit;
  65.     computeIds(facts, idOfUnit);
  66.     Graph graph;
  67.     parseFacts(facts, graph, idOfUnit);
  68.     vector<Answer> ans;
  69.     ans.reserve(queries.size());
  70.     for (const Query &q: queries) {
  71.         try {
  72.             ans.emplace_back(answerQuery(graph, idOfUnit, q), true);
  73.         } catch (const runtime_error& re) {
  74.             //cerr << re.what() << endl;
  75.             ans.emplace_back(.0, false);
  76.         }
  77.     }
  78.     return ans;
  79. }
  80.  
  81. int main() {
  82.     const vector<Fact> facts = {
  83.         make_tuple("m", 3.28, "ft"),
  84.         make_tuple("ft", 12, "in"),
  85.         make_tuple("hr", 60, "min"),
  86.         make_tuple("min", 60, "sec"),
  87.     };
  88.     const vector<Query> queries = {
  89.         make_tuple(2, "m", "in"),
  90.         make_tuple(13, "in", "m"),
  91.         make_tuple(2, "in", "hr"),
  92.     };
  93.     const auto ans = answerQueries(facts, queries);
  94.     cout << fixed << setprecision(3);
  95.     for (size_t i = 0; i < ans.size(); i++) {
  96.         auto q = queries[i];
  97.         if (ans[i].second) {
  98.             cout << get<0>(q) << get<1>(q) << " = "
  99.                 << ans[i].first << ' ' << get<2>(q) << endl;
  100.         } else {
  101.             cout << get<0>(q) << get<1>(q)
  102.              << " = CAN_NOT_CONVERT_TO " << get<2>(q) << endl;
  103.         }
  104.     }
  105.  
  106.     return 0;
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement