wa11求调
  • 板块CF1391D 505
  • 楼主Exile_Code
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/1/27 19:26
  • 上次更新2025/1/28 10:56:47
查看原帖
wa11求调
819682
Exile_Code楼主2025/1/27 19:26
#include <bits/stdc++.h>
using namespace std;


#define inf64 INT64_MAX/2
#define inf32 INT32_MAX/2
#define ll long long
#define int ll
#define pii pair<int,int>
#define endl '\n'
#define vv vector
#define cy cout<<"YES"<<endl
#define cn cout<<"NO"<<endl
#define its(a)  a.begin()+1,a.end()
#define minV *min_element
#define maxV *max_element
#define sumV(a) accumulate(its(a),0ll)
int _;
ll lcm(ll a, ll b) { return a / __gcd(a, b) * b; }
//数学公式
ll mpow(ll x, ll y, ll mod);




void solve(){


	int n, m; cin >> n >> m;

	vv<string>area(n + 2);
	map<pii, int>flag;
	for (int i = 0; i < n; i++) {
		cin >> area[i];
		area[i] = ")" + area[i];
	}
	auto ok2 = [&](int i, int j)->int {
		int f2 = 0;
		for (int k = 0; k < 2; k++) {
			if (i & (1ll << k))
				f2++;
			if (j & (1ll << k))
				f2++;
		}
		f2 %= 2;
		if (f2)
			return 1;
		else
			return 0;

	};
	auto ok3 = [&](int i, int j)->int {
		int f1 = 0, f2 = 0;
		for (int k = 0; k < 2; k++) {
			if (i & (1ll << k))
				f1++;
			if (j & (1ll << k))
				f1++;
		}

		for (int k = 1; k < 3; k++) {
			if (i & (1ll << k))
				f2++;
			if (j & (1ll << k))
				f2++;
		}
		f2 %= 2;		f1 %= 2;
		if (f1  && f2)
			return 1;
		else
			return 0;
	};
	auto cost2 = [&](int pos, int w) -> int {
		string now = bitset<2>(w).to_string();
		int cnt = 0;
		for (int i = 0; i < 2; i++) {
			if (now[i] != area[i][pos])
				cnt++;
		}
		return cnt;
	};
	
	auto cost3=[&](int pos, int w) -> int {
		string now = bitset<3>(w).to_string();
		int cnt = 0;
		for (int i = 0; i < 3; i++) {
			if (now[i] != area[i][pos])
				cnt++;
		}
		return cnt;
	};

	vv<vv<int>>dp(m + 1, vv<int>(8, 5e13));
	auto get2=[&]() -> void {
		for (int i = 0; i < 4; i++) {
			dp[1][i] = cost2(1, i);
		}
		for (int i = 2; i <= m; i++) {
			for (int k1 = 0; k1 < 4; k1++) {
				for (int k2 = 0; k2 < 4; k2++) {

					if (ok2(k1, k2)) {
						//cout << bitset<2>(k1).to_string() << endl << bitset<2>(k2).to_string() << endl;
						//cout << endl;
						dp[i][k2] = min(dp[i][k2], dp[i - 1][k1] + cost2(i, k2));
					}

				}
			}
		}
	};

	auto get3=[&]() -> void {
		for (int i = 0; i < 8; i++) {
			dp[1][i] = cost3(1, i);
		}
		for (int i = 2; i <= m; i++) {
			for (int k1 = 0; k1 < 8; k1++) {
				for (int k2 = 0; k2 < 8; k2++) {
				
					if (ok3(k1, k2)) {
						//cout << bitset<3>(k1).to_string() << endl << bitset<3>(k2).to_string() << endl;
						//cout << endl;
						dp[i][k2] = min(dp[i][k2], dp[i - 1][k1] + cost3(i, k2));
					}

				}
			}
		}
	};

	if (n > 4){
		cout << -1 << endl;
		return;
	}
	else if (n == 1) {
		cout << 0 << endl;
		return;
	}
	else if (n == 2) {
		get2();
		int mn = 1e9;
		for (int i = 0; i < 4; i++) {
			mn = min(dp[m][i], mn);
		}
		cout << mn << endl;
	}
	else if (n == 3) {
		get3();
		int mn = 1e9;
		for (int i = 0; i < 8; i++) {
			mn = min(dp[m][i], mn);
		}
		cout << mn << endl;
	}
	
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);


	_ = 1;
	//cin >> _;

	while (_--) {
		solve();
	}

	return 0;
}

ll mpow(ll x, ll y, ll mod) {
	x %= mod;
	ll s = 1;
	while (y) {
		if (y & 1) {
			s *= x;
			s %= mod;
		}
		x *= x;
		x %= mod;
		y >>= 1;
	}
	return s;
}
2025/1/27 19:26
加载中...