voidsolve(){ int _; cin >> _; for (int tc = 0; tc < _; ++tc) { int mi = 1000, ma = -1000; for (int i = 0; i < 4; ++i) { int u, v; cin >> u >> v; mi = min(mi, v); ma = max(ma, v); } cout << (ma - mi) * (ma - mi) << endl; } }
B. Arranging Cats
大致题意
有两个 01 字符串,允许对第一个字符串进行如下操作
将一个 1 变成 0
将一个 0 变成 1
将一个 1 和另外一个 0 交换一下位置
问最多操作几次能让两个字符串相同
思路
多用第三个方法即可,统计 1 的数量即可
AC code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
voidsolve(){ int _; cin >> _; for (int tc = 0; tc < _; ++tc) { int n; cin >> n; string str1, str2; str1.resize(n); str2.resize(n); cin >> str1 >> str2; int cnt[2][2] = {}; for (int i = 0; i < n; ++i) { if (str1[i] == str2[i]) continue; ++cnt[0][str1[i] - '0']; ++cnt[1][str2[i] - '0']; } cout << max(cnt[0][1], cnt[1][1]) << endl; } }
voidsolve(){ int _; cin >> _; for (int tc = 0; tc < _; ++tc) { int n, f, a, b; cin >> n >> f >> a >> b; int last = 0; for (int i = 0; i < n; ++i) { int cur; cin >> cur; f -= min(b, a * (cur - last)); last = cur; } cout << (f > 0 ? "YES" : "NO") << endl; } }
voidsolve(){ int _; cin >> _; for (int tc = 0; tc < _; ++tc) { int n, m; cin >> n >> m; vector<int> a(n), b(m); for (auto& i: a) cin >> i; for (auto& i: b) cin >> i; sort(a.begin(), a.end()); sort(b.begin(), b.end()); int l = 0, r = 0, ans = 0; while (l + r < n) { if (abs(a[l] - b[m - l - 1]) > abs(a[n - r - 1] - b[r])) { ans += abs(a[l] - b[m - l - 1]); ++l; } else { ans += abs(a[n - r - 1] - b[r]); ++r; } } cout << ans << endl; } }
voidsolve(){ int _; cin >> _; for (int tc = 0; tc < _; ++tc) { int n, m, k; cin >> n >> m >> k; vector<string> map(n); for (auto& s: map) { s.resize(m); cin >> s; }
vector<vector<int>> h(n), v(n), r(n); for (auto& i: h) i.resize(m, 0); for (auto& i: v) i.resize(m, 0); for (auto& i: r) i.resize(m, 0); auto cal = [&](vector<vector<bool>> &mp) { for (int i = 0; i < n; ++i) { h[i][0] = v[i][0] = r[i][0] = mp[i][0]; h[i][m - 1] = v[i][m - 1] = r[i][m - 1] = mp[i][m - 1]; } for (int j = 0; j < m; ++j) { h[0][j] = v[0][j] = r[0][j] = mp[0][j]; h[n - 1][j] = v[n - 1][j] = r[n - 1][j] = mp[n - 1][j]; }
for (int i = 0; i < n; ++i) for (int j = 1; j < m; ++j) h[i][j] = h[i][j - 1] + mp[i][j]; for (int j = 0; j < m; ++j) for (int i = 1; i < n; ++i) v[i][j] = v[i - 1][j] + mp[i][j]; for (int i = 1; i < n; ++i) for (int j = m - 2; j >= 0; --j) r[i][j] = r[i - 1][j + 1] + mp[i][j];
vector<vector<int>> ans(n); for (auto& i: ans) i.resize(m, 0); int res = 0;
// tl ans[0][0] = 0; for (int i = 0; i <= min(k, n - 1); ++i) for (int j = 0; j <= min(k - i, m - 1); ++j) ans[0][0] += mp[i][j]; for (int i = 0; i < n; ++i) { if (i != 0) { ans[i][0] = ans[i - 1][0]; ans[i][0] -= h[i - 1][min(k, m - 1)]; constint out = max(i + k - n + 1, 0); if (out >= m) continue; ans[i][0] += r[i + k - out][out] - (k + 1 >= m ? 0 : r[i - 1][k + 1]); } for (int j = 1; j < m; ++j) { ans[i][j] = ans[i][j - 1]; ans[i][j] -= v[min(i + k, n - 1)][j - 1] - (i == 0 ? 0 : v[i - 1][j - 1]); if (j + k >= m + n - 1 - i) continue; constint out = max(i + k - n + 1, 0); ans[i][j] += r[i + k - out][j + out] - (i == 0 || j + k + 1 >= m ? 0 : r[i - 1][j + k + 1]); } } for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) res = max(res, ans[i][j]); #ifdef ACM_LOCAL for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int tmp = 0; for (int a = 0; i + a < n && a <= k; ++a) for (int b = 0; b + a <= k && j + b < m; ++b) tmp += mp[i + a][j + b]; if (tmp != ans[i][j]) cerr << "tl: " << i << ' ' << j << ' ' << tmp << '-' << ans[i][j] << endl; } } #endif return res; };
vector<vector<bool>> mp; mp.resize(n); for (auto &i: mp) i.resize(m); int ans = 0;
// 1 for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) mp[i][j] = map[i][j] == '#'; ans = max(cal(mp), ans);
// 2 for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) mp[i][j] = map[i][m - j - 1] == '#'; ans = max(cal(mp), ans);
// 3 for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) mp[i][j] = map[n - i - 1][j] == '#'; ans = max(cal(mp), ans);
// 4 for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) mp[i][j] = map[n - i - 1][m - j - 1] == '#'; ans = max(cal(mp), ans);