#include <iostream> #include <vector> #include <algorithm> struct Soldier { int meleeSkill = 0; int rangeSkill = 0; int collisions = 0; }; bool isCollision(Soldier *&s1, Soldier *&s2) { return s1->meleeSkill < s2->meleeSkill && s1->rangeSkill > s2->rangeSkill; } bool sortByCollisions(Soldier *&s1, Soldier *&s2) { return s1->collisions < s2->collisions; } int findCountOfDeclaredCollisions(std::vector<Soldier*> &soldiers, int k, int collisions) { unsigned long n = soldiers.size(); std::string bitmask(k, 1); bitmask.resize(n, 0); int howMuch = 0; do { int collisionCount = 0; for (int x = 0; x < n; x++) { if (!bitmask[x]) { continue; } Soldier *&s1 = soldiers[x]; for (int y = x + 1; y < n; y++) { if (x == y || !bitmask[y]) { continue; } Soldier *&s2 = soldiers[y]; if (isCollision(s1, s2)) { collisionCount++; } if (isCollision(s2, s1)) { collisionCount++; } } } if (collisionCount == collisions) { howMuch++; } } while (std::prev_permutation(bitmask.begin(), bitmask.end())); return howMuch; } int main() { int n, a; std::cin >> n; std::vector<Soldier*> soldiers(static_cast<unsigned long>(n)); for (int i = 0; i < n; i++) { std::cin >> a; soldiers[i] = new Soldier{i, a}; } for (auto &s1 : soldiers) { for (auto s2 : soldiers) { if (isCollision(s1, s2)) { s1->collisions++; } } } std::sort(soldiers.begin(), soldiers.end(), sortByCollisions); for (int k = 1; k <= n; k++) { int collisions = 0; for (int i = 0; i < k; i++) { collisions += soldiers[i]->collisions; } std::cout << collisions << " " << findCountOfDeclaredCollisions(soldiers, k, collisions) << std::endl; } }
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 | #include <iostream> #include <vector> #include <algorithm> struct Soldier { int meleeSkill = 0; int rangeSkill = 0; int collisions = 0; }; bool isCollision(Soldier *&s1, Soldier *&s2) { return s1->meleeSkill < s2->meleeSkill && s1->rangeSkill > s2->rangeSkill; } bool sortByCollisions(Soldier *&s1, Soldier *&s2) { return s1->collisions < s2->collisions; } int findCountOfDeclaredCollisions(std::vector<Soldier*> &soldiers, int k, int collisions) { unsigned long n = soldiers.size(); std::string bitmask(k, 1); bitmask.resize(n, 0); int howMuch = 0; do { int collisionCount = 0; for (int x = 0; x < n; x++) { if (!bitmask[x]) { continue; } Soldier *&s1 = soldiers[x]; for (int y = x + 1; y < n; y++) { if (x == y || !bitmask[y]) { continue; } Soldier *&s2 = soldiers[y]; if (isCollision(s1, s2)) { collisionCount++; } if (isCollision(s2, s1)) { collisionCount++; } } } if (collisionCount == collisions) { howMuch++; } } while (std::prev_permutation(bitmask.begin(), bitmask.end())); return howMuch; } int main() { int n, a; std::cin >> n; std::vector<Soldier*> soldiers(static_cast<unsigned long>(n)); for (int i = 0; i < n; i++) { std::cin >> a; soldiers[i] = new Soldier{i, a}; } for (auto &s1 : soldiers) { for (auto s2 : soldiers) { if (isCollision(s1, s2)) { s1->collisions++; } } } std::sort(soldiers.begin(), soldiers.end(), sortByCollisions); for (int k = 1; k <= n; k++) { int collisions = 0; for (int i = 0; i < k; i++) { collisions += soldiers[i]->collisions; } std::cout << collisions << " " << findCountOfDeclaredCollisions(soldiers, k, collisions) << std::endl; } } |