#include <stdio.h> #include <map> #include <string> #include <iostream> using namespace std; int main(){ int number; scanf("%d\n", &number); int kind, sP, sT; map<int, int> map1, map2; map<int, int>::iterator it1, it2; int val; for (int idx = 0; idx < number; ++idx) { scanf("%d %d %d\n", &kind, &sP, &sT); val = sP - sT; if (kind == 1) { it1 = map1.find(val); if (it1 != map1.end()) { it1->second ++; } else { map1.insert(pair(val, 1)); } } else { it2 = map2.find(val); if (it2 != map2.end()) { it2->second ++; } else { map2.insert(pair(val, 1)); } } } // for (it1=map1.begin(); it1!=map1.end(); ++it1) // cout << it1->first << " => " << it1->second << '\n'; // for (it1=map2.begin(); it1!=map2.end(); ++it1) // cout << it1->first << " => " << it1->second << '\n'; it1 = map1.begin(); it2 = map2.begin(); int diff = 0; while (it1 != map1.end() && it2 != map2.end()) { if (it1->first == it2->first) { diff += min(it1->second, it2->second); it1++; it2++; continue; } if (it1->first > it2->first) { it2++; } else { it1++; } } printf("%d\n", diff); 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 | #include <stdio.h> #include <map> #include <string> #include <iostream> using namespace std; int main(){ int number; scanf("%d\n", &number); int kind, sP, sT; map<int, int> map1, map2; map<int, int>::iterator it1, it2; int val; for (int idx = 0; idx < number; ++idx) { scanf("%d %d %d\n", &kind, &sP, &sT); val = sP - sT; if (kind == 1) { it1 = map1.find(val); if (it1 != map1.end()) { it1->second ++; } else { map1.insert(pair(val, 1)); } } else { it2 = map2.find(val); if (it2 != map2.end()) { it2->second ++; } else { map2.insert(pair(val, 1)); } } } // for (it1=map1.begin(); it1!=map1.end(); ++it1) // cout << it1->first << " => " << it1->second << '\n'; // for (it1=map2.begin(); it1!=map2.end(); ++it1) // cout << it1->first << " => " << it1->second << '\n'; it1 = map1.begin(); it2 = map2.begin(); int diff = 0; while (it1 != map1.end() && it2 != map2.end()) { if (it1->first == it2->first) { diff += min(it1->second, it2->second); it1++; it2++; continue; } if (it1->first > it2->first) { it2++; } else { it1++; } } printf("%d\n", diff); return 0; } |