#include<iostream> #include<vector> #include<algorithm> using namespace std; int n,result=0; vector<int> dir1; vector<int> dir2; vector<pair<int,int>> dir1_s; vector<pair<int,int>> dir2_s; int main() { cin>>n; for(int i=0; i<n; i++) { int temp; cin>>temp; if(temp==1) { int temp2, temp3; cin>>temp2>>temp3; dir1.push_back(temp2-temp3); } if(temp==2) { int temp2, temp3; cin>>temp2>>temp3; dir2.push_back(temp2-temp3); } } //cout<<endl; sort(dir1.begin(),dir1.end()); sort(dir2.begin(),dir2.end()); //for(int i=0; i<dir2.size(); i++) cout<<dir2[i]<<" "; int count=0; for(int i=0; i<dir1.size(); i++) { int comp=dir1[i]; while(dir1[i]==comp&&dir1.size()>i) { count++; i++; } dir1_s.push_back({comp,count}); //cout<<dir1_s.back().first<<" "<<dir1_s.back().second<<" "; count=0; i--; } count=0; //cout<<endl; for(int i=0; i<dir2.size(); i++) { int comp=dir2[i]; while(dir2[i]==comp&&dir2.size()>i) { count++; i++; } dir2_s.push_back({comp,count}); //cout<<dir2_s.back().first<<" "<<dir2_s.back().second<<" "; count=0; i--; } while(!dir1_s.empty()&&!dir2_s.empty()) { if(dir1_s.back().first>dir2_s.back().first) dir1_s.pop_back(); else if(dir1_s.back().first<dir2_s.back().first) dir2_s.pop_back(); else { result+=min(dir1_s.back().second,dir2_s.back().second); dir1_s.pop_back(); dir2_s.pop_back(); } } cout<<result; 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 | #include<iostream> #include<vector> #include<algorithm> using namespace std; int n,result=0; vector<int> dir1; vector<int> dir2; vector<pair<int,int>> dir1_s; vector<pair<int,int>> dir2_s; int main() { cin>>n; for(int i=0; i<n; i++) { int temp; cin>>temp; if(temp==1) { int temp2, temp3; cin>>temp2>>temp3; dir1.push_back(temp2-temp3); } if(temp==2) { int temp2, temp3; cin>>temp2>>temp3; dir2.push_back(temp2-temp3); } } //cout<<endl; sort(dir1.begin(),dir1.end()); sort(dir2.begin(),dir2.end()); //for(int i=0; i<dir2.size(); i++) cout<<dir2[i]<<" "; int count=0; for(int i=0; i<dir1.size(); i++) { int comp=dir1[i]; while(dir1[i]==comp&&dir1.size()>i) { count++; i++; } dir1_s.push_back({comp,count}); //cout<<dir1_s.back().first<<" "<<dir1_s.back().second<<" "; count=0; i--; } count=0; //cout<<endl; for(int i=0; i<dir2.size(); i++) { int comp=dir2[i]; while(dir2[i]==comp&&dir2.size()>i) { count++; i++; } dir2_s.push_back({comp,count}); //cout<<dir2_s.back().first<<" "<<dir2_s.back().second<<" "; count=0; i--; } while(!dir1_s.empty()&&!dir2_s.empty()) { if(dir1_s.back().first>dir2_s.back().first) dir1_s.pop_back(); else if(dir1_s.back().first<dir2_s.back().first) dir2_s.pop_back(); else { result+=min(dir1_s.back().second,dir2_s.back().second); dir1_s.pop_back(); dir2_s.pop_back(); } } cout<<result; return 0; } |