AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

Codeforces Round 964 (Div. 4) 题解

作者: 作者的头像   Bzdd_zc ,  2024-08-08 11:11:51 ,  所有人可见 ,  阅读 75


6


1

Codeforces Round 964 (Div. 4) 题解-初学者分享

A. A+B again

算法:模拟;数学
关键: / % 算术运算

题目理解:每轮测试用例给出一个两位正整数n,将个位数字和十位上的数字相加,并输出结果

#include <iostream>

using namespace std;

int t;
int num;

int main()
{
    // 提高输入输出(I/O)的效率。
    ios::sync_with_stdio(false); // 解除cin/cout 和 scanf/printf 同步
    cin.tie(nullptr); // 解除cin和cout绑定

    cin >> t;

    int ten, one, res;

    while (t -- ) {
        cin >> num;

        ten = num / 10; // 整除,取出十位上的数
        one = num % 10; // 取余/取模,取出个位上的数

        res = ten + one;

        cout << res << '\n';
    }

    return 0;
}

B. Card Game

算法:暴力枚举;模拟
关键:vector;pair

题目理解:两人四张牌,枚举翻拍顺序的所有可能。全排列4种情况,每种情况2次比较,每次比较记录胜者+1分。每次比较结束,如果Suneet(player1)分数多,则获胜。

#include <iostream>
#include <utility> // pair
#include <vector>

using namespace std;

typedef pair<int, int> PII;

int t, a1, a2, b1, b2;
vector<PII> player1, player2;
int p1_win, p2_win;
int res;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> t;
    while (t -- ) {
        cin >> a1 >> a2 >> b1 >> b2;

        player1 = {{a1, a2}, {a2, a1}}; // 存入翻牌情况
        player2 = {{b1, b2}, {b2, b1}};

        res = 0; // Suneet 会赢的游戏数量

        // 利用迭代器暴力枚举两人4种的翻牌情况
        for (auto &p1 : player1) {
            for (auto &p2 : player2) {
                p1_win = 0, p2_win = 0; // 每次比较获胜次数

                // 第一次翻牌比较
                if (p1.first > p2.first) { p1_win ++ ; } 
                else if (p1.first < p2.first) { p2_win ++ ; }

                // 第二次翻牌比较
                if (p1.second > p2.second) { p1_win ++ ; }
                else if (p1.second < p2.second) { p2_win ++ ; }

                // 若Suneet获胜,则+1
                if (p1_win > p2_win) { res ++ ; }
            }
        }

        cout << res << '\n';
    }

    return 0;
}

C. Showering

算法:模拟
关键:pair,存数据的方式

题目理解:m分钟的时间轴,被n个时间段占据,未被占据的时间段是否大于s洗澡时间

#include <iostream>
#include <utility>

using namespace std;

typedef pair<int, int> PII;

const int N = 2e5 + 10; // n 的max值范围
int t;
int n, s, m;
int l, r;
PII a[N]; // pair 数组

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> t;
    while (t -- ) {
        cin >> n >> s >> m;

        bool flag = false; // 记录是否能完成洗澡
        // vector<PII> a(n); 用vector需要(n),就可以根据下标来赋值
        for (int i = 0; i < n; i ++ ) {
            cin >> a[i].first >> a[i].second; // 因为输入限制已经为从小到大,所以省略了sort
        }

        // 判断 所有任务开始前的时间是否满足
        if (a[0].first >= s) {
            flag = true;
        }

        // 每个任务之间的时间是否满足
        for (int i = 1; i < n; i ++ ) {
            if (a[i].first - a[i - 1].second >= s) {
                flag = true;
                //
                break;
            }
        }

        // 任务结束后到m分钟之间的时间是否满足
        if (m - a[n - 1].second >= s) {
            flag = true;
        }

        cout << (flag ? "Yes" : "No") << '\n';
    }

    return 0;
}

D. Slavic’s Exam

算法:贪心;字符串处理;双指针;模拟;
关键:string;双指针维护;贪心思想

题目理解:更改’s’字符串中的’?’,使得’t’可以成为’s’的字串

#include <iostream>
#include <string>

using namespace std;

int T;
string s, t;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> T;
    while (T -- ) {
        cin >> s >> t;

        int idx = 0; // t 的指针下标
        for (int i = 0; i < s.size(); i ++ ) { // s 指针下标
            if (s[i] == '?') { // 需要更改
                if (idx < t.size()) { s[i] = t[idx ++ ]; } // 还没匹配完成,所以进行匹配,并将idx递增
                else { s[i] = 'a'; } // 已经匹配完成,但是'?'还是存在,更改'?'
            } else if (s[i] == t[idx]) { idx ++ ;} // 原字符串已经匹配好,则直接将idx递增;若(s[i] != t[idx] && s[i] != '?')则不用改变s, i递增
        }

        // idx只有'?'和相等的情况下,才会递增
        if (idx >= t.size()) {
            cout << "Yes" << '\n' << s << '\n';
        } else { // '?' 已无,还未将t匹配到s中
            cout << "No" << '\n';
        }
    }

    return 0;
}

E.F.G1.G2.

5 评论


用户头像
Return.   2024-08-09 00:28      1    踩      回复

贴个完整的【codeforces Round 964 div4 (A~G2) - CSDN App】http://t.csdnimg.cn/kMR24🥳

用户头像
Bzdd_zc   2024-08-09 09:00         踩      回复

orz


用户头像
DKing2917   2024-08-08 12:59      1    踩      回复

学了多久?www

用户头像
Bzdd_zc   2024-08-08 19:29      1    踩      回复

自己效率特别慢,两天打鱼三天晒网,也学了半年了

用户头像
DKing2917   2024-08-08 20:16    回复了 Bzdd_zc 的评论         踩      回复

hhh我现在就是这个状态,学得太慢


App 内打开
你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息