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
#include <iostream>
#include <vector>
#include <set>
#include <utility>
#include <algorithm>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    int n, r, w, t, ans=0;
    vector<pair<int,pair<int, int>>> r1, r2;
    cin >> n;
    vector<pair<int,set<int>>> c(n);
    for (int i=0; n>i; i++) {
        cin >> r >> w >> t;
        if (r % 2) {
            for (int j=0; r2.size()>j; j++) {
                if(r2[j].second.first + t == w + r2[j].second.second) {
                    c[i].second.insert(r2[j].first);
                    c[r2[j].first].second.insert(i);
                }
            }
            r1.push_back(make_pair(i, make_pair(w, t)));
        }
        else {
            for (int j=0; r1.size()>j; j++) {
                if(r1[j].second.first + t == w + r1[j].second.second) {
                    c[i].second.insert(r1[j].first);
                    c[r1[j].first].second.insert(i);
                }
            }
            r2.push_back(make_pair(i, make_pair(w, t)));
        } 
        c[i].first = i;
    }
    int tmpmx, tmpit, lastres = -1;
    while (c.size() > 0) {
        tmpmx = 0;
        for (int j = 0; c.size()>j; j++) {
            c[j].second.erase(lastres);
            if (c[j].second.size() == 0) {
                c.erase(c.begin()+j);
                j--;
            }
            else if (c[j].second.size() > tmpmx) {
                tmpmx = c[j].second.size();
                tmpit = j;
            }
        }
        if (c.size() != 0) {
            lastres = c[tmpit].first;
            c.erase(c.begin()+tmpit);
            ans++;
        }
    }
    cout << ans << "\n";
    return 0;
}