import java.util.*; public class Solution { p...
作成日: 2025年7月12日
使用モデル O4 Mini High by Chat01
作成日: 2025年7月12日
使用モデル O4 Mini High by Chat01
import java.util.*;
public class Solution {
public static long maxSumAfterOps(int[] a, int[] b) {
int n = a.length, m = b.length;
long best = Long.MIN_VALUE;
int totalMasks = (int) Math.pow(3, m);
textfor (int mask = 0; mask < totalMasks; mask++) { int[] ops = new int[m]; int t = mask; for (int j = 0; j < m; j++, t /= 3) { ops[j] = t % 3; // 0→op1, 1→op2, 2→op3 } long[] cur = new long[n]; for (int i = 0; i < n; i++) cur[i] = a[i]; for (int j = 0; j < m; j++) { long x = b[j]; switch (ops[j]) { case 0: long bestLocal = Long.MIN_VALUE; int bestL = 0, bestR = -1; for (int L = 0; L < n; L++) { for (int R = L; R < n; R++) { long sum = 0; for (int i = 0; i < n; i++) { long v = cur[i]; if (i >= L && i <= R) v ^= x; else if (i > R) v ^= 2 * x; sum += v; } if (sum > bestLocal) { bestLocal = sum; bestL = L; bestR = R; } } } // apply best op1 for (int i = 0; i < n; i++) { if (i >= bestL && i <= bestR) cur[i] ^= x; else if (i > bestR) cur[i] ^= 2 * x; } break; case 1: long bestLocal2 = Long.MIN_VALUE; int bestStart = 0; for (int L = 0; L < n; L++) { long sum = 0; for (int i = 0; i < n; i++) { long v = cur[i]; if (i >= L) v ^= 2 * x; sum += v; } if (sum > bestLocal2) { bestLocal2 = sum; bestStart = L; } } // apply best op2 for (int i = bestStart; i < n; i++) { cur[i] ^= 2 * x; } break; case 2: // do nothing break; } } long total = 0; for (long v : cur) total += v; best = Math.max(best, total); } return best; } public static void main(String[] args) { int[] a = {5, 1, 3, 7}; int[] b = {2, 4}; System.out.println(maxSumAfterOps(a, b)); }
}
can you optimize for l and r currently using brute force but I want it to be O((3^m).n)
You can eliminate the brute-force over in your “op1” case by observing that
Let
java// the gain if you XOR x at position i long[] diff1 = new long[n]; for(int i=0;i<n;i++) diff1[i] = (cur[i]^x) - cur[i]; // the gain if you XOR 2*x at position i long[] diff2 = new long[n]; for(int i=0;i<n;i++) diff2[i] = (cur[i]^(2*x)) - cur[i];
Then
You can find
in a single scan by maintaining:
diff1
,diff2
.Likewise your “op2” (case 1) is just “pick the best suffix” of , also doable in .
Putting it all together gives you an (up to a factor ) solution:
javapublic static long maxSumAfterOps(int[] a, int[] b) { int n = a.length, m = b.length; long best = Long.MIN_VALUE; int totalMasks = (int) Math.pow(3, m); for (int mask = 0; mask < totalMasks; mask++) { // decode ops int[] ops = new int[m]; for (int j = 0, t = mask; j < m; j++, t /= 3) { ops[j] = t % 3; } // working copy of a[] long[] cur = new long[n]; for (int i = 0; i < n; i++) cur[i] = a[i]; // apply each operation in O(n) for (int j = 0; j < m; j++) { long x = b[j]; if (ops[j] == 0) { // -- op1: choose best (L,R) long[] diff1 = new long[n], suffix2 = new long[n+1]; for (int i = 0; i < n; i++) { diff1[i] = (cur[i] ^ x) - cur[i]; } suffix2[n] = 0; for (int i = n - 1; i >= 0; i--) { suffix2[i] = suffix2[i+1] + ((cur[i] ^ (2*x)) - cur[i]); } long maxGain = Long.MIN_VALUE; long prefixSum = 0, minPrefix = 0; int minPos = -1, bestL = 0, bestR = 0; for (int r = 0; r < n; r++) { prefixSum += diff1[r]; long gain = prefixSum - minPrefix + suffix2[r+1]; if (gain > maxGain) { maxGain = gain; bestL = minPos + 1; bestR = r; } if (prefixSum < minPrefix) { minPrefix = prefixSum; minPos = r; } } // apply the XORs in one more O(n) for (int i = 0; i < n; i++) { if (i >= bestL && i <= bestR) cur[i] ^= x; else if (i > bestR) cur[i] ^= 2*x; } } else if (ops[j] == 1) { // -- op2: choose best suffix long[] diff2 = new long[n]; for (int i = 0; i < n; i++) { diff2[i] = (cur[i] ^ (2*x)) - cur[i]; } long maxGain = Long.MIN_VALUE; long suffixSum = 0; int bestStart = 0; for (int i = n - 1; i >= 0; i--) { suffixSum += diff2[i]; if (suffixSum > maxGain) { maxGain = suffixSum; bestStart = i; } } for (int i = bestStart; i < n; i++) { cur[i] ^= 2*x; } } // else ops[j] == 2 → do nothing } // compute final total long total = 0; for (long v : cur) total += v; best = Math.max(best, total); } return best; }
Complexity breakdown
diff
arrays,