#include <iostream> #include <map> #include <vector> #include <set> #include <algorithm> struct Substance { unsigned int id; unsigned long int quantity; }; struct Reaction { Reaction(unsigned int first, unsigned int second): firstSubstance(std::min(first, second)), secondSubstance(std::max(first, second)) {}; unsigned int firstSubstance; unsigned int secondSubstance; }; struct Step { unsigned int fromVial; unsigned int toVial; }; struct CompareById { bool operator()(const Substance& a, const Substance& b) { return a.id < b.id; } }; struct CompareReactions { bool operator()(const Reaction& a, const Reaction& b) { return (a.firstSubstance < b.firstSubstance) or ((a.firstSubstance == b.firstSubstance) and (a.secondSubstance < b.secondSubstance)); } }; std::map< Reaction, unsigned int, CompareReactions > priorities; Substance substance[200001]; Step step[200001]; std::set< Substance, CompareById > vial[200001]; struct CompareReactionPriorities { bool operator()(const Reaction& a, const Reaction& b) { return priorities[a] < priorities[b]; } } compareRP; unsigned long long int mixVials(unsigned int from, unsigned int to) { std::vector<Reaction> actualReactions; std::vector<bool> inversions; unsigned long long int result = 0; std::set< Substance, CompareById >::iterator firstIt; std::set< Substance, CompareById >::iterator secondIt; for (firstIt=vial[from].begin(); firstIt!=vial[from].end(); ++firstIt) { if (firstIt->quantity > 0) { for (secondIt=vial[to].begin(); secondIt!= vial[to].end(); ++secondIt) { if (secondIt->quantity > 0) { Reaction toReact(firstIt->id, secondIt->id); if (priorities.find(toReact) != priorities.end()) { actualReactions.push_back(toReact); inversions.push_back(firstIt->id != toReact.firstSubstance); } } } } } std::sort(actualReactions.begin(), actualReactions.end(), compareRP); for(unsigned int i=0; i<actualReactions.size(); ++i) { Substance first = {actualReactions[i].firstSubstance, 0}; Substance second = {actualReactions[i].secondSubstance, 0}; if (inversions[i]) { firstIt = vial[from].find(second); secondIt = vial[to].find(first); } else { firstIt = vial[from].find(first); secondIt = vial[to].find(second); } if ((firstIt != vial[from].end()) and (secondIt != vial[to].end()) and (firstIt->quantity >0) and (secondIt->quantity > 0)) { unsigned long int newResidue = std::min(firstIt->quantity, secondIt->quantity); result += (2*newResidue); Substance firstChanged = {firstIt->id, firstIt->quantity - newResidue}; Substance secondChanged = {secondIt->id, secondIt->quantity - newResidue}; vial[to].erase(secondIt); if (secondChanged.quantity > 0) vial[to].insert(secondChanged); vial[from].erase(firstIt); if (firstChanged.quantity > 0) vial[from].insert(firstChanged); } } for (firstIt=vial[from].begin(); firstIt!=vial[from].end(); ++firstIt) { if (firstIt->quantity > 0) vial[to].insert(*firstIt); } return result; }; int main() { unsigned int substances; std::cin >> substances; unsigned int steps; std::cin >> steps; unsigned int reactions; std::cin >> reactions; for (unsigned int i=1; i<=substances; ++i) { substance[i].id = i; std::cin >> substance[i].quantity; vial[i].insert(substance[i]); } for (unsigned int i=0; i<steps; ++i) { std::cin >> step[i].fromVial; std::cin >> step[i].toVial; } for (unsigned int i=0; i<reactions; ++i) { unsigned int first; std::cin >> first; unsigned int second; std::cin >> second; Reaction toAdd(first, second); priorities[toAdd] = i; } unsigned long long int residue = 0; for (unsigned int i=0; i<steps; ++i) { residue += mixVials(step[i].fromVial, step[i].toVial); } std::cout << residue; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | #include <iostream> #include <map> #include <vector> #include <set> #include <algorithm> struct Substance { unsigned int id; unsigned long int quantity; }; struct Reaction { Reaction(unsigned int first, unsigned int second): firstSubstance(std::min(first, second)), secondSubstance(std::max(first, second)) {}; unsigned int firstSubstance; unsigned int secondSubstance; }; struct Step { unsigned int fromVial; unsigned int toVial; }; struct CompareById { bool operator()(const Substance& a, const Substance& b) { return a.id < b.id; } }; struct CompareReactions { bool operator()(const Reaction& a, const Reaction& b) { return (a.firstSubstance < b.firstSubstance) or ((a.firstSubstance == b.firstSubstance) and (a.secondSubstance < b.secondSubstance)); } }; std::map< Reaction, unsigned int, CompareReactions > priorities; Substance substance[200001]; Step step[200001]; std::set< Substance, CompareById > vial[200001]; struct CompareReactionPriorities { bool operator()(const Reaction& a, const Reaction& b) { return priorities[a] < priorities[b]; } } compareRP; unsigned long long int mixVials(unsigned int from, unsigned int to) { std::vector<Reaction> actualReactions; std::vector<bool> inversions; unsigned long long int result = 0; std::set< Substance, CompareById >::iterator firstIt; std::set< Substance, CompareById >::iterator secondIt; for (firstIt=vial[from].begin(); firstIt!=vial[from].end(); ++firstIt) { if (firstIt->quantity > 0) { for (secondIt=vial[to].begin(); secondIt!= vial[to].end(); ++secondIt) { if (secondIt->quantity > 0) { Reaction toReact(firstIt->id, secondIt->id); if (priorities.find(toReact) != priorities.end()) { actualReactions.push_back(toReact); inversions.push_back(firstIt->id != toReact.firstSubstance); } } } } } std::sort(actualReactions.begin(), actualReactions.end(), compareRP); for(unsigned int i=0; i<actualReactions.size(); ++i) { Substance first = {actualReactions[i].firstSubstance, 0}; Substance second = {actualReactions[i].secondSubstance, 0}; if (inversions[i]) { firstIt = vial[from].find(second); secondIt = vial[to].find(first); } else { firstIt = vial[from].find(first); secondIt = vial[to].find(second); } if ((firstIt != vial[from].end()) and (secondIt != vial[to].end()) and (firstIt->quantity >0) and (secondIt->quantity > 0)) { unsigned long int newResidue = std::min(firstIt->quantity, secondIt->quantity); result += (2*newResidue); Substance firstChanged = {firstIt->id, firstIt->quantity - newResidue}; Substance secondChanged = {secondIt->id, secondIt->quantity - newResidue}; vial[to].erase(secondIt); if (secondChanged.quantity > 0) vial[to].insert(secondChanged); vial[from].erase(firstIt); if (firstChanged.quantity > 0) vial[from].insert(firstChanged); } } for (firstIt=vial[from].begin(); firstIt!=vial[from].end(); ++firstIt) { if (firstIt->quantity > 0) vial[to].insert(*firstIt); } return result; }; int main() { unsigned int substances; std::cin >> substances; unsigned int steps; std::cin >> steps; unsigned int reactions; std::cin >> reactions; for (unsigned int i=1; i<=substances; ++i) { substance[i].id = i; std::cin >> substance[i].quantity; vial[i].insert(substance[i]); } for (unsigned int i=0; i<steps; ++i) { std::cin >> step[i].fromVial; std::cin >> step[i].toVial; } for (unsigned int i=0; i<reactions; ++i) { unsigned int first; std::cin >> first; unsigned int second; std::cin >> second; Reaction toAdd(first, second); priorities[toAdd] = i; } unsigned long long int residue = 0; for (unsigned int i=0; i<steps; ++i) { residue += mixVials(step[i].fromVial, step[i].toVial); } std::cout << residue; return 0; } |