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;
}
}
}
}