zoukankan      html  css  js  c++  java
  • Codeforces Round #732 (Div. 2) A ~ D 个人题解记录

    比赛链接:Here


    1546A - AquaMoon and Two Arrays

    选定两个数组元素执行以下操作:

    • (a_i,a_j (1le i,j le n)) 一个 +1 另一个 -1,

      前提是两个数都要结果非负

    请问在执行若干次后使得数组 (a) 等于 数组 (b)


    先统计两个数组总和,只有和相同可以,否则输出 -1

    int main() {
        cin.tie(nullptr)->sync_with_stdio(false);
        int _; for (cin >> _; _--;) {
            int n; cin >> n;
            int suma = 0, sumb = 0;
            vector<int>a(n), b(n);
            for (int &x : a)cin >> x, suma += x;
            for (int &x : b)cin >> x, sumb += x;
            if (suma != sumb) {
                cout << "-1
    ";
                continue;
            }
            int cnt = 0;
            vector<pair<int, int>>ans;
            for (int i = 0; i < n; ++i)
                if (a[i] > b[i])ans.push_back({i, a[i] - b[i]}), cnt += (a[i] - b[i]);
    
            cout << cnt << "
    ";
            if (cnt == 0)continue;
    
            int j = 0;
            for (int i = 0; i < n; ++i) {
                while (a[i] < b[i]) {
                    if (ans[j].second > 0) {
                        a[i]++;
                        cout << ans[j].first + 1 << " " << i + 1 << "
    ";
                        ans[j].second--;
                    } else j++;
                }
            }
        }
    }
    

    1546B - AquaMoon and Stolen String

    题意都能看懂就不写了...


    在每一列中,只会有一个元素出现奇数次,

    只需要 map 存第 (i) 位的值即可

    int main() {
        cin.tie(nullptr)->sync_with_stdio(false);
        int _; for (cin >> _; _--;) {
            int n, m;
            cin >> n >> m;
            vector<ll>v(m, 0);
            string s;
            for (int i = 0; i < n; ++i) {
                cin >> s;
                for (int j = 0 ; j < m; ++j)
                    v[j] += (s[j] - 'a');
            }
    
            for (int i = 0; i < n - 1; ++i) {
                cin >> s;
                for (int j = 0; j < m; ++j)
                    v[j] -= (s[j] - 'a');
            }
            for (int i = 0; i < m; ++i) cout << char(v[i] + 'a');
            cout << '
    ';
        }
    }
    

    1546C - AquaMoon and Strange Sort

    对于每一个元素,肯定只能移动偶数距离

    所以对于同一元素需要统计它们有多少个在奇数和偶数位置

    原数组 (a) ,排序后数组 (b)

    对于每一个元素,如果它在 (a) 中的奇数位置次数不同于在 (b) 中奇数位置出现次数(偶数同理)则输出 NO,否则输出 YES

    必须吐槽自己,大晚上写的代码不仔细,wa在 #38数据

    const int N = 1e6 + 10;
    int a[N], b[N], t[N][2];
    int main() {
        cin.tie(nullptr)->sync_with_stdio(false);
        int _; for (cin >> _; _--;) {
            int n; cin >> n;
            memset(t, 0, sizeof(t));
    
            for (int i = 1; i <= n; ++i)cin >> a[i], b[i] = a[i];
            sort(b + 1, b + 1 + n);
            for (int i = 1; i <= n; ++i) {
                t[a[i]][i & 1]++;
                t[b[i]][i & 1]--;
            }
    
            bool f = true;
            for (int i = 1; f and  i <= 100000; ++i)
                if (t[i][0] || t[i][1]) f = false;
            cout << (f ? "YES
    " : "NO
    ");
        }
    }
    

    1546D - AquaMoon and Chess

    看完题感觉是像某种组合数学题,但没思路

    先贴一下dalao代码

    #define ll long long
    #define int int64_t
    
    constexpr int N = 1e5 + 5;
    constexpr int INF = 1e9 + 5;
    constexpr int mod = 998244353;
    
    int n, fac[N], invfac[N];
    string s;
    
    int carp(int x, int y) { return x * y % mod;}
    
    int binpow(int x, int y) {
        int res = 1;
        for (; y; y >>= 1, x = carp(x, x))
            if (y & 1) res = carp(res, x);
        return res;
    }
    
    int inv(int x) { return binpow(x, mod - 2);}
    
    void factorial() {
        fac[0] = 1;
        for (int i = 1; i < N; i++) fac[i] = carp(fac[i - 1], i);
        invfac[N - 1] = inv(fac[N - 1]);
        for (int i = N - 2; i >= 0; i--)
            invfac[i] = carp(invfac[i + 1], i + 1);
    }
    
    int nCr(int m, int r) { return carp(fac[m], carp(invfac[r], invfac[m - r]));}
    
    void solve() {
        cin >> n >> s;
        int z = 0, o = 0;
        for (int i = 0; i < n; i++) {
            int j = i;
            if (s[j] == '1') {
                while (j < n && s[j] == '1')
                    j++;
                o += (j - i) / 2;
                i = j - 1;
            } else  z++;
        }
        cout << nCr(z + o, o) << nl;
    }
    

    The desire of his soul is the prophecy of his fate
    你灵魂的欲望,是你命运的先知。

  • 相关阅读:
    [Abp vNext 源码分析]
    C# 结合 PInvoke 对接 IP 摄像头的笔记
    Abp vNext 自定义 Ef Core 仓储引发异常
    [Abp vNext 源码分析]
    [Abp vNext 源码分析]
    [Abp vNext 源码分析]
    网站SEO中服务器优化的三个问题
    用香港服务器还是国内服务器好
    租用香港服务器最重要的三个问题
    如何选择服务器操作系统
  • 原文地址:https://www.cnblogs.com/RioTian/p/15002373.html
Copyright © 2011-2022 走看看