#include <iostream> #include <vector> #include <algorithm> using namespace std; int size(vector<int> &A, int q){ int n = A.size(),y; int b=0,e=n-1; while (e>b) { int x = (e+b)/2; if (A[x] < q) b = x+1; else e = x; } if (A[e] != q) return 0; else { int c=0,f=n-1; while (f>c) { int x = (c+f+1)/2; if (A[x] > q) f = x-1; else c = x; } return f-e+1; } } int main(){ cin.tie(0);cout.tie(0); ios_base::sync_with_stdio(false); int n; cin >> n; vector <int> A, B; while(n--){ int a, b, c; cin >> a >> b >> c; if(a==1)A.push_back(b-c); if(a==2)B.push_back(b-c); } sort(A.begin(),A.end()); sort(B.begin(),B.end()); if(A.size()==0 || B.size() ==0){ cout << 0 << '\n'; return 0; } int counter = 0; int i=0,j=0; while(i<A.size()){ int a = size(A,A[i]); while(B[j]<A[i] && j < B.size()){ j++; } if(A[i]==B[j]){ int b= size(B,A[i]); counter += min(a,b); j+=b; } i+=a; } cout << counter << '\n'; }
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 <iostream> #include <vector> #include <algorithm> using namespace std; int size(vector<int> &A, int q){ int n = A.size(),y; int b=0,e=n-1; while (e>b) { int x = (e+b)/2; if (A[x] < q) b = x+1; else e = x; } if (A[e] != q) return 0; else { int c=0,f=n-1; while (f>c) { int x = (c+f+1)/2; if (A[x] > q) f = x-1; else c = x; } return f-e+1; } } int main(){ cin.tie(0);cout.tie(0); ios_base::sync_with_stdio(false); int n; cin >> n; vector <int> A, B; while(n--){ int a, b, c; cin >> a >> b >> c; if(a==1)A.push_back(b-c); if(a==2)B.push_back(b-c); } sort(A.begin(),A.end()); sort(B.begin(),B.end()); if(A.size()==0 || B.size() ==0){ cout << 0 << '\n'; return 0; } int counter = 0; int i=0,j=0; while(i<A.size()){ int a = size(A,A[i]); while(B[j]<A[i] && j < B.size()){ j++; } if(A[i]==B[j]){ int b= size(B,A[i]); counter += min(a,b); j+=b; } i+=a; } cout << counter << '\n'; } |