AtCoder Beginner Contest 298——A-D题讲解
蒟蒻来讲题,还望大家喜。若哪有问题,大家尽可提!
Hello, 大家好哇!本初中生蒟蒻讲解一下AtCoder Beginner Contest 298这场比赛的A-D题!
今晚比赛动不动就是网页无法显示了,官方说是被DDOS攻击了,导致今晚不算分,好在不算分,在D题目翻车了,算法是对的,但折腾了好长时间。
===========================================================================================
A - Job Interview
原题
Problem Statement
Takahashi had a job interview.
You are given the number of interviewers, NNN, and a string SSS of length NNN representing the interviewers’ evaluations of him.
For each i=1,2,…,Ni=1,2,\\ldots,Ni=1,2,…,N, the iii-th character of SSS corresponds to the iii-th interviewer’s evaluation; o
means Good, -
means Fair, and x
means Poor.
Takahashi will pass if both of the following conditions are satisfied, and fail otherwise.
At least one interviewer’s evaluation is Good.
No interviewer’s evaluation is Poor.
Determine whether Takahashi passes.
Constraints
1≤N≤1001 \\leq N \\leq 1001≤N≤100
SSS is a string of length NNN consisting of o
, -
, and x
.
Input
The input is given from Standard Input in the following format:
NNN
SSS
Output
If Takahashi passes, print Yes
; otherwise, print No
.
Sample Input 1
4
oo–
Sample Output 1
Yes
The first and second interviewers’ evaluations are Good, and no interviewer’s evaluation is Poor, so he passes.
Sample Input 2
3
Sample Output 2
No
No interviewer’s evaluation is Good, so he fails.
Sample Input 3
1
o
Sample Output 3
Yes
Sample Input 4
100
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooox
Sample Output 4
No
The 100100100-th interviewer’s evaluation is Poor, so he fails.
思路
就是如果有x直接输出No,如果没有x,且有一个及以上o,那么输出Yes,否则输出No。
时间复杂度:O(n)O(n)O(n)
代码
#include <iostream>
#include <cstring>
#include <algorithm>
#define endl '\\n'
#define psb(i) push_back(i)
#define ppb() pop_back()
#define psf(i) push_front(i)
#define ppf() pop_front()
#define ps(i) push(i)using namespace std;typedef pair<int, int> PII;inline int read()
{int w = 1, s = 0;char c = getchar();while (c < '0' || c > '9'){if (c == '-') w = -1;c = getchar();}while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();return w * s;
}int main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int n;string s;cin >> n >> s;bool hy = 0, hn = 0;for (auto c : s)if (c == 'o')hy = 1;else if (c == 'x')hn = 1;if (!hn && hy)cout << "Yes" << endl;elsecout << "No" << endl;return 0;
}
B - Coloring Matrix
原题
Problem Statement
You are given NNN-by-NNN matrices AAA and BBB where each element is 000 or 111.
Let Ai,jA_{i,j}Ai,j and Bi,jB_{i,j}Bi,j denote the element at the iii-th row and jjj-th column of AAA and BBB, respectively.
Determine whether it is possible to rotate AAA so that Bi,j=1B_{i,j} = 1Bi,j=1 for every pair of integers (i,j)(i, j)(i,j) such that Ai,j=1A_{i,j} = 1Ai,j=1.
Here, to rotate AAA is to perform the following operation zero or more times:
for every pair of integers (i,j)(i, j)(i,j) such that 1≤i,j≤N1 \\leq i, j \\leq N1≤i,j≤N, simultaneously replace Ai,jA_{i,j}Ai,j with AN+1−j,iA_{N + 1 - j,i}AN+1−j,i.
Constraints
1≤N≤1001 \\leq N \\leq 1001≤N≤100
Each element of AAA and BBB is 000 or 111.
All values in the input are integers.
Input
The input is given from Standard Input in the following format:
NNN
A1,1A_{1,1}A1,1 A1,2A_{1,2}A1,2 …\\ldots… A1,NA_{1,N}A1,N
⋮\\vdots⋮
AN,1A_{N,1}AN,1 AN,2A_{N,2}AN,2 …\\ldots… AN,NA_{N,N}AN,N
B1,1B_{1,1}B1,1 B1,2B_{1,2}B1,2 …\\ldots… B1,NB_{1,N}B1,N
⋮\\vdots⋮
BN,1B_{N,1}BN,1 BN,2B_{N,2}BN,2 …\\ldots… BN,NB_{N,N}BN,N
Output
If it is possible to rotate AAA so that Bi,j=1B_{i,j} = 1Bi,j=1 for every pair of integers (i,j)(i, j)(i,j) such that Ai,j=1A_{i,j} = 1Ai,j=1, print Yes
; otherwise, print No
.
Sample Input 1
3
0 1 1
1 0 0
0 1 0
1 1 0
0 0 1
1 1 1
Sample Output 1
Yes
Initially, AAA is :
0 1 1
1 0 0
0 1 0
After performing the operation once, AAA is :
0 1 0
1 0 1
0 0 1
After performing the operation once again, AAA is :
0 1 0
0 0 1
1 1 0
Here, Bi,j=1B_{i,j} = 1Bi,j=1 for every pair of integers (i,j)(i, j)(i,j) such that Ai,j=1A_{i,j} = 1Ai,j=1, so you should print Yes
.
Sample Input 2
2
0 0
0 0
1 1
1 1
Sample Output 2
Yes
Sample Input 3
5
0 0 1 1 0
1 0 0 1 0
0 0 1 0 1
0 1 0 1 0
0 1 0 0 1
1 1 0 0 1
0 1 1 1 0
0 0 1 1 1
1 0 1 0 1
1 1 0 1 0
Sample Output 3
No
思路
这道题我们其实就是将数组进行90°90°90°翻转,我们可以新开一个临时数组C,然后是数组A的翻转数组,这里可以套题目给的公式Ci,j=AN+1−j,iC_{i, j} = A_{N + 1 - j,i}Ci,j=AN+1−j,i,之后进行判断是否可行。因为转四个就又会转回来,所以我们最多只用转4次。
时间复杂度:O(n2)O(n^2)O(n2)
代码
#include <iostream>
#include <cstring>
#include <algorithm>
#define endl '\\n'
#define psb(i) push_back(i)
#define ppb() pop_back()
#define psf(i) push_front(i)
#define ppf() pop_front()
#define ps(i) push(i)using namespace std;typedef pair<int, int> PII;const int N = 1e2 + 10;int n;
int a[N][N];
int b[N][N];
int c[N][N];inline int read()
{int w = 1, s = 0;char c = getchar();while (c < '0' || c > '9'){if (c == '-') w = -1;c = getchar();}while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();return w * s;
}int main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> n;for (int i = 1; i <= n; i ++)for (int j = 1; j <= n; j ++)cin >> a[i][j];for (int i = 1; i <= n; i ++)for (int j = 1; j <= n; j ++)cin >> b[i][j];for (int op = 1; op <= 4; op ++){memcpy(c, a, sizeof a);for (int i = 1; i <= n; i ++)for (int j = 1; j <= n; j ++)c[i][j] = a[n + 1 - j][i];bool yes = 1;for (int i = 1; i <= n; i ++)for (int j = 1; j <= n; j ++)if (c[i][j] == 1 && b[i][j] != 1){yes = 0;break;}if (yes){cout << "Yes" << endl;return 0;}memcpy(a, c, sizeof c);}cout << "No" << endl;return 0;
}
C - Cards Query Problem
原题
Problem Statement
We have NNN boxes numbered 111 to NNN that are initially empty, and an unlimited number of blank cards.
Process QQQ queries in order. There are three kinds of queries as follows.
1 i j
:\\colon: Write the number iii on a blank card and put it into box jjj.
2 i
:\\colon: Report all numbers written on the cards in box iii, in ascending order.
3 i
:\\colon: Report all box numbers of the boxes that contain a card with the number iii, in ascending order.
Here, note the following.
In a query of the second kind, if box iii contains multiple cards with the same number, that number should be printed the number of times equal to the number of those cards.
In a query of the third kind, even if a box contains multiple cards with the number iii, the box number of that box should be printed only once.
Constraints
1≤N,Q≤2×1051 \\leq N, Q \\leq 2 \\times 10^51≤N,Q≤2×105
For a query of the first kind:
1≤i≤2×1051 \\leq i \\leq 2 \\times 10^51≤i≤2×105
1≤j≤N1 \\leq j \\leq N1≤j≤N
For a query of the second kind:
1≤i≤N1 \\leq i \\leq N1≤i≤N
Box iii contains some cards when this query is given.
For a query of the third kind:
1≤i≤2×1051 \\leq i \\leq 2 \\times 10^51≤i≤2×105
Some boxes contain a card with the number iii when this query is given.
At most 2×1052 \\times 10^52×105 numbers are to be reported.
All values in the input are integers.
Input
The input is given from Standard Input in the following format:
NNN
QQQ
query1\\mathrm{query}_1query1
query2\\mathrm{query}_2query2
⋮\\vdots⋮
queryQ\\mathrm{query}_QqueryQ
Here, queryq\\mathrm{query}_qqueryq denotes the qqq-th query, which is in one of the following formats:
111 iii jjj
222 iii
333 iii
Output
Respond to the queries of the second and third kinds in order.
For each of those queries, print one line containing the elements to be reported in ascending order, with spaces in between.
Sample Input 1
5
8
1 1 1
1 2 4
1 1 4
2 4
1 1 4
2 4
3 1
3 2
Sample Output 1
1 2
1 1 2
1 4
4
Let us process the queries in order.
Write 111 on a card and put it into box 111.
Write 222 on a card and put it into box 444.
Write 111 on a card and put it into box 444.
Box 444 contains cards with the numbers 111 and 222.
Print 111 and 222 in this order.
Write 111 on a card and put it into box 444.
Box 444 contains cards with the numbers 111, 111, and 222.
Note that you should print 111 twice.
Boxes 111 and 444 contain a card with the number 111.
Note that you should print 444 only once, even though box 444 contains two cards with the number 111.
Boxes 444 contains a card with the number 222.
Sample Input 2
1
5
1 1 1
1 2 1
1 200000 1
2 1
3 200000
Sample Output 2
1 2 200000
1
思路
这道题我们可以用一个一维的vector(大小近乎相当于二维数组),来存储每个盒子里装的是什么,在进行操作二的时候,进行一次排序然后输出即可。之后我们可以用一个一维的set(大小近乎相当于二维数组)来存储这个数在哪几个盒子里(用set的原因:①题目中要求排序,set自带 ②题目中要求去重,set自带),然后每一次输出这个set就行
注意: 千万不能标记这个盒子是否出现这个数,这样时间复杂度会退化至O(N2)O(N^2)O(N2),无法通过此题!
时间复杂度:O(nlog(n))O(nlog(n))O(nlog(n))
代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <set>
#define endl '\\n'
#define psb(i) push_back(i)
#define ppb() pop_back()
#define psf(i) push_front(i)
#define ppf() pop_front()
#define ps(i) push(i)using namespace std;typedef pair<int, int> PII;const int N = 2e5 + 10;int n, q;
int op, u, v;
vector<int> s[N];
set<int> has[N];inline int read()
{int w = 1, s = 0;char c = getchar();while (c < '0' || c > '9'){if (c == '-') w = -1;c = getchar();}while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();return w * s;
}int main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> n >> q;while (q --){cin >> op;if (op == 1){cin >> u >> v;s[v].push_back(u);has[u].insert(v);}else if (op == 2){cin >> v;sort(s[v].begin(), s[v].end());for (auto c : s[v])cout << c << " ";cout << endl;}else{cin >> u;for (auto c : has[u])cout << c << " ";cout << endl;}}return 0;
}
D - Writing a Numeral
原题
Problem Statement
We have a string SSS. Initially, S=S=S= 1
.
Process QQQ queries in the following formats in order.
1 x
: Append a digit xxx at the end of SSS.
2
: Delete the digit at the beginning of SSS.
3
: Print the number represented by SSS in decimal, modulo 998244353998244353998244353.
Constraints
1≤Q≤6×1051 \\leq Q \\leq 6 \\times 10^51≤Q≤6×105
For each query in the first format, x∈{1,2,3,4,5,6,7,8,9}x \\in \\{1,2,3,4,5,6,7,8,9\\}x∈{1,2,3,4,5,6,7,8,9}.
A query in the second format is given only if SSS has a length of 222 or greater.
There is at least one query in the third format.
Input
The input is given from Standard Input in the following format:
QQQ
query1\\mathrm{query}_1query1
⋮\\vdots⋮
queryQ\\mathrm{query}_QqueryQ
Here, queryi\\mathrm{query}_iqueryi denotes the iii-th query, which is in one of the following formats:
111 xxx
222
333
Output
Print qqq lines, where qqq is the number of queries in the third format. The iii-th line (1≤i≤q)(1 \\leq i \\leq q)(1≤i≤q) should correspond to the iii-th query in the third format.
Sample Input 1
3
3
1 2
3
Sample Output 1
1
12
In the first query, SSS is 1
, so you should print 111 modulo 998244353998244353998244353, that is, 111.
In the second query, SSS becomes 12
.
In the third query, SSS is 12
, so you should print 121212 modulo 998244353998244353998244353, that is, 121212.
Sample Input 2
3
1 5
2
3
Sample Output 2
5
Sample Input 3
11
1 9
1 9
1 8
1 2
1 4
1 4
1 3
1 5
1 3
2
3
Sample Output 3
0
Be sure to print numbers modulo 998244353998244353998244353.
思路
设M=998244353,a=SmodMM = 998244353, a = S\\: mod \\: MM=998244353,a=SmodM
第一种操作:
设S后面加了xxx
则:a=(a×10+x)%Ma = (a\\times 10 + x) \\% Ma=(a×10+x)%M
证明:
∵a=S%M,S′=S×10+x,a′=S′%m∴a′=S′%m=(s×10+x)%m=(S×10)%M+x%m=S%M×10%M+x%M=a×10%M+x%M=(a×10+x)%M\\begin{equation*} \\begin{split} & \\because a = S \\% M, S' = S\\times 10 + x,a' = S' \\% m \\\\ & \\therefore a' =S'\\%m=(s\\times10+x)\\%m\\\\ & \\qquad = (S\\times 10) \\% M + x \\% m \\\\ & \\qquad = S \\% M \\times 10 \\% M + x \\% M\\\\ & \\qquad = a \\times 10 \\% M + x \\% M\\\\ & \\qquad = (a\\times10 + x) \\% M\\\\ \\end{split} \\end{equation*} ∵a=S%M,S′=S×10+x,a′=S′%m∴a′=S′%m=(s×10+x)%m=(S×10)%M+x%m=S%M×10%M+x%M=a×10%M+x%M=(a×10+x)%M
第二种操作
S=S0×10∣S∣−1+S′S = S_0\\times 10^{|S|-1} + S'S=S0×10∣S∣−1+S′
证明:
∵a=S%M,S′=S−S0×10∣S∣−1∴a′=S′%M=(S−S0×10∣S∣−1)%M=S%M−(S0×10∣S∣−1)%M=a−(S0×10∣S∣−1)%M\\begin{equation*} \\begin{split} & \\because a = S \\% M, S' = S - S_0\\times 10^{|S|-1}\\\\ & \\therefore a' = S' \\% M = (S - S_0\\times10^{|S| - 1})\\% M\\\\ & \\qquad = S \\% M - (S_0\\times10^{|S| - 1}) \\% M\\\\ & \\qquad = a - (S_0\\times10^{|S| - 1}) \\% M \\end{split} \\end{equation*} ∵a=S%M,S′=S−S0×10∣S∣−1∴a′=S′%M=(S−S0×10∣S∣−1)%M=S%M−(S0×10∣S∣−1)%M=a−(S0×10∣S∣−1)%M
注意:a−(S0×10∣S∣−1)a - (S_0\\times10^{|S| - 1})a−(S0×10∣S∣−1)有可能小于0,计算机算出的模数也会是个负的,事实上是个正的,所以我们只需在加M的基础上再模M就行,即:(a+M−(S0×10∣S∣−1))%M(a + M - (S_0\\times10^{|S| - 1})) \\% M(a+M−(S0×10∣S∣−1))%M
第三种操作
直接输出a
代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#define endl '\\n'
#define psb(i) push_back(i)
#define ppb() pop_back()
#define psf(i) push_front(i)
#define ppf() pop_front()
#define ps(i) push(i)using namespace std;typedef pair<int, int> PII;const int N = 8e5 + 10;int q, op, num;
long long m10[N];inline int read()
{int w = 1, s = 0;char c = getchar();while (c < '0' || c > '9'){if (c == '-') w = -1;c = getchar();}while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();return w * s;
}
string mod(string a,int b)//高精度a除以单精度b
{long long d = 0;for(int i = 0; i < a.size(); i ++) d = (d * 10 + (a[i] - '0')) % b; //求出余数return to_string(d);
}void init()
{string t = "1";int cnt = 6e5 + 10;int dig = 1;while (cnt --){m10[dig ++] = stoll(t);t += "0";t = mod(t, 998244353);}
}int main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);init();deque<int> s;cin >> q;s.psb(1);long long tmp = 1;while (q --){cin >> op;if (op == 1){cin >> num;s.psb(num);tmp = (tmp * 10 + num) % 998244353;}else if (op == 2){tmp = (tmp + 998244353 - ((long long)(s.front() * m10[s.size()]) % 998244353)) % 998244353;s.ppf();}elsecout << tmp << endl;}return 0;
}
今天就到这里了!
大家有什么问题尽管提,我都会尽力回答的!
吾欲您伸手,点的小赞赞。吾欲您喜欢,点得小关注!