[Question Solution] LuoGu3943: Starry Sky

The original title portal
because k <= 8 k<=8k<=8 so think of

The main idea is to
make the light dim to 1, and the light to 0. For
example, the case of a row of lights is
a: 01001100 a:01001100a:0 1 0 0 1 1 0 0
Then the corresponding difference array is
b: 011010100 b:011010100b:011010100
可以发现 a i = b 1 x o r b 2 x o r . . . x o r b i a_i=b_1xorb_2xor...xorb_i ai=b1x o r b2x or r . . . x o r bi
Then for the interval [l, r] [l, r][l,r ] performs the inversion operation, the performance in the difference array is
bl = blxor 1, br + 1 = br + 1 xor 1 b_l=b_lxor1,b_{r+1}=b_{r+1}xor1bl=blx o r 1 ,br+1=br+1x o r 1

The problem can be transformed into bbb array becomes all 0
findbbThe number of b array 1 must be an even number, and at most2 k 2k2 K th
conceivable to eliminate every two 1, to eliminate the cost of both a pre-determined by the next state is pressed

If you want to put 10000001 100000011 0 0 0 0 0 0 1 becomes all 0s, but I can only flip the length to 3, 4
can first00010001 000100010 0 0 1 0 0 0 1 means to[1, 3] [1, 3][1,3 ] Flip
again00000000 000000000 0 0 0 0 0 0 0 means to[4, 7] [4, 7][4,7 ] Flip to
indicate that the difference between the two isxxx of111. To change them to 0, choosex = b 1 + b 2 +... + Byx=b_1+b_2+...+b_yx=b1+b2+...+band
Multiple flip combinations are possible

Or put 00110000 001100000 0 1 1 0 0 0 0 becomes all 0s, but I can only flip the length to 3, 4
can first00010010 000100100 0 0 1 0 0 1 0 means to[3, 6] [3, 6][3,6 ] Flip
again00000000 000000000 0 0 0 0 0 0 0 means to[4, 6] [4, 6][4,6 ] Reversal
shows that subtraction between reversal interval lengths is also possible

So for a certain length, pass bbYou can get your own length by adding, adding, subtracting, and subtracting the b array. Find the minimum number of flips in a reasonable situation, and get it through a complete backpack

Code:

#include <bits/stdc++.h>
#define maxn 40010
#define maxm 100010
using namespace std;
int a[maxn], dp[maxm], cost[maxn], b[maxn], pos[maxn], power[25], tot, n, m, k;

inline int read(){
    
    
	int s = 0, w = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
	for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
	return s * w;
}

int main(){
    
    
	n = read(), k = read(), m = read();
	for (int i = 1; i <= k; ++i){
    
    
		int x = read();
		a[x] ^= 1, a[x + 1] ^= 1;
	}
	for (int i = 1; i <= n; ++i) cost[i] = 1e9;
	for (int i = 1; i <= m; ++i) b[i] = read();
	for (int i = 1; i <= m; ++i)
		for (int j = b[i]; j <= n; ++j) cost[j] = min(cost[j], cost[j - b[i]] + 1);
	for (int i = 1; i <= m; ++i)
		for (int j = n - b[i]; j >= 0; --j) cost[j] = min(cost[j], cost[j + b[i]] + 1);
	for (int i = 1; i <= n + 1; ++i)
		if (a[i]) pos[++tot] = i;
	power[0] = 1;
	for (int i = 1; i <= 20; ++i) power[i] = power[i - 1] << 1;
	for (int i = 1; i < power[tot]; ++i) dp[i] = 1e9;
	for (int i = 0; i < power[tot]; ++i)
		for (int j = 1; j <= tot; ++j)
			if (!(i & power[j - 1]))
				for (int k = j + 1; k <= tot; ++k)
					if (!(i & power[k - 1]))
						dp[i | power[j - 1] | power[k - 1]] = min(dp[i | power[j - 1] | power[k - 1]], dp[i] + cost[pos[k] - pos[j]]);
	printf("%d\n", dp[power[tot] - 1]);
	return 0;
}

Guess you like

Origin blog.csdn.net/ModestCoder_/article/details/108491966