#include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <map> #include <stdlib.h> #include <vector> #include <cstring> #include <algorithm> using namespace std; int main() { int numbers; scanf("%d", &numbers); int time, line, type; map<int, int> type1; map<int, int> type2; for (int i = 0 ; i < numbers; i++) { scanf("%d %d %d", &type, &line, &time); if (type == 1) { int k = line - time; map<int, int>::iterator lb = type1.lower_bound(k); if(lb != type1.end() && !(type1.key_comp()(k, lb->first))) { lb->second++; } else { type1.insert(lb, map<int, int>::value_type(k, 1)); } } else { int k = line - time; map<int, int>::iterator lb = type2.lower_bound(k); if(lb != type2.end() && !(type2.key_comp()(k, lb->first))) { lb->second++; } else { type2.insert(lb, map<int, int>::value_type(k, 1)); } } } int ileDoUsuniecia = 0; map<int, int>::iterator lb = type1.begin(); map<int, int>::iterator lb2 = type2.begin(); while (lb != type1.end() && lb2 != type2.end()) { if (lb->first == lb2->first) { if(lb->second <= lb2->second) { ileDoUsuniecia += lb->second ; } else { ileDoUsuniecia += lb2->second; } lb++; lb2++; } else if (lb->first < lb2->first) { lb++; } else { lb2++; } } printf("%d", ileDoUsuniecia); 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 63 64 65 66 67 68 69 70 71 | #include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <map> #include <stdlib.h> #include <vector> #include <cstring> #include <algorithm> using namespace std; int main() { int numbers; scanf("%d", &numbers); int time, line, type; map<int, int> type1; map<int, int> type2; for (int i = 0 ; i < numbers; i++) { scanf("%d %d %d", &type, &line, &time); if (type == 1) { int k = line - time; map<int, int>::iterator lb = type1.lower_bound(k); if(lb != type1.end() && !(type1.key_comp()(k, lb->first))) { lb->second++; } else { type1.insert(lb, map<int, int>::value_type(k, 1)); } } else { int k = line - time; map<int, int>::iterator lb = type2.lower_bound(k); if(lb != type2.end() && !(type2.key_comp()(k, lb->first))) { lb->second++; } else { type2.insert(lb, map<int, int>::value_type(k, 1)); } } } int ileDoUsuniecia = 0; map<int, int>::iterator lb = type1.begin(); map<int, int>::iterator lb2 = type2.begin(); while (lb != type1.end() && lb2 != type2.end()) { if (lb->first == lb2->first) { if(lb->second <= lb2->second) { ileDoUsuniecia += lb->second ; } else { ileDoUsuniecia += lb2->second; } lb++; lb2++; } else if (lb->first < lb2->first) { lb++; } else { lb2++; } } printf("%d", ileDoUsuniecia); return 0; } |