Codeforces Round #734 (Div. 3) A~D1 个人题解

比赛链接:Here

1551A. Polycarp and Coins (签到)

题意:

我们有任意个面额为 12 的硬币去支付 n 元账单,

现在请问怎么去分配数额使得 c1+2c2=n 并且要最小化 |c1c2|


贪心,

很容易想到最小化 |c1c2| 等于 01

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int _; for (cin >> _; _--;) {
        int n;
        cin >> n;
        if (n % 3 == 0) cout << n / 3 << " " << n / 3 << "\n";
        else if (n % 3 == 1) cout << n / 3 + 1 << " " << n / 3 << "\n";
        else cout << n / 3 << " " << n / 3 + 1 << "\n";
    }
}

1551B1. Wonderful Coloring - 1(easy)

题意:

给定长度为 n 的字符串和 k=2 种颜色,

对于字符串每一个位置要么不涂颜色,要么每种颜色的字母都要不同并且最后 k 种颜色涂的个数相同

请输出每种颜色最后的个数


统计 26 个字母出现次数,然后出现超过 2 的+1,仅出现一次的个数 / 2即可(易证明)

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int _; for (cin >> _; _--;) {
        string s;
        cin >> s;
        int a[30] = {0};
        for (int i = 0; i < int(s.length()); ++i) {
            a[s[i] - 'a']++;
        }
        int a2 = 0, a1 = 0;
        for (int i = 0; i < 26; ++i) {
            if (a[i] == 1)a1++;
            if (a[i] > 1)a2++;
        }
        cout << a1 / 2 + a2 << "\n";
    }
}

1551B2. Wonderful Coloring - 2 (hard版本)

题意基本同 B1 一致,

但需输出每个位置数字涂的颜色


写了两罚模拟 O(n2) 不出所料肯定 T了,

转换一下思路,首先和 B1 一样我们需要去统计每种数字的个数,但由于最后需要输出每个位置的颜色编号,所以还得存一下下标 (pos)

  • cnt[a[i]]k 的将下标存储
  • cnt[a[i]]>k 无需使用标 0

当然对于多余的元素,可以直接从删除

while (pos.size() % k != 0)pos.pop_back();

此时 pos 中所有元素均为有效下标,我们只要对应原数组标记颜色编号即可

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int _; for (cin >> _; _--;) {
        int n, k;
        cin >> n >> k;
        vector<int>a(n), cnt(n);
        vector<int>pos;
        for (int i = 0; i < n; ++i) {
            cin >> a[i], a[i] -= 1;
            cnt[a[i]] += 1;
            if (cnt[a[i]] <= k) pos.push_back(i);
        }
        while (pos.size() % k != 0)pos.pop_back();
        sort(pos.begin(), pos.end(), [&](int i, int j) {return a[i] < a[j];});
        vector<int>ans(n);
        for (int i = 0; i < pos.size(); ++i)
            ans[pos[i]] = i % k + 1;
        for (int i = 0; i < n; ++i)
            cout << ans[i] << " \n"[i == n - 1];
    }
}

1551C. Interesting Story

题意:

一个作家有 n 个单词,每一个单词只会由 a,b,c,d,e 构成,他想用 m(0mn) 个单词写一个故事

一个故事是否有趣在于:

  • 故事中存在一个字母的个数比其他字母个数的总和还更多

    比如:bac,aaada,e 三个单词,a 出现的次数比其他 4 种单词出现次数都多

请输出在保证故事有趣的情况下用更多的单词写故事


这里建议直接看代码便于理解

【AC Code】代码参考于比赛 高Rank dalao

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int _; for (cin >> _; _--;) {
        int n;
        cin >> n;
        vector<string>s(n);
        for (int i = 0; i < n; ++i) cin >> s[i];
        int ans = 0;
        for (int c = 0; c < 5; ++c) {
            vector<int>f(n);
            for (int i = 0; i < n; ++i) {
                for (auto x : s[i])
                    if (x == 'a' + c)f[i]++;
                    else f[i]--;
            }

            sort(f.begin(), f.end(), greater<int>());
            int sum  = 0;
            for (int i = 0; i < n; ++i) {
                sum += f[i];
                if (sum <= 0)break;
                ans = max(ans, i + 1);
            }
        }
        cout << ans << "\n";
    }
}

很好奇自己是怎么读题读成使用连续的单词去写故事的啊?kola

1551D1. Domino (easy version)

题意:待补


多米诺骨牌的排放似乎在ABC的某一场也出现过

如果 n 为奇数

  • 最少要横放 m/2 个,不然最少 0
  • 最多 nm/2=(m%2==1? n/2:0)

对于给出的 k 只要在 [mn,mx] 就输出 YES

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int _; for (cin >> _; _--;) {
        int n, m, k;
        cin >> n >> m >> k;

        int mn = (n % 2 == 1 ? m / 2 : 0);
        int mx = n * m / 2 - (m % 2 == 1 ? n / 2 : 0);
        if (mn <= k and k <= mx and (k - mn) % 2 == 0) cout << "YES\n";
        else cout << "NO\n";
    }
}
posted @   RioTian  阅读(216)  评论(4编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 全程不用写代码,我用AI程序员写了一个飞机大战
历史上的今天:
2020-07-24 常用代码模板4——数学知识
2020-07-24 常用代码模板3——搜索与图论
2020-07-24 常用代码模板2——数据结构
2020-07-24 常用代码模板1——基础算法
点击右上角即可分享
微信分享提示

📖目录