#include <iostream> #include <vector> #include <list> #include <algorithm> #include <sstream> #include <map> template<class T> std::ostream& operator<<(std::ostream& out, std::vector <T> v) { out << '{'; for (auto& e : v) out << e << " "; return out << '}'; } void testing(); void execute(std::istream& in); void executeCase(std::istream& in); enum class Player { player01, player02 }; std::ostream& operator<<(std::ostream& out, Player v) { switch (v) { case Player::player01: return out << "Player01"; case Player::player02: return out << "Player02"; }} int main() { // testing(); std::istringstream inputStream( "3\n" "5 5\n" "5 > 5\n" "1 > 5\n" "3 > 5\n" "4 > 5\n" "2 > 5\n" "2 2\n" "1 > 1\n" "1 > 2\n" "1 1\n" "1 < 1\n" ); // execute(inputStream); execute(std::cin); } void testing() { } void execute(std::istream& in) { int cases; // [1 : 20] in >> cases; while(cases-- > 0) executeCase(in); } typedef std::vector<int> WLRow; typedef WLRow Winners; typedef WLRow Loosers; class Deck { public: Deck(const int name) : name(name), isLooser(false), loosesTo({ }), isWinner(false), winsOver({ }) { } Deck() : Deck(0) { throw "Deck() is not supposed to be called"; } const int name; bool isLooser; Winners loosesTo; bool isWinner; Loosers winsOver; public: bool isNeutral()const{return not (isLooser or isWinner);} }; typedef std::map<int, Deck> Decks; typedef const std::pair<int, Deck>& DeckRep; std::ostream& operator<<(std::ostream& out, const Deck& d) { return out << '{' << d.name << " : " << (d.isWinner ? "W" : "x") << d.winsOver << (d.isLooser ? " L" : " x") << d.loosesTo << '}'; } template<class K, class V> std::ostream& operator<<(std::ostream& out, const std::map<K,V>& map) { out << "{\n"; for (const std::pair<K, V>& pair : map) out << "{" << pair.first << ":" << pair.second << "}\n"; return out << "}"; }; // important !! indexing from 1 (not 0) for simple reason - decs are counted from 1 not 0 // row 0 contains 1..numOfDecs numbers AND the numOfDecs on [0] position - indicates how many decs are not discarded std::string processCase(Decks&, Decks&); Decks createUnknownDecks(int size); void removeDeck(Decks& owner, Decks& oponent, int deckToRemove); bool removeNonAgresorsAgresor(Decks& activePlayer, Decks& target); bool removeNeutral(Decks& activePlayer, Decks& target); bool removeAgresor(Decks& activePlayer, Decks& target); void chooseAndRemoveDeck(Decks& player1, Decks& player2); void executeCase(std::istream& in) { int numOfDecs; // [1 : 100 000] int nonDrawPairs; // [0 : 200 000] in >> numOfDecs >> nonDrawPairs; int p1deck; // [1 : numOfDecs] int p2deck; // [1 : numOfDecs] char relation; // [<>] Decks player1 = createUnknownDecks(numOfDecs); Decks player2 = createUnknownDecks(numOfDecs); while(nonDrawPairs-- > 0) { in >> p1deck >> relation >> p2deck; // eg. "1 > 3" if (relation == '>') { player1[p1deck].isWinner = true; player1[p1deck].winsOver.push_back(p2deck); player2[p2deck].isLooser = true; player2[p2deck].loosesTo.push_back(p1deck); } else { player2[p2deck].isWinner = true; player2[p2deck].winsOver.push_back(p1deck); player1[p1deck].isLooser = true; player1[p1deck].loosesTo.push_back(p2deck); } } // std::cout << player1<<'\n'; // std::cout << player2<<'\n'; std::cout << processCase(player1, player2) << '\n'; } Decks createUnknownDecks(int size) { Decks decks { }; ++size; for (int i = 1; i < size; ++i) decks.emplace(i, Deck(i)); return std::move(decks); } bool areAllNodesAttackers(Decks player) { return std::all_of(player.begin(), player.end(), [](DeckRep rep){return rep.second.isWinner;}); } bool areAnyNodesAttackers(Decks player) { return std::any_of(player.begin(), player.end(), [](DeckRep rep){return rep.second.isWinner;}); } bool areAnyNodeNeutral(Decks player) { return std::any_of(player.begin(), player.end(), [](DeckRep rep){return rep.second.isNeutral();}); } bool areNoNodesAttackers(Decks player) { return not areAnyNodesAttackers(player); } bool canWin(Decks& player1, Decks& player2) { if (areAllNodesAttackers(player1) and areNoNodesAttackers(player2) and areAnyNodeNeutral(player2)) return true; return false; // TODO implement } //bool canDraw(Decks& player1, Decks& player2) //{ //// if(areAnyNodeNeutral(player2)) return true; // return false; // TODO implement //} bool canPullADraw = false; std::string processCase(Decks& player1, Decks& player2) { canPullADraw = false; static const std::string p1Wins = "WYGRANA"; static const std::string p1Loos = "PRZEGRANA"; static const std::string draw = "REMIS"; while(player1.size()>1) { if (canWin(player1, player2)) return p1Wins; // if (canDraw(player1, player2)) return draw; if (canWin(player2, player1)) return p1Loos; chooseAndRemoveDeck(player1, player2); chooseAndRemoveDeck(player2, player1); } Deck& finalDeck = player1.begin()->second; if (finalDeck.isNeutral()) return draw; if (finalDeck.isWinner) return p1Wins; if (canPullADraw) return draw; else return p1Loos; } void chooseAndRemoveDeck(Decks& player1, Decks& player2) { if (removeNonAgresorsAgresor(player1, player2)); else if (removeNeutral(player1, player2)); // remove inCycle ??? else if (removeAgresor(player1, player2)); else removeDeck(player2, player1, player2.begin()->first); } bool removeAgresor(Decks& activePlayer, Decks& target) { for(DeckRep rep : target) { if(rep.second.isWinner) { int deckToRemove = rep.second.loosesTo[0]; removeDeck(target, activePlayer, deckToRemove); return true; } } return false; } bool removeNeutral(Decks& activePlayer, Decks& target) { for(DeckRep rep : target) { if(rep.second.isNeutral()) { int deckToRemove = rep.second.loosesTo[0]; return true; canPullADraw = true; removeDeck(target, activePlayer, deckToRemove); } } return false; } bool removeNonAgresorsAgresor(Decks& activePlayer, Decks& target) { for(DeckRep rep : activePlayer) { if((not rep.second.isWinner) and (rep.second.isLooser)) { int deckToRemove = rep.second.loosesTo[0]; removeDeck(target, activePlayer, deckToRemove); return true; } } return false; } void removeDeck(Decks& owner, Decks& oponent, int deckToRemove) { Deck& deck = owner[deckToRemove]; for(int d:deck.winsOver) { oponent[d].loosesTo.erase(std::find(oponent[d].loosesTo.begin(), oponent[d].loosesTo.end(), deck.name)); if (oponent[d].loosesTo.size() == 0) oponent[d].isLooser = false; } for(int d:deck.loosesTo) { oponent[d].winsOver.erase(std::find(oponent[d].winsOver.begin(), oponent[d].winsOver.end(), deck.name)); if (oponent[d].winsOver.size() == 0) oponent[d].isWinner = false; } owner.erase(deckToRemove); }
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | #include <iostream> #include <vector> #include <list> #include <algorithm> #include <sstream> #include <map> template<class T> std::ostream& operator<<(std::ostream& out, std::vector <T> v) { out << '{'; for (auto& e : v) out << e << " "; return out << '}'; } void testing(); void execute(std::istream& in); void executeCase(std::istream& in); enum class Player { player01, player02 }; std::ostream& operator<<(std::ostream& out, Player v) { switch (v) { case Player::player01: return out << "Player01"; case Player::player02: return out << "Player02"; }} int main() { // testing(); std::istringstream inputStream( "3\n" "5 5\n" "5 > 5\n" "1 > 5\n" "3 > 5\n" "4 > 5\n" "2 > 5\n" "2 2\n" "1 > 1\n" "1 > 2\n" "1 1\n" "1 < 1\n" ); // execute(inputStream); execute(std::cin); } void testing() { } void execute(std::istream& in) { int cases; // [1 : 20] in >> cases; while(cases-- > 0) executeCase(in); } typedef std::vector<int> WLRow; typedef WLRow Winners; typedef WLRow Loosers; class Deck { public: Deck(const int name) : name(name), isLooser(false), loosesTo({ }), isWinner(false), winsOver({ }) { } Deck() : Deck(0) { throw "Deck() is not supposed to be called"; } const int name; bool isLooser; Winners loosesTo; bool isWinner; Loosers winsOver; public: bool isNeutral()const{return not (isLooser or isWinner);} }; typedef std::map<int, Deck> Decks; typedef const std::pair<int, Deck>& DeckRep; std::ostream& operator<<(std::ostream& out, const Deck& d) { return out << '{' << d.name << " : " << (d.isWinner ? "W" : "x") << d.winsOver << (d.isLooser ? " L" : " x") << d.loosesTo << '}'; } template<class K, class V> std::ostream& operator<<(std::ostream& out, const std::map<K,V>& map) { out << "{\n"; for (const std::pair<K, V>& pair : map) out << "{" << pair.first << ":" << pair.second << "}\n"; return out << "}"; }; // important !! indexing from 1 (not 0) for simple reason - decs are counted from 1 not 0 // row 0 contains 1..numOfDecs numbers AND the numOfDecs on [0] position - indicates how many decs are not discarded std::string processCase(Decks&, Decks&); Decks createUnknownDecks(int size); void removeDeck(Decks& owner, Decks& oponent, int deckToRemove); bool removeNonAgresorsAgresor(Decks& activePlayer, Decks& target); bool removeNeutral(Decks& activePlayer, Decks& target); bool removeAgresor(Decks& activePlayer, Decks& target); void chooseAndRemoveDeck(Decks& player1, Decks& player2); void executeCase(std::istream& in) { int numOfDecs; // [1 : 100 000] int nonDrawPairs; // [0 : 200 000] in >> numOfDecs >> nonDrawPairs; int p1deck; // [1 : numOfDecs] int p2deck; // [1 : numOfDecs] char relation; // [<>] Decks player1 = createUnknownDecks(numOfDecs); Decks player2 = createUnknownDecks(numOfDecs); while(nonDrawPairs-- > 0) { in >> p1deck >> relation >> p2deck; // eg. "1 > 3" if (relation == '>') { player1[p1deck].isWinner = true; player1[p1deck].winsOver.push_back(p2deck); player2[p2deck].isLooser = true; player2[p2deck].loosesTo.push_back(p1deck); } else { player2[p2deck].isWinner = true; player2[p2deck].winsOver.push_back(p1deck); player1[p1deck].isLooser = true; player1[p1deck].loosesTo.push_back(p2deck); } } // std::cout << player1<<'\n'; // std::cout << player2<<'\n'; std::cout << processCase(player1, player2) << '\n'; } Decks createUnknownDecks(int size) { Decks decks { }; ++size; for (int i = 1; i < size; ++i) decks.emplace(i, Deck(i)); return std::move(decks); } bool areAllNodesAttackers(Decks player) { return std::all_of(player.begin(), player.end(), [](DeckRep rep){return rep.second.isWinner;}); } bool areAnyNodesAttackers(Decks player) { return std::any_of(player.begin(), player.end(), [](DeckRep rep){return rep.second.isWinner;}); } bool areAnyNodeNeutral(Decks player) { return std::any_of(player.begin(), player.end(), [](DeckRep rep){return rep.second.isNeutral();}); } bool areNoNodesAttackers(Decks player) { return not areAnyNodesAttackers(player); } bool canWin(Decks& player1, Decks& player2) { if (areAllNodesAttackers(player1) and areNoNodesAttackers(player2) and areAnyNodeNeutral(player2)) return true; return false; // TODO implement } //bool canDraw(Decks& player1, Decks& player2) //{ //// if(areAnyNodeNeutral(player2)) return true; // return false; // TODO implement //} bool canPullADraw = false; std::string processCase(Decks& player1, Decks& player2) { canPullADraw = false; static const std::string p1Wins = "WYGRANA"; static const std::string p1Loos = "PRZEGRANA"; static const std::string draw = "REMIS"; while(player1.size()>1) { if (canWin(player1, player2)) return p1Wins; // if (canDraw(player1, player2)) return draw; if (canWin(player2, player1)) return p1Loos; chooseAndRemoveDeck(player1, player2); chooseAndRemoveDeck(player2, player1); } Deck& finalDeck = player1.begin()->second; if (finalDeck.isNeutral()) return draw; if (finalDeck.isWinner) return p1Wins; if (canPullADraw) return draw; else return p1Loos; } void chooseAndRemoveDeck(Decks& player1, Decks& player2) { if (removeNonAgresorsAgresor(player1, player2)); else if (removeNeutral(player1, player2)); // remove inCycle ??? else if (removeAgresor(player1, player2)); else removeDeck(player2, player1, player2.begin()->first); } bool removeAgresor(Decks& activePlayer, Decks& target) { for(DeckRep rep : target) { if(rep.second.isWinner) { int deckToRemove = rep.second.loosesTo[0]; removeDeck(target, activePlayer, deckToRemove); return true; } } return false; } bool removeNeutral(Decks& activePlayer, Decks& target) { for(DeckRep rep : target) { if(rep.second.isNeutral()) { int deckToRemove = rep.second.loosesTo[0]; return true; canPullADraw = true; removeDeck(target, activePlayer, deckToRemove); } } return false; } bool removeNonAgresorsAgresor(Decks& activePlayer, Decks& target) { for(DeckRep rep : activePlayer) { if((not rep.second.isWinner) and (rep.second.isLooser)) { int deckToRemove = rep.second.loosesTo[0]; removeDeck(target, activePlayer, deckToRemove); return true; } } return false; } void removeDeck(Decks& owner, Decks& oponent, int deckToRemove) { Deck& deck = owner[deckToRemove]; for(int d:deck.winsOver) { oponent[d].loosesTo.erase(std::find(oponent[d].loosesTo.begin(), oponent[d].loosesTo.end(), deck.name)); if (oponent[d].loosesTo.size() == 0) oponent[d].isLooser = false; } for(int d:deck.loosesTo) { oponent[d].winsOver.erase(std::find(oponent[d].winsOver.begin(), oponent[d].winsOver.end(), deck.name)); if (oponent[d].winsOver.size() == 0) oponent[d].isWinner = false; } owner.erase(deckToRemove); } |