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