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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
using namespace std;

struct Collision {
	int r, w, t;
	Collision * next = NULL;
	Collision(int r, int w, int t) {
		this->r = r;
		this->w = w;
		this->t = t;
	}

	bool doesCollide(Collision * other) {
		return (this->r != other->r) && (this->w - this->t == other->w - other->t);
	}

	bool operator==(Collision &rhs) {
		return this->r == rhs.r && this->w == rhs.w && this->t == rhs.t;
	}
};

struct Node {
	Node * next = NULL;
	Collision * collision = NULL;
	int length = 0;

	Node(Collision * collision) {
		this->collision = collision;
	}

	void addToTail(Node * n) {
		if (this->collision != NULL && this->collision->doesCollide(n->collision)) {
			this->length++;
			n->length++;
		}

		if (this->next == NULL) {
			this->next = n;
		} else {
			this->next->addToTail(n);
		}
	}
};

void print(Collision * collision) {
	if (collision == NULL) {
		cout << "NULL";
	} else {
		cout << "Collision(" << collision->r << ", " << collision->w << ", " << collision->t << ") ";
	}
}

void print(Node * node) {
	cout << "Node [" << node->length << "]: ";
	print(node->collision);
	cout << endl;

	if (node->next != NULL) {
		print(node->next);
	}
}

Node * getLongestNode(Node * node) {
	Node * longest = node;
	Node * n = node;

	while (n->next != NULL) {
		n = n->next;
		if (n->length > longest->length) {
			longest = n;
		}
	}

	return longest;
}

void removeCollision(Node * node, Collision * collision) {
	while(node != NULL) {
		if(node->collision != NULL && node->collision->doesCollide(collision)) node->length--;
		node = node -> next;
	}
}

void removeNodeWithCollision(Node * node, Collision * collision) {
	while (node->next != NULL) {
		if (*(node->next->collision) == *collision) {
			node->next = node->next->next;
			return;
		}
		node = node->next;
	}
}

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

	Node * root = new Node(NULL);

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

		Collision * collision = new Collision(r, w, t);
		root->addToTail(new Node(collision));
	}

	int toRemove = 0;

	while (true) {
		Node * max = getLongestNode(root);
		if (max->length == 0)
			break;
		removeCollision(root, max->collision);
		removeNodeWithCollision(root, max->collision);
		toRemove++;
	}

	cout << toRemove;

	return 0;
}