# [Educational Codeforces Round 113 (Rated for Div. 2)]

## A Balanced Substring

Topic:

Give you a string of length N containing only a and b, and the length of the string is 1-n. Now you need to find a balanced string (the number of a in this string is equal to the number of b). If such a string exists, output the subscripts of the start and end of the string, but not -1-1;

Solving ideas:

With two-tier loops, the outer loop represents the subscript to find the starting point of the string, and the inner loop represents the subscript to the ending point. If found, the subscript record exits the loop and outputs, otherwise outputs -1-1.

```#include<iostream>
#include<string>
using namespace std;

int main() {
int n;
scanf("%d", &n);
while (n--) {
int m;
scanf("%d", &m);
string s;
cin >> s;
bool flag = false;
int res=-1, ans=-1;
for (int i = 0; i < m; i++) {
int a = 0, b = 0;
for (int j = i; j < m; j++) {
if (s[j] == 'a')a++;
else b++;
if (a == b) {
ans = j+1;
flag = true;
break;
}
}
if (flag) {
res=i+1;
break;
}
}
printf("%d %d\n", res, ans);
}
return 0;
}
```

## B Chess Tournament

Topic:

Enter a string of length m containing only 1 and 2. 1 means that I can't lose (I can tie my opponent) in any match with any other team member. 2 means that I will win at least one of these matches. The Title requires the output of a two-dimensional matrix of m*m (i means the columns in the matrix, J means the rows in the matrix), which has three characters(X means i==j, +means I won j, or j lost to i, -means I lost to j, J lost to i). If the string you entered does not match the actual situation, output NO.

Solving ideas:

First let's consider the case of strings that don't match reality

When the number of 2 is 1 and 2, it must be nonexistent. It's easy to think about it. (Think about why it's OK when the number of 2 is greater than 2.)

Other circumstances must be appropriate

Count the number of 2 in the string and save the subscript of 2 with a b array. In order to form a ring, we will continue to lose the first subscript of 2 after the array. According to the meaning of 2, if the subscript of 2 is 123, we can assume that player 1 wins player 2, player 2 wins player 3... player 4 wins player 1.

Using a bool array, we fill the two-dimensional array of m*m in turn. Traversing through the b array will determine the + and - - inside, and make them only true. Finally, all the unfilled positions inside will be tied, as=.

```#include<iostream>
#include<time.h>
#include<string>
#include<cstring>
using namespace std;

const int mod = 1e6;

int main() {
int n;
cin >> n;
while (n--) {
char d;
bool e = { false };
int m;
cin >> m;
string s;
cin >> s;
int res = 0;
int b,count=0;
memset(b, 0, sizeof(b));
for (int i = 0; i < m; i++) {
if (s[i] == '2') {
res++;
b[count++] = i;
}
}
if (res ==1||res==2)printf("NO\n");
else {
b[count] = b;
printf("YES\n");
for (int i = 0; i < m; i++) {
d[i][i] = 'X';
e[i][i] = true;
if (s[i] == '1') {
for (int j = 0; j < m; j++) {
if (i != j) {
d[i][j] = d[j][i] = '=';
e[i][j] = e[j][i] = true;
}
}
}
}
for (int i = 0; i < count; i++) {
d[b[i]][b[i + 1]] = '+';
d[b[i + 1]][b[i]] = '-';
e[b[i]][b[i + 1]] = true;
e[b[i + 1]][b[i]] = true;
}
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
if (e[i][j] == false)d[i][j] = '=';
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++)
cout << d[i][j];
cout << endl;
}
}
}
return 0;
}
```

## C Jury Meeting

Topic:

There are n people. Each person has several questions in his or her hand, and then asks them one by one. If there are any more questions in his or her hand, ask the question to others. If there are no questions, skip the question directly, but one person cannot ask two or more questions in a row. Ask how many permutations there are to satisfy the question.

Solving ideas:

First, we sort the numbers we enter. We just look at the largest number and the second largest number. (Think about why?)

Let's make the largest number and the second number a[m-1],a[m-2]

When a[m-1]-a[m-2]>1, the last maximum number must be continuously asked several questions before it becomes 0, directly outputting 0;

When a[m-1]=a[m-2], the last two largest numbers must ask each other questions in turn, so there is no case where a person will kick two questions in a row. The answer is the factorial of m, because the number of questions for each person can be placed anywhere in the ranking.

The most complex is when a[m-1]-a[m-2]=1

The first largest number must not be placed at the end. If I put it at the end, it will lead to the two largest numbers asking questions twice in a row (I'm not sure you can try it). We will count the number of the second largest number out to n1, and then insert the largest number into the empty space (satisfying not to insert at the end), so there are N1 empty spaces in total

res=(ll)fac[n1]*n1%mod;

Finally, we will insert the other numbers in turn, first there is k=n1+1 number, there is k+1 empty, with a while cycle, each cycle in turn empty number plus one that is k++;

```#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

const int mod = 998244353,N=2e5+10;
typedef long long ll;
vector<int> fac(N, 1);
int a[N];

int main() {
for (int i = 2; i < 200010; i++)
fac[i] = (ll)fac[i - 1] * i % mod;
int n;
scanf("%d", &n);
while (n--) {
int m;
scanf("%d", &m);
for (int i = 0; i < m; i++)scanf("%d", &a[i]);
sort(a, a+m);

if (a[m - 1] == a[m - 2])printf("%d\n", fac[m]);
else if (a[m - 1] - a[m - 2] > 1)printf("0\n");
else {
int n1 = 0, n2 = 0;
for (int i = 0; i < m; i++) {
if (a[i] == a[m - 2])n1++;
else n2++;
}
n2--;
int res = (ll)fac[n1] * n1 % mod;
int k = n1 + 1;
while (n2--) {
res = (ll)res * (k + 1) % mod;
k++;
}
printf("%d\n", res % mod);
}
}
return 0;
}
```

Posted on Thu, 09 Sep 2021 15:09:06 -0400 by purpleshadez