#include <iostream> #include <vector> #include <map> using namespace std; pair<int, int> findEntryWithLargestValue( map<int, int> sampleMap) { // Reference variable to help find // the entry with the highest value pair<int, int> entryWithMaxValue = make_pair(0, 0); // Iterate in the map to find the required entry map<int, int>::iterator currentEntry; for (currentEntry = sampleMap.begin(); currentEntry != sampleMap.end(); ++currentEntry) { // If this entry's value is more // than the max value // Set this entry as the max if (currentEntry->second > entryWithMaxValue.second) { entryWithMaxValue = make_pair( currentEntry->first, currentEntry->second); } } return entryWithMaxValue; } int main() { int n = 0; cin >> n; vector<pair<int, int>> cars1, cars2, bum; vector<pair<int, int>>::iterator i, j; // Załadowanie tras while (n--) { short r; int w, t; cin >> r >> w >> t; if (r == 1) cars1.push_back(make_pair(w, t)); else cars2.push_back(make_pair(w, t)); } //cout << "Cars1:" << endl; //for (i = cars1.begin(); i != cars1.end(); i++) { // cout << "(" << (*i).first << " " << (*i).second << ")" << endl; //} //cout << "Cars2:" << endl; //for (i = cars2.begin(); i != cars2.end(); i++) { // cout << "(" << (*i).first << " " << (*i).second << ")" << endl; //} //cout << "Sprawdzenie konfliktów" << endl; map <int, int> hist1, hist2; for (int a = 0; a < cars1.size(); a++) { for (int b = 0; b < cars2.size(); b++) { if (cars1[a].first + cars2[b].second == cars2[b].first + cars1[a].second) { //cout << a << " " << b << endl; bum.push_back(make_pair(a, b)); if (hist1.find(a) == hist1.end()) hist1[a] = 1; else hist1[a]++; if (hist2.find(b) == hist2.end()) hist2[b] = 1; else hist2[b]++; } } } int sum = 0; //cout << "Czyszczenie" << endl; while (bum.size() > 0) { pair<int, int> m1 = findEntryWithLargestValue(hist1); pair<int, int> m2 = findEntryWithLargestValue(hist2); //cout << "m1:" << m1.first << "(" << m1.second << ") m2:" << m2.first << "(" << m2.second << ")" << endl; if (m1.second > m2.second) { for (i = bum.begin(); i != bum.end();) { if ((*i).first == m1.first) { hist1[m1.first]--; hist2[(*i).second]--; i = bum.erase(i); } else ++i; } } else { for (i = bum.begin(); i != bum.end();) { if ((*i).second == m2.first) { hist1[(*i).second]--; hist2[m2.first]--; i = bum.erase(i); } else ++i; } } sum++; } cout << sum << 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 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | #include <iostream> #include <vector> #include <map> using namespace std; pair<int, int> findEntryWithLargestValue( map<int, int> sampleMap) { // Reference variable to help find // the entry with the highest value pair<int, int> entryWithMaxValue = make_pair(0, 0); // Iterate in the map to find the required entry map<int, int>::iterator currentEntry; for (currentEntry = sampleMap.begin(); currentEntry != sampleMap.end(); ++currentEntry) { // If this entry's value is more // than the max value // Set this entry as the max if (currentEntry->second > entryWithMaxValue.second) { entryWithMaxValue = make_pair( currentEntry->first, currentEntry->second); } } return entryWithMaxValue; } int main() { int n = 0; cin >> n; vector<pair<int, int>> cars1, cars2, bum; vector<pair<int, int>>::iterator i, j; // Załadowanie tras while (n--) { short r; int w, t; cin >> r >> w >> t; if (r == 1) cars1.push_back(make_pair(w, t)); else cars2.push_back(make_pair(w, t)); } //cout << "Cars1:" << endl; //for (i = cars1.begin(); i != cars1.end(); i++) { // cout << "(" << (*i).first << " " << (*i).second << ")" << endl; //} //cout << "Cars2:" << endl; //for (i = cars2.begin(); i != cars2.end(); i++) { // cout << "(" << (*i).first << " " << (*i).second << ")" << endl; //} //cout << "Sprawdzenie konfliktów" << endl; map <int, int> hist1, hist2; for (int a = 0; a < cars1.size(); a++) { for (int b = 0; b < cars2.size(); b++) { if (cars1[a].first + cars2[b].second == cars2[b].first + cars1[a].second) { //cout << a << " " << b << endl; bum.push_back(make_pair(a, b)); if (hist1.find(a) == hist1.end()) hist1[a] = 1; else hist1[a]++; if (hist2.find(b) == hist2.end()) hist2[b] = 1; else hist2[b]++; } } } int sum = 0; //cout << "Czyszczenie" << endl; while (bum.size() > 0) { pair<int, int> m1 = findEntryWithLargestValue(hist1); pair<int, int> m2 = findEntryWithLargestValue(hist2); //cout << "m1:" << m1.first << "(" << m1.second << ") m2:" << m2.first << "(" << m2.second << ")" << endl; if (m1.second > m2.second) { for (i = bum.begin(); i != bum.end();) { if ((*i).first == m1.first) { hist1[m1.first]--; hist2[(*i).second]--; i = bum.erase(i); } else ++i; } } else { for (i = bum.begin(); i != bum.end();) { if ((*i).second == m2.first) { hist1[(*i).second]--; hist2[m2.first]--; i = bum.erase(i); } else ++i; } } sum++; } cout << sum << endl; return 0; } |