|
| 1 | +package g3401_3500.s3480_maximize_subarrays_after_removing_one_conflicting_pair |
| 2 | + |
| 3 | +// #Hard #Array #Prefix_Sum #Enumeration #Segment_Tree |
| 4 | +// #2025_03_11_Time_48_ms_(100.00%)_Space_164.15_MB_(100.00%) |
| 5 | + |
| 6 | +import kotlin.math.max |
| 7 | +import kotlin.math.min |
| 8 | + |
| 9 | +class Solution { |
| 10 | + fun maxSubarrays(n: Int, conflictingPairs: Array<IntArray>): Long { |
| 11 | + val totalSubarrays = n.toLong() * (n + 1) / 2 |
| 12 | + val h = IntArray(n + 1) |
| 13 | + val d2 = IntArray(n + 1) |
| 14 | + h.fill(n + 1) |
| 15 | + d2.fill(n + 1) |
| 16 | + for (pair in conflictingPairs) { |
| 17 | + var a = pair[0] |
| 18 | + var b = pair[1] |
| 19 | + if (a > b) { |
| 20 | + val temp = a |
| 21 | + a = b |
| 22 | + b = temp |
| 23 | + } |
| 24 | + if (b < h[a]) { |
| 25 | + d2[a] = h[a] |
| 26 | + h[a] = b |
| 27 | + } else if (b < d2[a]) { |
| 28 | + d2[a] = b |
| 29 | + } |
| 30 | + } |
| 31 | + val f = IntArray(n + 2) |
| 32 | + f[n + 1] = n + 1 |
| 33 | + f[n] = h[n] |
| 34 | + for (i in n - 1 downTo 1) { |
| 35 | + f[i] = min(h[i], f[i + 1]).toInt() |
| 36 | + } |
| 37 | + // forbiddenCount(x) returns (n - x + 1) if x <= n, else 0. |
| 38 | + // This is the number of forbidden subarrays starting at some i when f[i] = x. |
| 39 | + var originalUnion: Long = 0 |
| 40 | + for (i in 1..n) { |
| 41 | + if (f[i] <= n) { |
| 42 | + originalUnion += (n - f[i] + 1).toLong() |
| 43 | + } |
| 44 | + } |
| 45 | + val originalValid = totalSubarrays - originalUnion |
| 46 | + var best = originalValid |
| 47 | + // For each index j (1 <= j <= n) where a candidate conflicting pair exists, |
| 48 | + // simulate removal of the pair that gave h[j] (if any). |
| 49 | + // (If there is no candidate pair at j, h[j] remains n+1.) |
| 50 | + for (j in 1..n) { |
| 51 | + // no conflicting pair at index j |
| 52 | + if (h[j] == n + 1) { |
| 53 | + continue |
| 54 | + } |
| 55 | + // Simulate removal: new candidate at j becomes d2[j] |
| 56 | + val newCandidate = if (j < n) min(d2[j], f[j + 1]).toInt() else d2[j] |
| 57 | + // We'll recompute the new f values for indices 1..j. |
| 58 | + // Let newF[i] denote the updated value. |
| 59 | + // For i > j, newF[i] remains as original f[i]. |
| 60 | + // For i = j, newF[j] = min( newCandidate, f[j+1] ) (which is newCandidate by |
| 61 | + // definition). |
| 62 | + val newFj = newCandidate |
| 63 | + // forbiddenCount(x) is defined as (n - x + 1) if x<= n, else 0. |
| 64 | + var delta = forbiddenCount(newFj, n) - forbiddenCount(f[j], n) |
| 65 | + var cur = newFj |
| 66 | + // Now update backwards for i = j-1 down to 1. |
| 67 | + for (i in j - 1 downTo 1) { |
| 68 | + val newVal = min(h[i], cur) |
| 69 | + // no further change for i' <= i |
| 70 | + if (newVal == f[i]) { |
| 71 | + break |
| 72 | + } |
| 73 | + delta += forbiddenCount(newVal, n) - forbiddenCount(f[i], n) |
| 74 | + cur = newVal |
| 75 | + } |
| 76 | + val newUnion = originalUnion + delta |
| 77 | + val newValid = totalSubarrays - newUnion |
| 78 | + best = max(best, newValid) |
| 79 | + } |
| 80 | + return best |
| 81 | + } |
| 82 | + |
| 83 | + private fun forbiddenCount(x: Int, n: Int): Long { |
| 84 | + return (if (x <= n) (n - x + 1) else 0).toLong() |
| 85 | + } |
| 86 | +} |
0 commit comments