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
| #include <iostream> #include <cstdio> #define SIZE 14 #define INF 2147483647 using namespace std; int n, ans, c[SIZE]; void init() {ans = INF; for (int i = 0; i < SIZE; i++) c[i] = 0;} void modify(int l, int r, int x) {for (int i = l; i <= r; i++) c[i] += x;} int calc() { int c1, c2, c3, c4, ret; c1 = c2 = c3 = c4 = ret = 0; for (int i = 0; i < SIZE; i++) c1 += (c[i] == 1), c2 += (c[i] == 2), c3 += (c[i] == 3), c4 += (c[i] == 4); while (c2 >= 2 && c4 >= 1) c2 -= 2, c4 -= 1, ret++; while (c2 >= 1 && c3 >= 1) c2 -= 1, c3 -= 1, ret++; while (c1 >= 2 && c4 >= 1) c1 -= 2, c4 -= 1, ret++; while (c2 >= 1 && c4 >= 1) c2 -= 1, c4 -= 1, ret++; while (c1 >= 1 && c3 >= 1) c1 -= 1, c3 -= 1, ret++; return ret+c1+c2+c3+c4; } void DFS(int stp) { int lft = calc(); bool flag = false; if (stp > ans) return; ans = min(ans, stp+lft); for (int l = 2; l+1 < SIZE; l++) { int tr = l; while (c[tr] >= 3 && tr < SIZE) tr++; if (--tr-l+1 < 2) continue; flag = true; for (int r = tr; r >= l+1; r--) modify(l, r, -3), DFS(stp+1), modify(l, r, 3); } for (int l = 2; l+2 < SIZE; l++) { int tr = l; while (c[tr] >= 2 && tr < SIZE) tr++; if (--tr-l+1 < 3) continue; flag = true; for (int r = tr; r >= l+2; r--) modify(l, r, -2), DFS(stp+1), modify(l, r, 2); } for (int l = 2; l+4 < SIZE; l++) { int tr = l; while (c[tr] >= 1 && tr < SIZE) tr++; if (--tr-l+1 < 5) continue; flag = true; for (int r = tr; r >= l+4; r--) modify(l, r, -1), DFS(stp+1), modify(l, r, 1); } lft = calc(), ans = min(ans, stp+lft); } int main() { int T; scanf("%d%d", &T, &n); while (T--) { init(); for (int i = 0, a, b; i < n; i++) scanf("%d%d", &a, &b), a = a == 1 ? 13 : (a == 0 ? 0 : a-1), c[a]++; DFS(0), printf("%d\n", ans); } return 0; }
|