public override void Do() { var h = read.Int(); var w = read.Int(); write.WriteLine(Math.Min(Solve(h, w), Solve(w, h))); }
public override void Do() { var n = read.Int(); var m = read.Int(); var q = read.Int(); var s = new string[n]; var dot = Create2DArray <int>(n + 1, m + 1); var hor = Create2DArray <int>(n + 1, m + 1); var ver = Create2DArray <int>(n + 1, m + 1); for (int i = 0; i < n; ++i) { s[i] = read.String(); for (int j = 0; j < m; ++j) { dot[i + 1][j + 1] = s[i][j] - '0' + dot[i][j + 1] + dot[i + 1][j] - dot[i][j]; var hr = 0; if (j > 0 && s[i][j] - '0' == 1 && s[i][j - 1] - '0' == 1) { hr = 1; } hor[i + 1][j + 1] = hr + hor[i][j + 1] + hor[i + 1][j] - hor[i][j]; var vr = 0; if (i > 0 && s[i][j] - '0' == 1 && s[i - 1][j] - '0' == 1) { vr = 1; } ver[i + 1][j + 1] = vr + ver[i][j + 1] + ver[i + 1][j] - ver[i][j]; } } while (q-- > 0) { var x1 = read.Int() - 1; var y1 = read.Int() - 1; var x2 = read.Int() - 1; var y2 = read.Int() - 1; var dts = dot[x2 + 1][y2 + 1] + dot[x1][y1] - dot[x2 + 1][y1] - dot[x1][y2 + 1]; var hors = 0; var verts = 0; if (y2 > y1) { hors = hor[x2 + 1][y2 + 1] + hor[x1][y1 + 1] - hor[x2 + 1][y1 + 1] - hor[x1][y2 + 1]; } if (x2 > x1) { verts = ver[x2 + 1][y2 + 1] + ver[x1 + 1][y1] - ver[x2 + 1][y1] - ver[x1 + 1][y2 + 1]; } write.WriteLine(dts - hors - verts); } }
public override void Do() { var t = read.Int(); for (int caseNum = 1; caseNum <= t; ++caseNum) { var n = read.Int(); var c = read.Int(); var m = read.Int(); var cust = new int[c]; var all = Create2DArray <int>(2, n); for (int j = 0; j < m; ++j) { var p = read.Int() - 1; var b = read.Int() - 1; if (c <= 2) { ++all[b][p]; } } if (c > 2) { write.WriteLine("Case #{0}: {1} {2}", caseNum, -1, -1); continue; } List <int> coupl = new List <int>(); for (int i = 0; i < n; ++i) { if (all[0][i] > 0 && all[1][i] > 0) { coupl.Add(i); } } int pos1 = 0; int pos2 = 1; int res = 0; int imps = 0; while (pos2 < coupl.Count) { var pa = coupl[pos1]; var pb = coupl[pos2]; var mn = Math.Min(all[0][pa], all[1][pb]); all[0][pa] -= mn; all[1][pb] -= mn; res += mn; if (all[0][pa] == 0 && all[1][pb] > 0) { pos1 = pos2; } else if (all[0][pa] == 0 && all[1][pb] == 0) { pos1 = pos2 + 1; } pos2 = pos2 + 1; } coupl = new List <int>(); for (int i = 0; i < n; ++i) { if (all[0][i] > 0 && all[1][i] > 0) { coupl.Add(i); } } if (coupl.Count > 1) { throw new Exception("bad algo"); } if (coupl.Count == 1) { var p = coupl[0]; for (int i = 0; i < n && all[0][p] > 0; ++i) { if (i != p && all[1][i] > 0) { var mn = Math.Min(all[0][p], all[1][i]); all[0][p] -= mn; all[1][i] -= mn; res += mn; } } for (int i = 0; i < n && all[1][p] > 0; ++i) { if (i != p && all[0][i] > 0) { var mn = Math.Min(all[1][p], all[0][i]); all[1][p] -= mn; all[0][i] -= mn; res += mn; } } if (all[0][p] > 0 && all[1][p] > 0) { if (p > 0) { imps = Math.Min(all[0][p], all[1][p]); res += Math.Max(all[0][p], all[1][p]); } else { res += all[0][p] + all[1][p]; } } else { var s0 = all[0].Sum(); var s1 = all[1].Sum(); res += Math.Max(s0, s1); } } else { var s0 = all[0].Sum(); var s1 = all[1].Sum(); res += Math.Max(s0, s1); } write.WriteLine("Case #{0}: {1} {2}", caseNum, res, imps); } }
public override void Do() { const int MOD = (int)(1e9 + 7); var n = read.Int(); var m = read.Int(); var lst = Create2DArray <List <int> >(3, n + 1); for (int i = 0; i < m; ++i) { var l = read.Int(); var r = read.Int(); var x = read.Int() - 1; lst[x][r] = lst[x][r] ?? new List <int>(); lst[x][r].Add(l); } var res = Create3DArray <int>(n + 1, n + 1, n + 1); res[0][0][0] = 1; for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { if (j != i || i == 0) { for (int h = 0; h <= j; ++h) { bool can = true; if (lst[0][j + 1] != null) { foreach (var ls in lst[0][j + 1]) { if (ls <= j) { can = false; } } } if (lst[1][j + 1] != null) { foreach (var ls in lst[1][j + 1]) { if (ls <= i || ls > j) { can = false; } } } if (lst[2][j + 1] != null) { foreach (var ls in lst[2][j + 1]) { if (ls > i) { can = false; } } } if (can) { if (i < h) { res[i][j][j + 1] += res[i][h][j]; } else { res[i][j][j + 1] += res[h][i][j]; } res[i][j][j + 1] %= MOD; } } for (int h = j + 2; h <= n; ++h) { bool can = true; if (lst[0][h] != null) { foreach (var ls in lst[0][h]) { if (ls <= j) { can = false; } } } if (lst[1][h] != null) { foreach (var ls in lst[1][h]) { if (ls <= i || ls > j) { can = false; } } } if (lst[2][h] != null) { foreach (var ls in lst[2][h]) { if (ls > i) { can = false; } } } if (can) { res[i][j][h] += res[i][j][h - 1]; res[i][j][h] %= MOD; } } } } } int answer = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i < j) { answer += res[i][j][n]; } else { answer += res[j][i][n]; } answer %= MOD; } } write.WriteLine(answer * 3L % MOD); }