static void Main() { Sc sc = new Sc(); int n = sc.I; Bit bit = new Bit(n * 2); int[] b = new int[n + 1], w = new int[n + 1]; string[] a = new string[n * 2]; int[] r = new int[n * 2]; for (int i = 0; i < n * 2; i++) { a[i] = sc.S; r[i] = int.Parse((a[i].Split())[1]); if (a[i][0] == 'B') { b[r[i]] = i; } else { w[r[i]] = i; } } long[,,] dp = new long[n + 1, n + 1, 2]; for (int i = 0; i <= n; i++) { bit = new Bit(n * 2); if (i != 0) { for (int j = 1; j < i; j++) { bit.Ud(b[j] + 1, 1); } dp[i, 0, 0] = dp[i - 1, 0, 0] + b[i] - bit.Get(b[i] + 1); bit.Ud(b[i] + 1, 1); } for (int j = 1; j <= n; j++) { dp[i, j, 0] = dp[i, j - 1, 0] + w[j] - bit.Get(w[j] + 1); if (i != 0) { dp[i, j, 0] = Math.Min(dp[i, j, 0], dp[i - 1, j, 0] + b[i] - dp[i - 1, j, 1]); } if (j != 0) { bit.Ud(w[j] + 1, 1); } if (i != n) { dp[i, j, 1] = bit.Get(b[i + 1] + 1); } } bit.Clear(); } Console.WriteLine("{0}", dp[n, n, 0]); }
static void Main() { Sc sc = new Sc(); int n = sc.I; Bit bit = new Bit(200000); var sb = new StringBuilder(); for (int i = 0; i < n; i++) { int[] q = sc.Ia; if (q[0] == 1) { bit.Ud(q[1], 1); } else { int lb = 0, ub = 200000, mid = 0; while (ub - lb > 1) { mid = (ub + lb) / 2; if (bit.Get(mid) > q[1] - 1) { ub = mid; } else { lb = mid; } } sb.Append(ub + "\n"); bit.Ud(ub, -1); } } Console.Write(sb); }
static int Fu(int lb, int ub, long q) { int mid = 0; while (ub - lb > 1) { mid = (ub + lb) / 2; if (bit.Get(mid) >= q) { ub = mid; } else { lb = mid; } } return(ub); }
static void Main() { Sc sc = new Sc(); int[] s = sc.Ia; int[] a = new int[s[0] + 1]; long[][] b = new long[s[0] + 1][]; int[] c = new int[s[0] + 1]; b[0] = new long[2]; for (int i = 1; i <= s[0]; i++) { a[i] = sc.I; b[i] = new long[] { b[i - 1][0] + a[i] - s[1], i }; } Array.Sort(b, Ca); int p = 1; long ans = 0; c[b[0][1]] = 1; for (int i = 1; i <= s[0]; i++) { if (b[i][0] != b[i - 1][0]) { p++; } c[b[i][1]] = p; } Bit bit = new Bit(p); bit.Ud(c[0], 1); for (int i = 1; i <= s[0]; i++) { ans += bit.Get(c[i]); bit.Ud(c[i], 1); } Console.WriteLine(ans); }
static void Main() { Sc sc = new Sc(); int[] s = sc.Ia, a = sc.Ia; long ans = 0; int[][] h = new int[s[0]][]; for (int i = 0; i < s[0]; i++) { h[i] = new int[] { a[i], i + 1 }; } Array.Sort(h, (u, v) => u[0] - v[0]); bit = new Bit(s[0]); var hs = new Dictionary <string, int>(); bit.Ud(h[0][1], 1); ans = h[s[2] - 1][0] - h[0][0]; for (int i = 1; i <= s[0] - s[2]; i++) { if (h[i + s[2] - 1][0] - h[i][0] < ans) { int p = 0; for (int j = i;; j++) { long q = bit.Get(h[j][1]); int l = Fu(0, h[j][1], q) + (q == 0?0:1), r = Fu(h[j][1], s[0] + 1, q + 1) - 1; string t = l + " " + r; if (r - l - (hs.ContainsKey(t)?hs[t]:0) + 1 >= s[1]) { if (j - i - p + 1 == s[2]) { ans = h[i + s[2] - 1 + p][0] - h[i][0]; break; } } else { p++; if (i + s[2] - 1 + p >= s[0]) { break; } else if (h[i + s[2] - 1 + p][0] - h[i][0] >= ans) { break; } } if (hs.ContainsKey(t)) { hs[t]++; } else { hs.Add(t, 1); } } hs.Clear(); } bit.Ud(h[i][1], 1); } Console.WriteLine("{0}", ans); }