#include <iostream> #include <vector> #include <map> #include <unordered_map> #include <unordered_set> using namespace std; map<int, pair<int, int>> collisions; void readDeliveries(int size, vector<pair<int, int>>& vertical, vector<pair<int, int>>& horizontal) { int type, loc, time; for (int i = 0; i < size; ++i) { cin >> type; cin >> loc; cin >> time; int key = time - loc; if (collisions.find(key) == collisions.end()) { collisions[key] = pair<int, int>(0,0); } if (type % 2 == 1) { collisions[key].first += 1; } else { collisions[key].second += 1; } } } int removeCollisions() { int deleted = 0; while (collisions.size() != 0) { pair<int, int> collision = (*collisions.begin()).second; if (collision.first == 0 || collision.second == 0) { //they won't even collide collisions.erase(collisions.begin()); } else { deleted += min(collision.first, collision.second); collisions.erase(collisions.begin()); } } return deleted; }; int main() { int size; cin >> size; vector<pair<int, int>> vertical; vector<pair<int, int>> horizontal; readDeliveries(size, vertical, horizontal); cout << removeCollisions() << endl; 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 | #include <iostream> #include <vector> #include <map> #include <unordered_map> #include <unordered_set> using namespace std; map<int, pair<int, int>> collisions; void readDeliveries(int size, vector<pair<int, int>>& vertical, vector<pair<int, int>>& horizontal) { int type, loc, time; for (int i = 0; i < size; ++i) { cin >> type; cin >> loc; cin >> time; int key = time - loc; if (collisions.find(key) == collisions.end()) { collisions[key] = pair<int, int>(0,0); } if (type % 2 == 1) { collisions[key].first += 1; } else { collisions[key].second += 1; } } } int removeCollisions() { int deleted = 0; while (collisions.size() != 0) { pair<int, int> collision = (*collisions.begin()).second; if (collision.first == 0 || collision.second == 0) { //they won't even collide collisions.erase(collisions.begin()); } else { deleted += min(collision.first, collision.second); collisions.erase(collisions.begin()); } } return deleted; }; int main() { int size; cin >> size; vector<pair<int, int>> vertical; vector<pair<int, int>> horizontal; readDeliveries(size, vertical, horizontal); cout << removeCollisions() << endl; return 0; } |