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
#include <iostream>
#include <iterator> 
#include <map> 
using namespace std;

int main() {
    int n;
    cin >> n;

    map<int, int> azz, bzz;
    map<int, int>::iterator it;
    int aj = 0, bj = 0;

    for (int i = 0; i < n; i++) {
        int r, w, t;
        cin >> r >> w >> t;

        int v = w - t;
        
        if (r == 1) {
            it = azz.find(v);
            if (it != azz.end()) {
                it->second += 1;
            } else {
                azz.insert(pair<int, int>(v, 1)); 
            }
            aj++;
        }
        if (r == 2) {
            it = bzz.find(v);
            if (it != bzz.end()) {
                it->second += 1;
            } else {
                bzz.insert(pair<int, int>(v, 1)); 
            }
            bj++;
        }
    }

    if (aj == 0 || bj == 0) {
        cout << 0;
        return 0;
    }

    int w = 0;
    for (map<int, int>::iterator it2 = azz.begin(); it2 != azz.end(); it2++) {
        it = bzz.find(it->first);
        if (it != bzz.end()) {
            w += min(it->second, it2->second); 
        }
    }

    cout << w;
    return 0;
}