ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-05-25 02:32:26  阅读:188  来源: 互联网

标签:tmp Educational Rated minpos cout int Codeforces cin ++


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

A

题目

https://codeforces.com/contest/1681/problem/A

题解

思路

知识点:贪心。

先手的一方拥有大于等于对方最大牌的牌即可获胜,所以考虑取两组牌各自的最大值进行比较。

时间复杂度 \(O(n)\)

空间复杂度 \(O(1)\)

代码

#include <bits/stdc++.h>
#define ll long long

using namespace std;

bool solve() {
    int a = 0, b = 0;
    int n;
    cin >> n;
    for (int i = 0, tmp = 0;i < n;i++) cin >> tmp, a = max(a, tmp);
    int m;
    cin >> m;
    for (int j = 0, tmp = 0;j < m;j++) cin >> tmp, b = max(b, tmp);
    if (a >= b) cout << "Alice" << '\n';
    else cout << "Bob" << '\n';
    if (b >= a) cout << "Bob" << '\n';
    else cout << "Alice" << '\n';
    return true;
}

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) {
        if (!solve()) cout << -1 << '\n';
    }
    return 0;
}

B

题目

https://codeforces.com/contest/1681/problem/B

题解

思路

知识点:模拟,数学。

注意到每次取的牌顺序不变放入排堆尾部,因此是一个循环,于是交换总和除以牌总数的余数 \(sum \% n\) 即等效交换次数,而对应的数组元素即交换后的牌顶元素。

时间复杂度 \(O(n)\)

空降复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int a[200007];

bool solve() {
    int n;
    cin >> n;
    for (int i = 0;i < n;i++) cin >> a[i];
    int m;
    ll sum = 0;
    cin >> m;
    for (int i = 0, tmp = 0;i < m;i++) cin >> tmp, sum += tmp;
    cout << a[sum % n] << '\n';
    return true;
}

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) {
        if (!solve()) cout << -1 << '\n';
    }
    return 0;
}

C

题目

https://codeforces.com/contest/1681/problem/C

题解

思路

知识点:模拟,排序。

注意到 \(n\) 最大只有 \(100\) ,允许交换操作最大能到 \(10^4\) 而且不需要最小化交换次数,显然可以考虑先将数组 \(a\) 进行选择排序(相等元素是不需要交换的,保险点防止炸 \(10^4\) ),并在交换过程中同时交换 \(b\) 相同位置的元素。然后对数组 \(b\) 用选择排序进行检查,并且交换只能在相同位置数组 \(a\) 元素相等时才行,如果某次交换因为 \(a\) 限制不可操作,则一定判失败;否则记录所有结果输出即可。

时间复杂度 \(O(n^2)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int a[107], b[107];

bool solve() {
    vector<pair<int, int>> v;
    int n;
    cin >> n;
    for (int i = 0;i < n;i++)cin >> a[i];
    for (int i = 0;i < n;i++)cin >> b[i];
    for (int i = 0;i < n - 1;i++) {
        int minpos = i;
        for (int j = i + 1;j < n;j++) {
            minpos = a[minpos] > a[j] ? j : minpos;
        }
        if (i != minpos) swap(a[i], a[minpos]), swap(b[i], b[minpos]), v.push_back({ i,minpos });
    }
    for (int i = 0;i < n - 1;i++) {
        int minpos = i;
        for (int j = i + 1;j < n;j++) {
            minpos = b[minpos] > b[j] ? j : minpos;
        }
        if (i != minpos) {
            if (a[i] == a[minpos]) swap(b[i], b[minpos]), v.push_back({ i,minpos });
            else return false;
        }
    }
    cout << v.size() << '\n';
    for (int i = 0;i < v.size();i++) {
        cout << v[i].first + 1 << ' ' << v[i].second + 1 << '\n';
    }
    return true;
}

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) {
        if (!solve()) cout << -1 << '\n';
    }
    return 0;
}

D

题目

https://codeforces.com/contest/1681/problem/D

题解

思路

知识点:搜索,暴力。

首先注意到每次贪心地取最大数位去乘是不可行的,比如有 \(5 \times 5 > 9 \times 2\) 如果先选 \(9\) 后只有 \(2\) 那就没有选两次 \(5\) 更优。

因此,考虑用dfs枚举每种可能,当然直接枚举会tle,有一个重要剪枝 if (step + n - len >= ans) return; 意思是已走步数 step 加上至少还需要的步数 n-len 如果大于等于当前答案 ans 那这条分支就是无用的,这个叉掉很多分支。

当然,也可以用bfs记忆化搜索,直接bfs也会超时,还可能炸空间qwq。

时间复杂度 \(O(8^n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
#define ll long long

using namespace std;

ll n, x, ans = 100;
void dfs(ll cur = x, ll step = 0) {
    bool vis[10] = { 0 };
    ll tmp = cur, len = 0;
    while (tmp) {
        vis[tmp % 10] = 1;
        tmp /= 10;
        len++;
    }
    if (step + n - len >= ans) return;
    if (n == len) {
        ans = step;
        return;
    }
    for (int i = 9;i >= 2;i--) if (vis[i]) dfs(cur * i, step + 1);
}
bool solve() {
    cin >> n >> x;
    dfs();
    if (ans >= 100) return false;
    cout << ans << '\n';
    return true;
}

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) {
        if (!solve()) cout << -1 << '\n';
    }
    return 0;
}

标签:tmp,Educational,Rated,minpos,cout,int,Codeforces,cin,++
来源: https://www.cnblogs.com/BlankYang/p/16307888.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有