Codeforces Round #792 (Div. 1 + Div. 2) A ~ E

本文包含了五个算法问题的解决方案,分别是A-DigitMinimization,找到整数的最小数字;B-ZmodX=C,计算模运算后的结果;C-ColumnSwapping,找出需要交换的列来使所有行有序;D-Traps,求解陷阱总数;E-MEXvsDIFF,计算MEX和DIFF的最小值。涉及C++编程,算法设计和数学逻辑。

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

有空就写思路

A - Digit Minimization

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t -- ) {
        int n;
        cin >> n;
        int mn = 10;
        if (n < 100) {
            cout << n % 10 << endl;
        } else {
            while (n) {
                int tmp = n % 10;
                mn = min(mn, tmp);
                n /= 10;
            }
            cout << mn << endl;
        }
    }
}

B - Z mod X = C

#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, c;
        cin >> a >> b >> c;
        cout << a + b + c << ' ' << b + c << ' ' << c << endl;
    }
}

C - Column Swapping

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t -- ) {
        int n, m;
        cin >> n >> m;
        vector<vector<int>> a(n, vector<int>(m));
        for (int i = 0; i < n; i ++ )
            for (int j = 0; j < m; j ++ ) cin >> a[i][j];
        int pos = 0;
        bool ok = 1;
        vector<int> b;
        while (pos < n && ok) {
            b = a[pos];
            sort(b.begin(), b.end());
            bool f = 1;
            for (int i = 0; i < m; i ++ )
                if (b[i] != a[pos][i]) {
                    f = 0;
                    break;
                }
            if (f) pos ++;
            else {
                ok = 0;
                break;
            }
        }
        if (pos == n) {
            cout << "1 1\n";
            continue;
        }
        vector<int> res;
        for (int i = 0; i < m; i ++ )
            if (b[i] != a[pos][i]) res.push_back(i);
        if (res.size() == 2) {
            int x = res[0], y = res[1];
            bool ok = 1;
            for (pos = 0; pos < n; pos ++ ) {
                b = a[pos];
                sort(b.begin(), b.end());
                swap(a[pos][x], a[pos][y]);
                for (int i = 0; i < m; i ++ )
                    if (a[pos][i] != b[i]) {
                        ok = 0;
                        break;
                    }
                if (!ok) break;
            }
            if (ok) cout << x + 1 << ' ' << y + 1 << endl;
            else cout << -1 << endl;
        } else {
            cout << -1 << endl;
        }
    }
}

D - Traps

#include <bits/stdc++.h>
using namespace std;
#define PB push_back
typedef long long LL;
typedef vector<int> VI;
int main() {
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t -- ) {
        int n, k;
        cin >> n >> k;
        VI a(n), vis(n);
        vector<pair<int, int>> b(n);
        for (int i = 0; i < n; i ++ ) {
            cin >> a[i];
            b[i] = {a[i] + i, i};
        }
        sort(b.begin(), b.end(), greater<pair<int, int>>());
        for (int i = 0; i < k; i ++ ) 
            vis[b[i].second] = 1;
        LL ans = 0;
        int cnt = 0;
        for (int i = 0; i < n; i ++ )
            if (!vis[i]) ans += cnt + a[i];
            else cnt ++;
        cout << ans << endl;
    }
}

E - MEX vs DIFF

#include <bits/stdc++.h>
using namespace std;
#define PB push_back
typedef pair<int, int> PII;
typedef vector<int> VI;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t -- ) {
        int n, k;
        cin >> n >> k;
        VI a(n);
        map<int, int> mp;
        for (int i = 0; i < n; i ++ ) {
            cin >> a[i];
            mp[a[i]] ++;
        }
        sort(a.begin(), a.end());
        a.erase(unique(a.begin(), a.end()), a.end());
        int mex = 0, cnt = 0, pos = 0, res = n;
        for (int i = 0; i <= n && res >= cnt; i ++ ) {
            if (cnt > k) break;
            if (a[pos] == i) pos ++;
            else cnt ++;
            mex = i;
            res -= mp[i];
        }
        if (cnt > k) cnt = k;
        vector<PII> st;
        if (pos < a.size()) {
            for (auto [x, num] : mp)
                if (x >= a[pos] && num)
                    st.PB({num , x});
        }
        sort(st.begin(), st.end());
        int ans = 0, sum = 0;
        for (auto [num, x] : st) {
            if (sum >= cnt) ans ++;
            else {
                if (sum + num > cnt) ans ++;
                sum += num;
            }
        }
        cout << ans << endl;
    }
    return 0; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值