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;
}

相关推荐

  1. Codeforces Round 941 (Div. 2) ABC

    2024-05-04 18:42:03       36 阅读
  2. codeforces round 879 div2 (a,b,c)

    2024-05-04 18:42:03       33 阅读
  3. Codeforces Round 958 (Div. 2)[部分题解ABC]

    2024-05-04 18:42:03       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-04 18:42:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-04 18:42:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-04 18:42:03       82 阅读
  4. Python语言-面向对象

    2024-05-04 18:42:03       91 阅读

热门阅读

  1. 第Ⅰ章-V package.json文件详解

    2024-05-04 18:42:03       26 阅读
  2. 爬⾍监控与⾃动恢复机制

    2024-05-04 18:42:03       30 阅读
  3. Nodejs-异步并发控制

    2024-05-04 18:42:03       30 阅读
  4. 分割等和子集

    2024-05-04 18:42:03       33 阅读
  5. Vue3 + TS + Element-Plus 封装的 Table 表格组件

    2024-05-04 18:42:03       23 阅读
  6. python调用微信自带OCR实现内容识别(全)

    2024-05-04 18:42:03       29 阅读
  7. 2024.4.28力扣每日一题——负二进制转换

    2024-05-04 18:42:03       30 阅读
  8. OneFlow深度学习框架入门与实践

    2024-05-04 18:42:03       36 阅读
  9. 云计算技术概述_3.云计算的部署方式

    2024-05-04 18:42:03       28 阅读