ICode9

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

Educational Codeforces Round 113 (Rated for Div. 2) 补题 A B C

2021-09-10 14:32:00  阅读:164  来源: 互联网

标签:Educational Rated int res cnt ++ 补题 mx mod


A. Balanced Substring

在这里插入图片描述

分析: 暴力枚举每个子串,签到题

代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 55;
int T, n;
char str[N];
signed main() {
    cin >> T;
    while (T --) {
        int x = -1, y = -1;
        cin >> n >> str + 1;
        for (int len = 1; len <= n; len ++) {
            for (int l = 1; l + len - 1 <= n ; l ++) {
                int r = l + len - 1;
                map<char, int> mp;
                for (int k = l; k <= r; k ++) mp[str[k]] ++;
                if (mp['a'] == mp['b']) x = l, y = r;
            }
        }
        cout << x << " " << y << endl;
    }
}

B. Chess Tournament

在这里插入图片描述

分析: 先判断类型为 2 2 2 的人,都把对手改为 = = =,再判断类型为 1 1 1 的人,如果存在一个非 = = = 的位置,那么就让他赢一局,之后 break,如果没找到,那么就输出 NO \text{NO} NO

代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 55;
int T, n;
string str;
char res[N][N];
signed main() {
    cin >> T;
    while (T --) {
        for (int i = 0; i < N; i ++) {
            for (int j = 0; j < N; j ++) {
                if (i == j) {
                    res[i][i] = 'X';
                } else {
                    res[i][j] = '0';
                }
            }
        }
        int flag = 0, er = 0;
        cin >> n >> str;
        for (int i = 0; i < n; i ++) {
            if (str[i] == '1') {
                for (int j = 0; j < n; j ++) {
                    if (j != i) {
                        res[i][j] = res[j][i] = '=';
                    }
                }
            }
        }
        for (int i = 0; i < n; i ++) {
            if (str[i] == '2') {
                er = 1;
                int pd = 0;
                for (int j = 0; j < n; j ++) {
                    if (res[i][j] == '0' && res[j][i] == '0') {
                        res[i][j] = '+', res[j][i] = '-';
                        pd = 1;
                        break;
                    }
                }
                if (!pd) flag = 1;
            }
        }
        if (!er || !flag) {
            cout << "YES" << endl;
            for (int i = 0; i < n; i ++) {
                for (int j = 0; j < n; j ++) {
                    if (res[i][j] == '0') {
                        res[i][j] = '=';
                    }
                    cout << res[i][j];
                }
                cout << endl;
            }
        } else {
            cout << "NO" << endl;
        }
    }
}

C. Jury Meeting

在这里插入图片描述

分析: 发现合法的方案一定跟 max ⁡ ( a 1 , ⋯   , a n ) \max(a_1,\cdots,a_n) max(a1​,⋯,an​) 有关,所以设 m x = max ⁡ ( a 1 , ⋯   , a n ) mx=\max(a_1,\cdots,a_n) mx=max(a1​,⋯,an​)

若 m x ≥ 2 mx \ge 2 mx≥2,那么最后一轮一定只剩下一堆 1 1 1,这堆数量就是 m x mx mx 的个数,这样一定可以在一轮完成,总是合法的方案,所以此情况的方案数为 n ! n! n!

若 m x = 1 mx =1 mx=1 ,那么 m x − 1 mx -1 mx−1 一定不能放在 m x mx mx 的前面,所以考虑用总方案数减去不合法的方案数,设当前的 m x − 1 mx - 1 mx−1 的数量为 c n t cnt cnt,那么其他的元素就为 n − c n t − 1 n - cnt-1 n−cnt−1 ,那么可以枚举每个可移动的元素 i i i,根据排列组合,不合法的方案数就为 ( c n t + i ) ! ⋅ C n − c n t − 1 i ⋅ ( n − c n t − 1 − i ) ! (cnt+i)! \cdot C_{n-cnt - 1} ^{i} \cdot (n - cnt - 1 - i)! (cnt+i)!⋅Cn−cnt−1i​⋅(n−cnt−1−i)!

代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5, mod = 998244353;
int T, n, a[N], fact[N], infact[N];
int qmi(int a, int b) {
    int res = 1;
    while (b) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int C(int n, int m) {
    return fact[n] * infact[m] % mod * infact[n - m] % mod;
}
signed main() {
    fact[0] = infact[0] = 1;
    for (int i = 1; i < N; i ++) {
        fact[i] = fact[i - 1] * i % mod;
        infact[i] = infact[i - 1] * qmi(i, mod - 2) % mod;
    }
    cin >> T;
    while (T --) {
        int res = 0, mx = 0;
        cin >> n;
        for (int i = 1; i <= n; i ++) cin >> a[i], mx = max(mx, a[i]);
        if (count(a + 1, a + 1 + n, mx) >= 2) {
            cout << fact[n] << endl;
        } else {
            int cnt = count(a + 1, a + 1 + n, mx - 1);
            if (!cnt) {
                cout << 0 << endl;
            } else {
                for (int i = 0; i <= n - cnt - 1; i ++) {
                    res = ((res + fact[cnt + i] * C(n - cnt - 1, i) % mod * fact[n - cnt - 1 - i] % mod) % mod + mod) % mod;
                }
                cout << ((fact[n] - res) % mod + mod) % mod << endl;
            }
        }
    }
}

标签:Educational,Rated,int,res,cnt,++,补题,mx,mod
来源: https://blog.csdn.net/messywind/article/details/120220544

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

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

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

ICode9版权所有