WaterAngel题解Educational Codeforces Round 129 (Rated for Div. 2) A-D

本文解析了四道算法竞赛题目,包括一招制胜策略、简单魔术计算、双数组排序同步操作及深度优先搜索应用。通过代码示例详细阐述了解题思路与实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A : 题目意思其实就是看看谁一招制胜,谁能先出自己最大的牌让对手无牌可出就行,所以对于Alice先手,只要把最大的牌直接打就行,不小于Bob最大的就行,反正同理。

#include <iostream>
using namespace std;
int max(int a, int b) {
    if (a > b)
        return a;
    return b;
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        int a, b;
        int x, VA = 0, VB = 0;
        cin >> a;
        for (int i = 1; i <= a; i++) {
            cin >> x;
            VA = max(x, VA);
        }
        cin >> b;
        for (int i = 1; i <= b; i++) {
            cin >> x;
            VB = max(x, VB);
        }
        if (VA > VB) {
            cout << "Alice" << endl;
            cout << "Alice" << endl;
        }
        if (VA < VB) {
            cout << "Bob" << endl;
            cout << "Bob" << endl;
        }
        if (VA == VB) {
            cout << "Alice" << endl;
            cout << "Bob" << endl;
        }
    }
}

B : 说实话我没想那么多,一看标题魔术就知道是可以现实中操作的,说明只需要简单的计算。不过这题数据也太小了,嗯模都行。

#include <iostream>
#define int long long
using namespace std;
signed main() {
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        int a[200010], x, sum = 0, op;
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        cin >> m;
        for (int i = 1; i <= m; i++) {
            cin >> x;
            sum += x;
            sum = sum % n;
        }
        cout << a[sum + 1] << endl;
    }
    return 0;
}

C : 对于这俩数组,其实只用对其中一个执行选择排序,同步操作另外一个,然后执行选择排序另一个,最后检查最后输出步骤。

碰到的唯一问题就是例如排序完成后是A ={2,2,2,3,4,5},B是{2,1,2,3,4,5},此时如果直接做最终检查,而不去排序B,就会被卡,当时做题太想当然了没考虑到,WA了好久,还以为是评测机问题。

#include <iostream>
using namespace std;
int a[110], b[110], r[10004][2];
int swap(int n) {
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = i; j <= n; j++) {
            if (a[i] > a[j]) {
                int t = a[i];
                a[i] = a[j];
                a[j] = t;
 
                t = b[i];
                b[i] = b[j];
                b[j] = t;
                cnt++;
                r[cnt][0] = j;
                r[cnt][1] = i;
            }
        }
    }
    return cnt;
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        for (int i = 1; i <= n; i++) {
            cin >> b[i];
        }
        int cnt = swap(n);
        for (int i = 1; i <= n; i++) {
            for (int j = i; j <= n; j++) {
                if (b[i] > b[j]) {
                    int t = b[i];
                    b[i] = b[j];
                    b[j] = t;
 
                    t = a[i];
                    a[i] = a[j];
                    a[j] = t;
 
                    cnt++;
                    r[cnt][0] = j;
                    r[cnt][1] = i;
                }
            }
        }
 
        bool Fj = true;
        for (int i = 2; i <= n; i++) {
            if (a[i] < a[i - 1] || b[i] < b[i - 1]) {
                Fj = false;
                break;
            }
        }
        if (!Fj || cnt > 10000) {
            cout << "-1" << endl;
        } else {
            cout << cnt << endl;
            for (int i = 1; i <= cnt; i++) {
                cout << r[i][0] << ' ' << r[i][1] << endl;
            }
        }
    }
    return 0;
}

D : 深度优先搜索dfs(num , step),num表示的是现在得到的数字,step是执行的步骤,只需要把num分解出可用的数字,然后搜索就行,值得注意的是初始化,不要影响到上一层能做的事情,不然就变成了贪心,而贪心恰好可以过样例,就很坏~

为了初始化,我想了最简单的办法就是用二维数组,反正数据也不大。

三种Return分别用C123标出

#include <string.h>
#include <cstdio>
#include <iostream>
#define int long long
using namespace std;
bool avb[200][20];
int n, x, res = 0x7fffff;
void dfs(int num, int step) {
    int check = num, cntl = 0;

    for (int i = 0; i <= 10; i++) {
        avb[step][i] = false;
    }  //初始化

    if (step > res)
        return;  // C1 : 已经超出了当前最优解

    while (check) {
        avb[step][check % 10] = true;  //这个数字可以用
        check /= 10;
        cntl++;
    }
    if (cntl == n) {
        res = step;
        return;
    }  // C2 : 目标达成
    if ((n - cntl) + step >= res)
        return;  // C3 : 用这个数字一直乘也不能得到更优的解
    for (int i = 9; i >= 2; i--) {
        if (avb[step][i]) {
            dfs(num * i, step + 1);
            if ((n - cntl) + step >= res)  // C3
                return;
        }
    }
}
signed main() {
    cin >> n >> x;
    dfs(x, 0);
    if (res == 0x7fffff) {
        cout << "-1" << endl;
    } else
        cout << res << endl;
    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值