codeforces round 879 div2 (a,b,c)

题目链接
中规中矩

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;
}
  • 17
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值