Codeforces Round #786 (Div. 3)

本文介绍了五个不同编程题目,涉及数论运算、字符串处理、替换操作、数组排序和墙的打破策略。通过实例代码展示了如何运用Number Transformation、字典操作、无限替换、数组排序算法以及优化墙面布局来解决问题。

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

I AK div3

思路有空就写哈哈哈哈

A - Number Transformation

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t -- ) {
        int a, b;
        cin >> a >> b;
        if (b % a) cout << 0 << ' ' << 0 << endl;
        else cout << 1 <<  ' ' << b / a << endl;
    }
    return 0;
}

B - Dictionary

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t -- ) {
        string str;
        cin >> str;
        int i = str[0] - 'a';
        int j = str[1] - 'a';
        if (str[0] <= str[1]) j --;
        cout << i * 25 + j + 1 << endl;
    }
    return 0;
}

C - Infinite Replacement

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t -- ) {
        string s, t;
        cin >> s >> t;
        int cnt = 0;
        for (int i = 0; i < t.size(); i ++ )
            if (t[i] == 'a') cnt ++;
        if (cnt == t.size() && t.size() == 1) {
            cout << 1 << endl;
        } else if (cnt) {
            cout << -1 << endl;
        } else {
            LL ans = 1ll << s.size();
            cout << ans << endl;
        }
    }
    return 0;
}

D - A-B-C Sort

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N], b[N];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t -- ) {
        int n;
        cin >> n;
        bool ok = 1;
        for (int i = 1; i <= n; i ++ ) {
            cin >> a[i];
            b[i] = a[i];
        }
        sort(b + 1, b + n + 1);
        for (int i = n; i; i -- ) {
            if (b[i] == a[i]) continue;
            if (i > 1 && b[i] == a[i - 1] && b[i - 1] == a[i] && (n - i) % 2 == 0) {
                i --;
                continue;
            }
            else ok = 0;
        }
        if (ok) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}

E - Breaking the Wall

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N], b[N];
int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i ++ ) {
        cin >> a[i];
        b[i] = a[i];
    }
    sort(b + 1, b + n + 1);
    int ans = (b[1] + 1) / 2 + (b[2] + 1) / 2;
    for (int i = 1; i < n; i ++ ) {
        ans = min(ans, max((a[i] + a[i + 1] + 2) / 3, max((a[i] + 1) / 2, (a[i + 1] + 1) / 2)));
    }
    for (int i = 1; i < n - 1; i ++ ) {
        ans = min(ans, (a[i] + a[i + 2] + 1) / 2);
    }
    cout << ans << endl;
    return 0;
}

F - Desktop Rearrangement

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
char g[N][N];
int col[N][N];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m, q;
    cin >> n >> m >> q;
    int sum = 0;
    for (int i = 0; i < n; i ++ ) cin >> g[i];
    for (int i = 0; i < n; i ++ ) {
        for (int j = 0; j < m; j ++ ) {
            if (j) col[i][j] = col[i][j - 1];
            if (g[i][j] == '*') {
                col[i][j] ++;
                sum ++;
            }
        }
    }
    while (q -- ) {
        int x, y;
        cin >> x >> y;
        x --, y --;
        if (g[x][y] == '.') {
            g[x][y] = '*';
            col[x][y] ++;
            for (int i = y + 1; i < m; i ++ ) {
                col[x][i] = col[x][i - 1];
                if (g[x][i] == '*') col[x][i] ++;
            }
            sum ++;
        } else {
            g[x][y] = '.';
            col[x][y] --;
            for (int i = y + 1; i < m; i ++ ) {
                col[x][i] = col[x][i - 1];
                if (g[x][i] == '*') col[x][i] ++;
            }
            sum --;
        }
        int cnt = sum / n;
        int r = sum % n;
        int num = 0;
        for (int i = 0; i < n; i ++ ) {
            if (i < r) {
                num += col[i][cnt - 1 + 1];
            } else if (cnt && i >= r) num += col[i][cnt - 1];
        }
        cout << sum - num << endl;
    }
    return 0;
}

G - Remove Directed Edges

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int M = 4e5 + 10;
int h[N], e[M], ne[M], idx;
int in[N], out[N];
int dp[N];
void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
void dfs(int u) {
    if (dp[u]) return;
    dp[u] = 1;
    if (out[u] <= 1) return;
    for (int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        dfs(j);
        if (in[j] >= 2) dp[u] = max(dp[u], dp[j] + 1);
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    memset(h, -1, sizeof h);
    int n, m;
    cin >> n >> m;
    while (m -- ) {
        int a, b;
        cin >> a >> b;
        in[b] ++, out[a] ++;
        add(a, b);
    }
    for (int i = 1; i <= n; i ++ )
        if (!dp[i]) dfs(i);
    int ans = 0;
    for (int i = 1; i <= n; i ++ )
        ans = max(ans, dp[i]);
    cout << ans << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值