题目链接
中规中矩
A
小模拟
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
int n;cin >> n;
int n1, n2;n1 = n2 = 0;
for (int i = 1;i <= n;i++) {
int p;cin >> p;
if (p == 1)n1++;
else n2++;
}
if (n2 % 2 == 0 && n1 >= n2) {
cout << 0 << '\n';
return;
}
int ans = 0;
if (n1 < n2) {
ans += (n2 - n1) / 2;
if ((n2 - n1) % 2 == 1 && n2 - (n2 - n1) / 2 > n1 + (n2 - n1) / 2)ans++, n2--;
n2 -= (n2 - n1) / 2;
}
if (n2 % 2 == 1)ans++;
cout << ans << '\n';
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;cin >> t;
while (t--) {
solve();
}
return 0;
}
B
先补全 s s s令其与 t t t一样长,再找到第一个不相等的字符,该位置的字符对答案的贡献为 a b s ( s [ i ] − t [ i ] ) abs(s[i]-t[i]) abs(s[i]−t[i]), i i i~ s . s i z e ( ) s.size() s.size()每一对都可以构造成 0 , 9 0,9 0,9,故对答案贡献为 9 ∗ ( s . s i z e ( ) − i ) 9*(s.size()-i) 9∗(s.size()−i)
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
string s, t;cin >> s >> t;
if (s == t) {
cout << 0 << '\n';
return;
}
int ans = 0;
while (s.size() < t.size())s = '0' + s;
int i = 0;
while (s[i] == t[i])i++;
ans += abs(s[i] - t[i]);
i++;
ans += 9 * (s.size() - i);
cout << ans << '\n';
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;cin >> t;
while (t--) {
solve();
}
return 0;
}
C
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
int n;cin >> n;
string s, t;cin >> s >> t;
if (s == t) {
cout << 0 << '\n';
return;
}
int ans = 0;
int ou = 1e9;
for (int i = 0;i < n;i++)ans += (s[i] != t[i]);
if (ans == 0)ou = min(ou, 0ll);
else if (ans & 1) {
ou = min(ou, ans + ans - 1);
}
else {
ou = min(ou, ans + ans);
}
ans = 0;
reverse(s.begin(), s.end());
for (int i = 0;i < n;i++)ans += (s[i] != t[i]);
if (ans == 0)ou = min(2ll, ou);
else if (ans & 1) {
ou = min(ou, ans + ans);
}
else {
ou = min(ou, ans + ans - 1);
}
cout << ou << '\n';
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;cin >> t;
while (t--) {
solve();
}
return 0;
}