#include <iostream> #include <unordered_map> #include <algorithm> using namespace std; int findCollisions(unordered_map<int, int> &horizontal, unordered_map<int, int> &vertical) { int ret = 0; for (auto &hcar: horizontal) { int offset = hcar.first; if (vertical.find(offset) != vertical.end()) { ret += min(hcar.second, vertical[offset]); } } return ret; } int main() { int n; int ti; int wi; int ri; cin >> n; auto horizontal = unordered_map<int, int>(); auto vertical = unordered_map<int, int>(); for (int i = 0; i < n; i++) { cin >> ti >> wi >> ri; int offset = wi - ri; if (ti == 1) { vertical[offset] += 1; } else { horizontal[offset] += 1; } } int result = findCollisions(horizontal, vertical); cout << result << std::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 | #include <iostream> #include <unordered_map> #include <algorithm> using namespace std; int findCollisions(unordered_map<int, int> &horizontal, unordered_map<int, int> &vertical) { int ret = 0; for (auto &hcar: horizontal) { int offset = hcar.first; if (vertical.find(offset) != vertical.end()) { ret += min(hcar.second, vertical[offset]); } } return ret; } int main() { int n; int ti; int wi; int ri; cin >> n; auto horizontal = unordered_map<int, int>(); auto vertical = unordered_map<int, int>(); for (int i = 0; i < n; i++) { cin >> ti >> wi >> ri; int offset = wi - ri; if (ti == 1) { vertical[offset] += 1; } else { horizontal[offset] += 1; } } int result = findCollisions(horizontal, vertical); cout << result << std::endl; return 0; } |