Educational Codeforces Round 105 (Rated for Div. 2)
比赛链接:https://codeforces.com/contest/1494
A | 水题(AC)、三种字母分别代表'('、')',记录出现最多次数的字母 = 另外两种和,记录前缀和 or 模拟栈操作、【代码写的较low、冗余度较高】 |
B | 水题(AC)、模拟操作、分别讨论不同的情况,分情况讨论是否可行 |
C | (WA待补)、分别向左向右推动箱子判断有多少箱子落在特殊位置上、三层循环操作(使用后缀和 + 循环操作 + 滑块操作 or 尺取法) |
D | 尚未看题 |
E | 尚未看题 |
F | 尚未看题 |
AC两道,罚时1次 。
A. ABC String
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a string 𝑎a, consisting of 𝑛n characters, 𝑛n is even. For each 𝑖i from 11 to 𝑛n 𝑎𝑖ai is one of 'A', 'B' or 'C'.
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.
You want to find a string 𝑏b that consists of 𝑛n characters such that:
- 𝑏b is a regular bracket sequence;
- if for some 𝑖i and 𝑗j (1≤𝑖,𝑗≤𝑛1≤i,j≤n) 𝑎𝑖=𝑎𝑗ai=aj, then 𝑏𝑖=𝑏𝑗bi=bj.
In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.
Your task is to determine if such a string 𝑏b exists.
Input
The first line contains a single integer 𝑡t (1≤𝑡≤10001≤t≤1000) — the number of testcases.
Then the descriptions of 𝑡t testcases follow.
The only line of each testcase contains a string 𝑎a. 𝑎a consists only of uppercase letters 'A', 'B' and 'C'. Let 𝑛n be the length of 𝑎a. It is guaranteed that 𝑛n is even and 2≤𝑛≤502≤n≤50.
Output
For each testcase print "YES" if there exists such a string 𝑏b that:
- 𝑏b is a regular bracket sequence;
- if for some 𝑖i and 𝑗j (1≤𝑖,𝑗≤𝑛1≤i,j≤n) 𝑎𝑖=𝑎𝑗ai=aj, then 𝑏𝑖=𝑏𝑗bi=bj.
Otherwise, print "NO".
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
Example
input
Copy
4
AABBAC
CACA
BBBBAC
ABCA
output
Copy
YES
YES
NO
NO
Note
In the first testcase one of the possible strings 𝑏b is "(())()".
In the second testcase one of the possible strings 𝑏b is "()()".
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100;
char str[maxn];
int a, b, c;
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int a = 0, b = 0, c = 0;
scanf("%s", str);
int len = strlen(str);
for(int i=0; i<len; i++)
{
if(str[i] == 'A')
a++;
else if(str[i] == 'B')
b++;
else if(str[i] == 'C')
c++;
}
if(a * 2 == len)
{
int sum = 0, k = 0;
if(str[0] == 'A')
{
for(int i=0; i<len; i++)
{
if(str[i] == 'A')
sum++;
else
sum--;
if(sum < 0)
{
k = -1;
break;
}
}
}
else if(str[0] != 'A')
{
for(int i=0; i<len; i++)
{
if(str[i] == 'A')
sum--;
else
sum++;
if(sum < 0)
{
k = -1;
break;
}
}
}
if(k == -1)
{
printf("NO\n");
}
else
{
printf("YES\n");
}
}
else
{
if(b * 2 == len)
{
int sum = 0, k = 0;
if(str[0] == 'B')
{
for(int i=0; i<len; i++)
{
if(str[i] == 'B')
sum++;
else
sum--;
if(sum < 0)
{
k = -1;
break;
}
}
}
else if(str[0] != 'B')
{
for(int i=0; i<len; i++)
{
if(str[i] == 'B')
sum--;
else
sum++;
if(sum < 0)
{
k = -1;
break;
}
}
}
if(k == -1)
{
printf("NO\n");
}
else
{
printf("YES\n");
}
}
else
{
if(c * 2 == len)
{
int sum = 0, k = 0;
if(str[0] == 'C')
{
for(int i=0; i<len; i++)
{
if(str[i] == 'C')
sum++;
else
sum--;
if(sum < 0)
{
k = -1;
break;
}
}
}
else if(str[0] != 'C')
{
for(int i=0; i<len; i++)
{
if(str[i] == 'C')
sum--;
else
sum++;
if(sum < 0)
{
k = -1;
break;
}
}
}
if(k == -1)
{
printf("NO\n");
}
else
{
printf("YES\n");
}
}
else
{
printf("NO\n");
}
}
}
}
return 0;
}
B. Berland Crossword
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Berland crossword is a puzzle that is solved on a square grid with 𝑛n rows and 𝑛n columns. Initially all the cells are white.
To solve the puzzle one has to color some cells on the border of the grid black in such a way that:
- exactly 𝑈U cells in the top row are black;
- exactly 𝑅R cells in the rightmost column are black;
- exactly 𝐷D cells in the bottom row are black;
- exactly 𝐿L cells in the leftmost column are black.
Note that you can color zero cells black and leave every cell white.
Your task is to check if there exists a solution to the given puzzle.
Input
The first line contains a single integer 𝑡t (1≤𝑡≤10001≤t≤1000) — the number of testcases.
Then the descriptions of 𝑡t testcases follow.
The only line of each testcase contains 55 integers 𝑛,𝑈,𝑅,𝐷,𝐿n,U,R,D,L (2≤𝑛≤1002≤n≤100; 0≤𝑈,𝑅,𝐷,𝐿≤𝑛0≤U,R,D,L≤n).
Output
For each testcase print "YES" if the solution exists and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
Example
input
Copy
4
5 2 5 3 1
3 0 0 0 0
4 4 1 4 0
2 1 1 1 1
output
Copy
YES
YES
NO
YES
Note
Here are possible solutions to testcases 11, 22 and 44:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
int n, U, R, D, L;
while(t--)
{
scanf("%d%d%d%d%d", &n, &U, &R, &D, &L);
int k = 1;
int x, y;
x = 0, y = 0;
if(n == U)
x = 2;
else if(n == U +1)
x = 1;
if(n == D)
y = 2;
else if(n == D + 1)
y = 1;
if(x + y == 4)
{
if(R < 2 || L < 2)
k = 0;
}
else if(x + y == 3)
{
if(max(R, L) < 2 || min(R, L) < 1)
k = 0;
}
else if(x + y == 2)
{
if(x == 1 && y == 1)
{
if(R + L < 2)
k = 0;
}
else
{
if(min(R, L) < 1)
k = 0;
}
}
else if(x + y == 1)
{
if(R + L == 0)
k = 0;
}
x = 0, y = 0;
int key = U;
U = R;
R = D;
D = L;
L = key;
if(n == U)
x = 2;
else if(n == U +1)
x = 1;
if(n == D)
y = 2;
else if(n == D + 1)
y = 1;
if(x + y == 4)
{
if(R < 2 || L < 2)
k = 0;
}
else if(x + y == 3)
{
if(max(R, L) < 2 || min(R, L) < 1)
k = 0;
}
else if(x + y == 2)
{
if(x == 1 && y == 1)
{
if(R + L < 2)
k = 0;
}
else
{
if(min(R, L) < 1)
k = 0;
}
}
else if(x + y == 1)
{
if(R + L == 0)
k = 0;
}
if(k == 0)
{
printf("NO\n");
}
else
{
printf("YES\n");
}
}
return 0;
}