Problem:
Three Meals

You are running a restaurant that serves dishes only three times a day. Each of your customer has his/her own separate time zones for eating breakfast, lunch and supper. Thus, your customer enjoys your dish only when he/she visits your restaurant to meet your serving time settings. Your wish is to modify your serving time arrangement so that as many as possible of your customers can enjoy your meals three times a day.

Write a program to enable as many as possible of customers can enjoy your service three times a day, i.e. breakfast, lunch, and supper. A list of customers eating time zones is given, which you cannot change. Settings of your service time are your option. Coincidence between your service time and either edge of customer’s time zone (start or end time) does not hinder you to provide your service.

Input

The input is given in the following format.

N
ast_1 aet_1 hst_1 het_1 bst_1 bet_1
ast_2 aet_2 hst_2 het_2 bst_2 bet_2
:
ast_N aet_N hst_N het_N bst_N bet_N

The first line provides the number of customers N(1≤N≤50). Each of subsequent N lines provides time zone information of i-th customer: start and end time of breakfast zone (ast_i, aet_i), those of lunch zone (hst_i, het_i) and those of supper zone (bst_i, bet_i). The end time must be later in time as the start time. Time zones do not overlap, i.e. hst_i is later in time than aet_i, and so on. Each time settings must not cross 0:00 midnight.

h m

Each time information is given by hour h (0≤h≤23) and minute m(0≤m≤59).

Output

Output the maximum number of customers that can be served all three meals (breakfast, lunch, and supper).

Sample Input 1

5
1 0 2 0 3 30 4 30 6 0 7 0
2 30 3 0 4 0 5 0 5 30 6 30
1 30 2 30 4 30 5 0 6 30 7 0
2 30 3 0 5 0 6 0 6 30 7 0
1 0 2 0 3 0 3 30 4 0 5 0

Sample Output 1

3

Solution in C (judge verdict: Accepted)

// AOJ 0372: Three Meals
// 2017.11.14 bal4u@uu

#include <stdio.h>
#include <stdlib.h>

typedef struct { long long b; int n, t; } T;
T tbl[3][1440]; int sz[3];
long long a[3][1440];

int bitcount64(long long x)
{
	x = ((x & 0xAAAAAAAAAAAAAALL) >>  1) + (x & 0x55555555555555LL);
	x = ((x & 0xCCCCCCCCCCCCCCLL) >>  2) + (x & 0x33333333333333LL);
	x = ((x & 0xF0F0F0F0F0F0F0LL) >>  4) + (x & 0x0F0F0F0F0F0F0FLL);
	x = ((x & 0x00FF00FF00FF00LL) >>  8) + (x & 0xFF00FF00FF00FFLL);
	x = ((x & 0xFF0000FFFF0000LL) >> 16) + (x & 0x00FFFF0000FFFFLL);
	x = ((x & 0xFFFFFF00000000LL) >> 32) + (x & 0x000000FFFFFFFFLL);
	return (int)x;
}

int cmp(T *a, T *b) { if (b->n - a->n) return b->n - a->n; return b->t - a->t; }

int main()
{
	int n, i, j, k, at, ht, f, ans;
	long long b, bb;
	
	scanf("%d", &n);
	for (b = 1, i = 0; i < n; i++, b <<= 1) for (j = 0; j < 3; j++) {
		int sh, sm, eh, em, t;
		scanf("%d%d%d%d", &sh, &sm, &eh, &em);
		sh = sh*60+sm, eh = eh*60+em;
		for (t = sh; t <= eh; t++) a[j][t] |= b;
	}
	for (j = 0; j < 3; j++) {
		T *tp;
		for (tp = tbl[j], i = 0; i < 1440; i++) if ((b = a[j][i]) > 0) {
			tp->b = b, tp->n = bitcount64(b), tp->t = i, tp++;
		}
		sz[j] = tp - tbl[j];
		qsort(tbl[j], sz[j], sizeof(T), cmp);
	}

	ans = 0;
	b  = tbl[0][0].b, n = tbl[0][0].n;
	b &= tbl[1][0].b; if (tbl[1][0].n < n) n = tbl[1][0].n;
	b &= tbl[2][0].b; if (tbl[2][0].n < n) n = tbl[2][0].n;
	if (bitcount64(b) == n && tbl[0][0].t < tbl[1][0].t && tbl[1][0].t < tbl[2][0].t) {
		ans = n; goto done;
	}

	for (i = 0; i < sz[0]; i++) {
		if (tbl[0][i].n <= ans) goto done;
		at = tbl[0][i].t, b = tbl[0][i].b;
		for (j = 0; j < sz[1]; j++) {
			if (tbl[1][j].n <= ans) break;
			if ((ht = tbl[1][j].t) <= at) continue;
			bb = tbl[1][j].b & b;
			if (bitcount64(bb) <= ans) continue;
			for (k = 0; k < sz[2]; k++) {
				if (tbl[2][k].n <= ans) break;
				if (tbl[2][k].t <= ht) continue;
				if ((f = bitcount64(bb & tbl[2][k].b)) > ans) {
					ans = f;
					if (ans == n) goto done;
				}
			}
		}
	}
done:
	printf("%d\n", ans);
	return 0;
}