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
#include <bits/stdc++.h>
using namespace std;
#define bit(n, i) ((n>>i) & 1)
#define fi first
#define se second
#define pb push_back
#define cat(x) cerr << #x " = " << x << "\n"
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;

const int N = 5e5+9;
const int inf = 1e9;
pii A[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n; cin >> n;
    for(int i = 1; i <= n; ++i) {
        int r, w, t; cin >> r >> w >> t;
        A[i] = {w-t, r};
    }
    sort(A+1, A+n+1);
    A[n+1] = {inf, 0};
    int res = 0;
    for(int i = 1, c1 = 0, c2 = 0; i <= n; ++i) {
        if(A[i].se == 1)
            c1++;
        else
            c2++;
        if(A[i].fi != A[i+1].fi) {
            res += min(c1, c2);
            c1 = c2 = 0;
        }
    }
    cout << res;

    return 0;
}