蒟蒻来讲题,还望大家喜。若哪有问题,大家尽可提!
Hello, 大家好哇!本初中生蒟蒻讲解一下AtCoder Beginner Contest 303这场比赛的A-E题!
===========================================================================================
A - Similar String
原题
Problem Statement
Two characters
x
x
x and
y
y
y are called similar characters if and only if one of the following conditions is satisfied:
x
x
x and
y
y
y are the same character.
One of
x
x
x and
y
y
y is 1
and the other is l
.
One of
x
x
x and
y
y
y is 0
and the other is o
.
Two strings
S
S
S and
T
T
T, each of length
N
N
N, are called similar strings if and only if:
for all
i
(
1
≤
i
≤
N
)
i\ (1\leq i\leq N)
i (1≤i≤N), the
i
i
i-th character of
S
S
S and the
i
i
i-th character of
T
T
T are similar characters.
Given two length-
N
N
N strings
S
S
S and
T
T
T consisting of lowercase English letters and digits, determine if
S
S
S and
T
T
T are similar strings.
Constraints
N
N
N is an integer between
1
1
1 and
100
100
100.
Each of
S
S
S and
T
T
T is a string of length
N
N
N consisting of lowercase English letters and digits.
Input
The input is given from Standard Input in the following format:
N
N
N
S
S
S
T
T
T
Output
Print Yes
if
S
S
S and
T
T
T are similar strings, and No
otherwise.
Sample Input 1
3
l0w
1ow
Sample Output 1
Yes
The
1
1
1-st character of
S
S
S is l
, and the
1
1
1-st character of
T
T
T is 1
. These are similar characters.
The
2
2
2-nd character of
S
S
S is 0
, and the
2
2
2-nd character of
T
T
T is o
. These are similar characters.
The
3
3
3-rd character of
S
S
S is w
, and the
3
3
3-rd character of
T
T
T is w
. These are similar characters.
Thus,
S
S
S and
T
T
T are similar strings.
Sample Input 2
3
abc
arc
Sample Output 2
No
The
2
2
2-nd character of
S
S
S is b
, and the
2
2
2-nd character of
T
T
T is r
. These are not similar characters.
Thus,
S
S
S and
T
T
T are not similar strings.
Sample Input 3
4
nok0
n0ko
Sample Output 3
Yes
题目大意
判断两个字符串是否一致,其中l与1,o与0等价。
思路
太简单,不写了,直接见代码
代码
#include <iostream>
using namespace std;
int main()
{
int n;
string a, b;
cin >> n >> a >> b;
a = ' ' + a, b = ' ' + b;
for (int i = 1; i <= n; i ++)
{
if (a[i] == '0')
a[i] = 'o';
if (b[i] == '0')
b[i] = 'o';
if (a[i] == '1')
a[i] = 'l';
if (b[i] == '1')
b[i] = 'l';
}
if (a == b)
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
B - Discord
原题
Problem Statement
N
N
N people numbered
1
,
2
,
…
,
N
1,2,\ldots,N
1,2,…,N were in
M
M
M photos. In each of the photos, they stood in a single line. In the
i
i
i-th photo, the
j
j
j-th person from the left is person
a
i
,
j
a_{i,j}
ai,j.
Two people who did not stand next to each other in any of the photos may be in a bad mood.
How many pairs of people may be in a bad mood? Here, we do not distinguish a pair of person
x
x
x and person
y
y
y, and a pair of person
y
y
y and person
x
x
x.
Constraints
2
≤
N
≤
50
2 \leq N \leq 50
2≤N≤50
1
≤
M
≤
50
1 \leq M \leq 50
1≤M≤50
1
≤
a
i
,
j
≤
N
1 \leq a_{i,j} \leq N
1≤ai,j≤N
a
i
,
1
,
…
,
a
i
,
N
a_{i,1},\ldots,a_{i,N}
ai,1,…,ai,N contain each of
1
,
…
,
N
1,\ldots,N
1,…,N exactly once.
All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N
N
N
M
M
M
a
1
,
1
a_{1,1}
a1,1
…
\ldots
…
a
1
,
N
a_{1,N}
a1,N
⋮
\vdots
⋮
a
M
,
1
a_{M,1}
aM,1
…
\ldots
…
a
M
,
N
a_{M,N}
aM,N
Output
Print the answer.
Sample Input 1
4 2
1 2 3 4
4 3 1 2
Sample Output 1
2
The pair of person 1 1 1 and person 4 4 4, and the pair of person 2 2 2 and person 4 4 4, may be in a bad mood.
Sample Input 2
3 3
1 2 3
3 1 2
1 2 3
Sample Output 2
0
Sample Input 3
10 10
4 10 7 2 8 3 9 1 6 5
3 6 2 9 1 8 10 7 4 5
9 3 4 5 7 10 1 8 2 6
7 3 1 8 4 9 5 6 2 10
5 2 1 4 10 7 9 8 3 6
5 8 1 6 9 3 2 4 7 10
8 10 3 4 5 7 2 9 6 1
3 10 2 7 8 5 1 4 9 6
10 6 1 5 4 2 3 8 9 7
4 5 9 1 8 2 7 6 3 10
Sample Output 3
6
题目大意
没有在同一张照片里出现过的两个人的对数
思路
太简单,不写了,直接见代码
代码
#include <iostream>
#include <set>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
swap(n, m);
int a[55][55];
set<pair<int, int>> res;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= m; j ++)
cin >> a[i][j];
for (int i = 1; i <= m; i ++)
for (int j = i + 1; j <= m; j ++)
res.insert({i, j});
for (int i = 1; i <= n; i ++)
for (int j = 1; j < m; j ++)
{
int t1 = min(a[i][j], a[i][j + 1]), t2 = max(a[i][j], a[i][j + 1]);
if (res.count({t1, t2}))
res.erase({t1, t2});
}
cout << res.size() << endl;
return 0;
}
C - Dash
原题
Problem Statement
On a two-dimensional plane, Takahashi is initially at point
(
0
,
0
)
(0, 0)
(0,0), and his initial health is
H
H
H.
M
M
M items to recover health are placed on the plane; the
i
i
i-th of them is placed at
(
x
i
,
y
i
)
(x_i,y_i)
(xi,yi).
Takahashi will make
N
N
N moves. The
i
i
i-th move is as follows.
Let
(
x
,
y
)
(x,y)
(x,y) be his current coordinates. He consumes a health of
1
1
1 to move to the following point, depending on
S
i
S_i
Si, the
i
i
i-th character of
S
S
S:
(
x
+
1
,
y
)
(x+1,y)
(x+1,y) if
S
i
S_i
Si is R
;
(
x
−
1
,
y
)
(x-1,y)
(x−1,y) if
S
i
S_i
Si is L
;
(
x
,
y
+
1
)
(x,y+1)
(x,y+1) if
S
i
S_i
Si is U
;
(
x
,
y
−
1
)
(x,y-1)
(x,y−1) if
S
i
S_i
Si is D
.
If Takahashi’s health has become negative, he collapses and stops moving. Otherwise, if an item is placed at the point he has moved to, and his health is strictly less than
K
K
K, then he consumes the item there to make his health
K
K
K.
Determine if Takahashi can complete the N N N moves without being stunned.
Constraints
1
≤
N
,
M
,
H
,
K
≤
2
×
1
0
5
1\leq N,M,H,K\leq 2\times 10^5
1≤N,M,H,K≤2×105
S
S
S is a string of length
N
N
N consisting of R
, L
, U
, and D
.
∣
x
i
∣
,
∣
y
i
∣
≤
2
×
1
0
5
|x_i|,|y_i| \leq 2\times 10^5
∣xi∣,∣yi∣≤2×105
(
x
i
,
y
i
)
(x_i, y_i)
(xi,yi) are pairwise distinct.
All values in the input are integers, except for
S
S
S.
Input
The input is given from Standard Input in the following format:
N
N
N
M
M
M
H
H
H
K
K
K
S
S
S
x
1
x_1
x1
y
1
y_1
y1
⋮
\vdots
⋮
x
M
x_M
xM
y
M
y_M
yM
Output
Print Yes
if he can complete the
N
N
N moves without being stunned; print No
otherwise.
Sample Input 1
4 2 3 1
RUDL
-1 -1
1 0
Sample Output 1
Yes
Initially, Takahashi’s health is
3
3
3. We describe the moves below.
1
1
1-st move:
S
i
S_i
Si is R
, so he moves to point
(
1
,
0
)
(1,0)
(1,0). His health reduces to
2
2
2. Although an item is placed at point
(
1
,
0
)
(1,0)
(1,0), he do not consume it because his health is no less than
K
=
1
K=1
K=1.
2
2
2-nd move:
S
i
S_i
Si is U
, so he moves to point
(
1
,
1
)
(1,1)
(1,1). His health reduces to
1
1
1.
3
3
3-rd move:
S
i
S_i
Si is D
, so he moves to point
(
1
,
0
)
(1,0)
(1,0). His health reduces to
0
0
0. An item is placed at point
(
1
,
0
)
(1,0)
(1,0), and his health is less than
K
=
1
K=1
K=1, so he consumes the item to make his health
1
1
1.
4
4
4-th move:
S
i
S_i
Si is L
, so he moves to point
(
0
,
0
)
(0,0)
(0,0). His health reduces to
0
0
0.
Thus, he can make the
4
4
4 moves without collapsing, so Yes
should be printed. Note that the health may reach
0
0
0.
Sample Input 2
5 2 1 5
LDRLD
0 0
-1 -1
Sample Output 2
No
Initially, Takahashi’s health is
1
1
1. We describe the moves below.
1
1
1-st move:
S
i
S_i
Si is L
, so he moves to point
(
−
1
,
0
)
(-1,0)
(−1,0). His health reduces to
0
0
0.
2
2
2-nd move:
S
i
S_i
Si is D
, so he moves to point
(
−
1
,
−
1
)
(-1,-1)
(−1,−1). His health reduces to
−
1
-1
−1. Now that the health is
−
1
-1
−1, he collapses and stops moving.
Thus, he will be stunned, so No
should be printed.
Note that although there is an item at his initial point
(
0
,
0
)
(0,0)
(0,0), he does not consume it before the
1
1
1-st move, because items are only consumed after a move.
题目大意
在一个二维空间里,Takahashi从(0,0)点开始走,每走一步H减1,如果H减到负就不能再走了。如果遇到标记的点,此时如果H减到小于K时,将消耗掉这个标记点的K,使得H重新回到K。如果能够把字符串S中的每一步都走完,那么输出Yes,否则转出No.
思路
这道题目就正常模拟就可以,但一定一定一定要注意,当走过标记点时,如果H小于K,此时要把这个点的K消耗掉,这个点不注意,就会一直有4个样例WA。
代码
#include <iostream>
#include <map>
using namespace std;
map<pair<int, int>, int> has;
map<char, int> sd;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int main()
{
sd['R'] = 0, sd['L'] = 1, sd['U'] = 2, sd['D'] = 3;
int n, m, h, k;
cin >> n >> m >> h >> k;
string s;
cin >> s;
s = ' ' + s;
int x, y;
for (int i = 1; i <= m; i ++)
cin >> x >> y, has[{x, y}] = 1;
x = 0, y = 0;
for (int i = 1; i <= n; i ++)
{
x += dx[sd[s[i]]], y += dy[sd[s[i]]];
h --;
if (h < 0) break;
if (has[{x, y}] && h < k) h = k, has[{x, y}] = 0;
//这里的has[{x, y}] = 0一定要有,因为把K消耗掉了.
}
if (h >= 0) cout << "Yes" << endl;
else cout << "No" << endl;
}
D - Shift vs. CapsLock
原题
Problem Statement
Your computer has a keyboard with three keys: ‘a’ key, Shift key, and Caps Lock key. The Caps Lock key has a light on it.
Initially, the light on the Caps Lock key is off, and the screen shows an empty string.
You can do the following three actions any number of times in any order:
Spend
X
X
X milliseconds to press only the ‘a’ key. If the light on the Caps Lock key is off, a
is appended to the string on the screen; if it is on, A
is.
Spend
Y
Y
Y milliseconds to press the ‘a’ key and Shift key simultaneously. If the light on the Caps Lock key is off, A
is appended to the string on the screen; if it is on, a
is.
Spend
Z
Z
Z milliseconds to press the Caps Lock key. If the light on the Caps Lock key is off, it turns on; if it is on, it turns off.
Given a string
S
S
S consisting of A
and a
, determine at least how many milliseconds you need to spend to make the string shown on the screen equal to
S
S
S.
Constraints
1
≤
X
,
Y
,
Z
≤
1
0
9
1 \leq X,Y,Z \leq 10^9
1≤X,Y,Z≤109
X
X
X,
Y
Y
Y, and
Z
Z
Z are integers.
1
≤
∣
S
∣
≤
3
×
1
0
5
1 \leq |S| \leq 3 \times 10^5
1≤∣S∣≤3×105
S
S
S is a string consisting of A
and a
.
Input
The input is given from Standard Input in the following format:
X
X
X
Y
Y
Y
Z
Z
Z
S
S
S
Output
Print the answer.
Sample Input 1
1 3 3
AAaA
Sample Output 1
9
The following sequence of actions makes the string on the screen equal to AAaA
in
9
9
9 milliseconds, which is the shortest possible.
Spend
Z
(
=
3
)
Z(=3)
Z(=3) milliseconds to press the CapsLock key. The light on the Caps Lock key turns on.
Spend
X
(
=
1
)
X(=1)
X(=1) milliseconds to press the ‘a’ key. A
is appended to the string on the screen.
Spend
X
(
=
1
)
X(=1)
X(=1) milliseconds to press the ‘a’ key. A
is appended to the string on the screen.
Spend
Y
(
=
3
)
Y(=3)
Y(=3) milliseconds to press the Shift key and ‘a’ key simultaneously. a
is appended to the string on the screen.
Spend
X
(
=
1
)
X(=1)
X(=1) milliseconds to press the ‘a’ key. A
is appended to the string on the screen.
Sample Input 2
1 1 100
aAaAaA
Sample Output 2
6
Sample Input 3
1 2 4
aaAaAaaAAAAaAaaAaAAaaaAAAAA
Sample Output 3
40
题目大意
键盘有caps、shift和a三个键,具体使用与键盘完全一样。每次按不同键消耗时间不一样,问输出字符串S消耗的最短时间
思路
这道题可以用我们的动态规划: d p i , j dp_{i,j} dpi,j表示按第i个字符的最小时间,其中 j = 0 j=0 j=0表示没按着 c a p s caps caps键,反之,即按着。
转移分4个方面:
第一种:直接按a
键,消耗为
X
X
X
第二种:按着Shift
再按a
,消耗为
Y
Y
Y
第三种:按下caps
键再按a
,消耗为
Z
+
X
Z+X
Z+X
第四种:按下caps
键再按shift
键a
,消耗为
Z
+
Y
Z+Y
Z+Y
代码
#include <iostream>
#include <cstring>
#define int long long
using namespace std;
const int N = 3e5 + 10;
int dp[N][2];
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int x, y, z;
string s;
cin >> x >> y >> z >> s;
memset(dp, 0x3f, sizeof dp);
dp[0][0] = 0;
for (int i = 0; i < s.size(); i ++)
{
int cur = (s[i] == 'A');
for (int caps = 0; caps <= 1; caps ++)
for (int want = 0; want <= 1; want ++)
{
int cost = dp[i][caps];
if (want != caps) cost += z;
if (cur == want) cost += x;
else cost += y;
dp[i + 1][want] = min(dp[i + 1][want], cost);
}
}
cout << min(dp[s.size()][0], dp[s.size()][1]) << endl;
return 0;
}
E - A Gift From the Stars
原题
Problem Statement
A graph with
(
k
+
1
)
(k+1)
(k+1) vertices and
k
k
k edges is called a level-
k
(
k
≥
2
)
k\ (k\geq 2)
k (k≥2) star if and only if:
it has a vertex that is connected to each of the other
k
k
k vertices with an edge, and there are no other edges.
At first, Takahashi had a graph consisting of stars. He repeated the following operation until every pair of vertices in the graph was connected:
choose two vertices in the graph. Here, the vertices must be disconnected, and their degrees must be both
1
1
1. Add an edge that connects the chosen two vertices.
He then arbitrarily assigned an integer from
1
1
1 through
N
N
N to each of the vertices in the graph after the procedure. The resulting graph is a tree; we call it
T
T
T.
T
T
T has
(
N
−
1
)
(N-1)
(N−1) edges, the
i
i
i-th of which connects
u
i
u_i
ui and
v
i
v_i
vi.
Takahashi has now forgotten the number and levels of the stars that he initially had. Find them, given
T
T
T.
Constraints
3
≤
N
≤
2
×
1
0
5
3\leq N\leq 2\times 10^5
3≤N≤2×105
1
≤
u
i
,
v
i
≤
N
1\leq u_i, v_i\leq N
1≤ui,vi≤N
The given graph is an
N
N
N-vertex tree obtained by the procedure in the problem statement.
All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N
N
N
u
1
u_1
u1
v
1
v_1
v1
⋮
\vdots
⋮
u
N
−
1
u_{N-1}
uN−1
v
N
−
1
v_{N-1}
vN−1
Output
Suppose that Takahashi initially had
M
M
M stars, whose levels were
L
=
(
L
1
,
L
2
,
…
,
L
M
)
L=(L_1,L_2,\ldots,L_M)
L=(L1,L2,…,LM).
Sort
L
L
L in ascending order, and print them with spaces in between.
We can prove that the solution is unique in this problem.
Sample Input 1
6
1 2
2 3
3 4
4 5
5 6
Sample Output 1
2 2
Two level-
2
2
2 stars yield
T
T
T, as the following figure shows:
Sample Input 2
9
3 9
7 8
8 6
4 6
4 1
5 9
7 3
5 2
Sample Output 2
2 2 2
Sample Input 3
20
8 3
8 18
2 19
8 20
9 17
19 7
8 7
14 12
2 15
14 10
2 13
2 16
2 1
9 5
10 15
14 6
2 4
2 11
5 12
Sample Output 3
2 3 4 7
题目大意
星图是指中心点连着旁边所有点,旁边的所有点度数为1。题目给你一个图,要你在这个图中删边,能够形成的每个星图的大小,还要计算一下一共有多少个星图。
思路
一共两种情况:
情况1:若有一个点连着的所有点的个数大于等于3,那么定然是一个星图。此时,将星图的个数加1,删掉这个星图的点的个数减1,因为有一个点会连接着另一个星图,不能删!
情况2:剩下的就都是周围点个数为2的星图了,此时我们将剩余的点的个数除以3,就是剩余的个数,这些星图的大小都是2。
详情见代码吧~~~
代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 2e5 + 10;
int n;
int u, v;
int din[N];
vector<int> res;
int main()
{
cin >> n;
for (int i = 1; i < n; i ++)
cin >> u >> v, din[u] ++, din[v] ++;
int ans = n;
for (int i = 1; i <= n; i ++)
if (din[i] >= 3)
{
ans -= din[i] + 1;
res.push_back(din[i]);
}
for (int i = 1; i <= ans / 3; i ++)
res.push_back(2);
sort(res.begin(), res.end());
for (auto c : res)
cout << c << " ";
}
最近作业太多,所以一直拖到今天才发。
大家有什么问题尽管提,我都会尽力回答的!
吾欲您伸手,点的小赞赞。吾欲您喜欢,点得小关注!