#include <iostream> #include <vector> #include <tuple> #include <math.h> #include <algorithm> #include <unordered_set> using namespace std; bool compare(unordered_set<int> v1, unordered_set<int> v2) { return v1.size() > v2.size(); } vector<unordered_set<int> > collisions(vector<tuple<int, int, int> > &x, vector<tuple<int, int, int> > &y, int n) { vector < unordered_set<int> > ret(n); for (int i = 0; i < x.size(); i++) { int delay1 = get<1>(x[i]); int start1 = get<0>(x[i]); int index1 = get<2>(x[i]); for (int p = 0; p < y.size(); p++) { int delay2 = get<1>(y[p]); int start2 = get<0>(y[p]); int index2 = get<2>(y[p]); if (start1 - delay1 == start2 - delay2) { ret[index1].insert(index2); ret[index2].insert(index1); } } } return ret; } pair<int,int> findmaxindex(vector<unordered_set<int> > v) { int indx = 0; int max = 0; for (int i = 0;i<v.size();i++) { if (v[i].size() > max) { indx = i; max = v[i].size(); } } return { indx,max }; } void remove(int x, vector<unordered_set<int> > &v) { for (auto k : v[x]) { v[k].erase(x); } v[x].clear(); //sort(v.begin(), v.end(), compare); } void printv(vector<unordered_set<int> > v) { for (auto k : v) { for (auto g : k) { cout << g << " "; } cout << endl; } } int main() { int n; cin >> n; vector<tuple<int,int,int> > x; vector<tuple<int,int,int> > y; for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; if (a == 1) { x.push_back({ b,c,i }); } else { y.push_back({ b,c,i }); } } vector<unordered_set<int> > v = collisions(x, y, n); pair<int, int> p = findmaxindex(v); int cnt = 0; //printv(v); while (p.second != 0) { //printv(v); remove(p.first, v); cnt++; p = findmaxindex(v); } cout << cnt; }
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 <tuple> #include <math.h> #include <algorithm> #include <unordered_set> using namespace std; bool compare(unordered_set<int> v1, unordered_set<int> v2) { return v1.size() > v2.size(); } vector<unordered_set<int> > collisions(vector<tuple<int, int, int> > &x, vector<tuple<int, int, int> > &y, int n) { vector < unordered_set<int> > ret(n); for (int i = 0; i < x.size(); i++) { int delay1 = get<1>(x[i]); int start1 = get<0>(x[i]); int index1 = get<2>(x[i]); for (int p = 0; p < y.size(); p++) { int delay2 = get<1>(y[p]); int start2 = get<0>(y[p]); int index2 = get<2>(y[p]); if (start1 - delay1 == start2 - delay2) { ret[index1].insert(index2); ret[index2].insert(index1); } } } return ret; } pair<int,int> findmaxindex(vector<unordered_set<int> > v) { int indx = 0; int max = 0; for (int i = 0;i<v.size();i++) { if (v[i].size() > max) { indx = i; max = v[i].size(); } } return { indx,max }; } void remove(int x, vector<unordered_set<int> > &v) { for (auto k : v[x]) { v[k].erase(x); } v[x].clear(); //sort(v.begin(), v.end(), compare); } void printv(vector<unordered_set<int> > v) { for (auto k : v) { for (auto g : k) { cout << g << " "; } cout << endl; } } int main() { int n; cin >> n; vector<tuple<int,int,int> > x; vector<tuple<int,int,int> > y; for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; if (a == 1) { x.push_back({ b,c,i }); } else { y.push_back({ b,c,i }); } } vector<unordered_set<int> > v = collisions(x, y, n); pair<int, int> p = findmaxindex(v); int cnt = 0; //printv(v); while (p.second != 0) { //printv(v); remove(p.first, v); cnt++; p = findmaxindex(v); } cout << cnt; } |