#include<iostream> #include<algorithm> using namespace std; const int MAX_N = 500007; int l[2]; int x[2][MAX_N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, w, t, r; cin >> n; for(int i = 0; i < n; i++) { cin >> r >> w >> t; r--; x[r][l[r]++] = w-t; } sort(x[0], x[0]+l[0]); sort(x[1], x[1]+l[1]); int i = 0, j = 0, wyn = 0; while(i < l[0] && j < l[1]) { if(x[0][i] < x[1][j]) { i++; } else if(x[0][i] > x[1][j]) { j++; } else { int ilast = i, jlast = j; while(i < l[0]-1 && x[0][i+1] == x[0][i]) { i++; } while(j < l[1]-1 && x[1][j+1] == x[1][j]) { j++; } wyn += min(i-ilast+1, j-jlast+1); i++; j++; } } cout << wyn << '\n'; 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 | #include<iostream> #include<algorithm> using namespace std; const int MAX_N = 500007; int l[2]; int x[2][MAX_N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, w, t, r; cin >> n; for(int i = 0; i < n; i++) { cin >> r >> w >> t; r--; x[r][l[r]++] = w-t; } sort(x[0], x[0]+l[0]); sort(x[1], x[1]+l[1]); int i = 0, j = 0, wyn = 0; while(i < l[0] && j < l[1]) { if(x[0][i] < x[1][j]) { i++; } else if(x[0][i] > x[1][j]) { j++; } else { int ilast = i, jlast = j; while(i < l[0]-1 && x[0][i+1] == x[0][i]) { i++; } while(j < l[1]-1 && x[1][j+1] == x[1][j]) { j++; } wyn += min(i-ilast+1, j-jlast+1); i++; j++; } } cout << wyn << '\n'; return 0; } |