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
// Karol Kosinski 2020
#include <cstdio>
#include <algorithm>
#include <map>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
//#define DEBUG(x...) printf(x)
#define DEBUG(x...)
using namespace std;

int main()
{
    int n, r, w, t, res = 0;
    map<int, int> M[2];
    scanf("%d", &n);
    FOR(i,0,n)
    {
        scanf("%d%d%d", &r, &w, &t);
        ++ M[ r - 1 ][ w - t ];
        DEBUG("(%d  %d):  %d\n", r - 1, w - t, M[ r - 1 ][ w - t ]);
    }
    auto it_0 = M[0].cbegin();
    auto it_1 = M[1].cbegin();
    while (it_0 != M[0].cend() and it_1 != M[1].cend())
    {
        while (it_0 != M[0].cend() and it_0->first < it_1->first) ++ it_0;
        if (it_0 == M[0].cend()) break;
        while (it_1 != M[1].cend() and it_1->first < it_0->first) ++ it_1;
        if (it_1 == M[1].cend()) break;
        if (it_0->first == it_1->first)
        {
            res += min(it_0->second, it_1->second);
            ++ it_0, ++ it_1;
        }
    }
    printf("%d\n", res);
    return 0;
}