A - 3.14
Description
Problem Statement
The number pi to the
100
100
100-th decimal place is3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
.
You are given an integer
N
N
N between
1
1
1 and
100
100
100, inclusive.
Print the value of pi to the
N
N
N-th decimal place.
More precisely, truncate the value of pi to
N
N
N decimal places and print the result without removing the trailing 0
s.
Constraints
1
≤
N
≤
100
1\leq N\leq 100
1≤N≤100
N
N
N is an integer.
Input
The input is given from Standard Input in the following format:
N N N
Output
Print the value of pi to the N N N-th decimal place in a single line.
Sample Input 1
2
Sample Output 1
3.14
Truncating the value of pi to
2
2
2 decimal places results in 3.14
. Thus, you should print 3.14
.
Sample Input 2
32
Sample Output 2
3.14159265358979323846264338327950
Do not remove the trailing 0
s.
Sample Input 3
100
Sample Output 3
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
Solution
用一个 string 存储一下然后访问即可。
Code
#include <bits/stdc++.h>
#define int long long
using namespace std;
string s = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 0; i <= n + 1; i ++)
cout << s[i];
return 0;
}
B - Roulette
Problem Statement
N
N
N people, person
1
1
1, person
2
2
2,
…
\ldots
…, person
N
N
N, are playing roulette.
The outcome of a spin is one of the
37
37
37 integers from
0
0
0 to
36
36
36.
For each
i
=
1
,
2
,
…
,
N
i = 1, 2, \ldots, N
i=1,2,…,N, person
i
i
i has bet on
C
i
C_i
Ci of the
37
37
37 possible outcomes:
A
i
,
1
,
A
i
,
2
,
…
,
A
i
,
C
i
A_{i, 1}, A_{i, 2}, \ldots, A_{i, C_i}
Ai,1,Ai,2,…,Ai,Ci.
The wheel has been spun, and the outcome is
X
X
X.
Print the numbers of all people who have bet on
X
X
X with the fewest bets, in ascending order.
More formally, print all integers
i
i
i between
1
1
1 and
N
N
N, inclusive, that satisfy both of the following conditions, in ascending order:
Person
i
i
i has bet on
X
X
X.
For each
j
=
1
,
2
,
…
,
N
j = 1, 2, \ldots, N
j=1,2,…,N, if person
j
j
j has bet on
X
X
X, then
C
i
≤
C
j
C_i \leq C_j
Ci≤Cj.
Note that there may be no number to print (see Sample Input 2).
Constraints
1
≤
N
≤
100
1 \leq N \leq 100
1≤N≤100
1
≤
C
i
≤
37
1 \leq C_i \leq 37
1≤Ci≤37
0
≤
A
i
,
j
≤
36
0 \leq A_{i, j} \leq 36
0≤Ai,j≤36
A
i
,
1
,
A
i
,
2
,
…
,
A
i
,
C
i
A_{i, 1}, A_{i, 2}, \ldots, A_{i, C_i}
Ai,1,Ai,2,…,Ai,Ci are all different for each
i
=
1
,
2
,
…
,
N
i = 1, 2, \ldots, N
i=1,2,…,N.
0
≤
X
≤
36
0 \leq X \leq 36
0≤X≤36
All input values are integers.
Input
The input is given from Standard Input in the following format:
N N N
C 1 C_1 C1
A 1 , 1 A_{1, 1} A1,1 A 1 , 2 A_{1, 2} A1,2 … \ldots … A 1 , C 1 A_{1, C_1} A1,C1
C 2 C_2 C2
A 2 , 1 A_{2, 1} A2,1 A 2 , 2 A_{2, 2} A2,2 … \ldots … A 2 , C 2 A_{2, C_2} A2,C2
⋮ \vdots ⋮
C N C_N CN
A N , 1 A_{N, 1} AN,1 A N , 2 A_{N, 2} AN,2 … \ldots … A N , C N A_{N, C_N} AN,CN
X X X
Output
Let
B
1
,
B
2
,
…
,
B
K
B_1, B_2, \ldots, B_K
B1,B2,…,BK be the sequence of numbers to be printed in ascending order.
Using the following format, print the count of numbers to be printed,
K
K
K, on the first line,
and
B
1
,
B
2
,
…
,
B
K
B_1, B_2, \ldots, B_K
B1,B2,…,BK separated by spaces on the second line:
K K K
B 1 B_1 B1 B 2 B_2 B2 … \ldots … B K B_K BK
Sample Input 1
4
3
7 19 20
4
4 19 24 0
2
26 10
3
19 31 24
19
Sample Output 1
2
1 4
The wheel has been spun, and the outcome is
19
19
19.
The people who has bet on
19
19
19 are person
1
1
1, person
2
2
2, and person
4
4
4, and the number of their bets are
3
3
3,
4
4
4, and
3
3
3, respectively.
Therefore, among the people who has bet on
19
19
19, the ones with the fewest bets are person
1
1
1 and person
4
4
4.
Sample Input 2
3
1
1
1
2
1
3
0
Sample Output 2
0
The wheel has been spun and the outcome is 0 0 0, but no one has bet on 0 0 0, so there is no number to print.
Solution
按照题目中所说的暴力即可,注意要升序排列。
Code
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e2 + 10;
int n;
int t[N];
int a[N];
unordered_map<int, int> pos[N];
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i ++)
{
cin >> t[i];
for (int j = 1; j <= t[i]; j ++)
cin >> a[j], pos[i][a[j]] = 1;
}
int x;
cin >> x;
std::vector<pair<int, int>> v;
for (int i = 1; i <= n; i ++)
if (pos[i][x])
v.push_back({t[i], i});
sort(v.begin(), v.end());
for (int i = 1; i < v.size(); i ++)
if (v[i].first > v[i - 1].first)
{
cout << i << endl;
std::vector<int> v2;
for (int j = 0; j < i; j ++)
v2.push_back(v[j].second);
sort(v2.begin(), v2.end());
for (auto c : v2)
cout << c << " ";
return 0;
}
std::vector<int> v2;
for (int j = 0; j < v.size(); j ++)
v2.push_back(v[j].second);
sort(v2.begin(), v2.end());
cout << v2.size() << endl;
for (auto c : v2)
cout << c << " ";
return 0;
}
C - Rotate Colored Subsequence
Description
Problem Statement
You are given a string
S
S
S of length
N
N
N consisting of lowercase English letters.
Each character of
S
S
S is painted in one of the
M
M
M colors: color
1
1
1, color
2
2
2, …, color
M
M
M; for each
i
=
1
,
2
,
…
,
N
i = 1, 2, \ldots, N
i=1,2,…,N, the
i
i
i-th character of
S
S
S is painted in color
C
i
C_i
Ci.
For each
i
=
1
,
2
,
…
,
M
i = 1, 2, \ldots, M
i=1,2,…,M in this order, let us perform the following operation.
Perform a right circular shift by
1
1
1 on the part of
S
S
S painted in color
i
i
i.
That is, if the
p
1
p_1
p1-th,
p
2
p_2
p2-th,
p
3
p_3
p3-th,
…
\ldots
…,
p
k
p_k
pk-th characters are painted in color
i
i
i from left to right, then simultaneously replace the
p
1
p_1
p1-th,
p
2
p_2
p2-th,
p
3
p_3
p3-th,
…
\ldots
…,
p
k
p_k
pk-th characters of
S
S
S with the
p
k
p_k
pk-th,
p
1
p_1
p1-th,
p
2
p_2
p2-th,
…
\ldots
…,
p
k
−
1
p_{k-1}
pk−1-th characters of
S
S
S, respectively.
Print the final
S
S
S after the above operations.
The constraints guarantee that at least one character of
S
S
S is painted in each of the
M
M
M colors.
Constraints
1
≤
M
≤
N
≤
2
×
1
0
5
1 \leq M \leq N \leq 2 \times 10^5
1≤M≤N≤2×105
1
≤
C
i
≤
M
1 \leq C_i \leq M
1≤Ci≤M
N
N
N,
M
M
M, and
C
i
C_i
Ci are all integers.
S
S
S is a string of length
N
N
N consisting of lowercase English letters.
For each integer
1
≤
i
≤
M
1 \leq i \leq M
1≤i≤M, there is an integer
1
≤
j
≤
N
1 \leq j \leq N
1≤j≤N such that
C
j
=
i
C_j = i
Cj=i.
Input
The input is given from Standard Input in the following format:
N N N M M M
S S S
C 1 C_1 C1 C 2 C_2 C2 … \ldots … C N C_N CN
Output
Print the answer.
Sample Input 1
8 3
apzbqrcs
1 2 3 1 2 2 1 2
Sample Output 1
cszapqbr
Initially, $S = $ apzbqrcs
.
For
i
=
1
i = 1
i=1, perform a right circular shift by
1
1
1 on the part of
S
S
S formed by the
1
1
1-st,
4
4
4-th,
7
7
7-th characters, resulting in $S = $ cpzaqrbs
.
For
i
=
2
i = 2
i=2, perform a right circular shift by
1
1
1 on the part of
S
S
S formed by the
2
2
2-nd,
5
5
5-th,
6
6
6-th,
8
8
8-th characters, resulting in $S = $ cszapqbr
.
For
i
=
3
i = 3
i=3, perform a right circular shift by
1
1
1 on the part of
S
S
S formed by the
3
3
3-rd character, resulting in $S = $ cszapqbr
(here,
S
S
S is not changed).
Thus, you should print cszapqbr
, the final
S
S
S.
Sample Input 2
2 1
aa
1 1
Sample Output 2
aa
Solution
其实暴力交换就行,看似是两重循环,但其实还是
O
(
n
)
O(n)
O(n) 的,大家不要被其迷惑了双眼。
因为,我们其实复杂度是
O
(
∑
i
=
1
k
M
i
)
O(\sum\limits_{i=1}^{k}M_i)
O(i=1∑kMi),其中
k
k
k 为颜色数量,
M
i
M_i
Mi为每一种颜色数量的个数。但是
∑
i
=
1
k
M
i
\sum\limits_{i=1}^{k}M_i
i=1∑kMi其实就等于
n
n
n,所以时间复杂度还是
O
(
n
)
O(n)
O(n) 的。
Code
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 10;
int n, m;
string s;
int c[N];
std::vector<int> pos[N];
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> n >> m >> s;
for (int i = 0; i < n; i ++)
cin >> c[i], pos[c[i]].push_back(i);
for (int i = 1; i <= m; i ++)
{
for (int j = pos[i].size() - 1; j >= 1; j --)
swap(s[pos[i][j]], s[pos[i][j - 1]]);
}
cout << s << endl;
return 0;
}
D - LOWER
Problem Statement
You are given a string
S
S
S of length
N
N
N consisting of uppercase and lowercase English letters.
Let us perform
Q
Q
Q operations on the string
S
S
S.
The
i
i
i-th operation
(
1
≤
i
≤
Q
)
(1\leq i\leq Q)
(1≤i≤Q) is represented by a tuple
(
t
i
,
x
i
,
c
i
)
(t _ i,x _ i,c _ i)
(ti,xi,ci) of two integers and one character, as follows.
If
t
i
=
1
t _ i=1
ti=1, change the
x
i
x _ i
xi-th character of
S
S
S to
c
i
c _ i
ci.
If
t
i
=
2
t _ i=2
ti=2, convert all uppercase letters in
S
S
S to lowercase (do not use
x
i
,
c
i
x _ i,c _ i
xi,ci for this operation).
If
t
i
=
3
t _ i=3
ti=3, convert all lowercase letters in
S
S
S to uppercase (do not use
x
i
,
c
i
x _ i,c _ i
xi,ci for this operation).
Print the
S
S
S after the
Q
Q
Q operations.
Constraints
1
≤
N
≤
5
×
1
0
5
1\leq N\leq5\times10^5
1≤N≤5×105
S
S
S is a string of length
N
N
N consisting of uppercase and lowercase English letters.
1
≤
Q
≤
5
×
1
0
5
1\leq Q\leq5\times10^5
1≤Q≤5×105
1
≤
t
i
≤
3
(
1
≤
i
≤
Q
)
1\leq t _ i\leq3\ (1\leq i\leq Q)
1≤ti≤3 (1≤i≤Q)
If
t
i
=
1
t _ i=1
ti=1, then
1
≤
x
i
≤
N
(
1
≤
i
≤
Q
)
1\leq x _ i\leq N\ (1\leq i\leq Q)
1≤xi≤N (1≤i≤Q).
c
i
c _ i
ci is an uppercase or lowercase English letter.
If
t
i
≠
1
t _ i\neq 1
ti=1, then
x
i
=
0
x _ i=0
xi=0 and
c
i
=
c _ i=
ci= ‘a’
.
N
,
Q
,
t
i
,
x
i
N,Q,t _ i,x _ i
N,Q,ti,xi are all integers.
Input
The input is given from Standard Input in the following format:
N N N
S S S
Q Q Q
t 1 t _ 1 t1 x 1 x _ 1 x1 c 1 c _ 1 c1
t 2 t _ 2 t2 x 2 x _ 2 x2 c 2 c _ 2 c2
⋮ \vdots ⋮
t Q t _ Q tQ x Q x _ Q xQ c Q c _ Q cQ
Output
Print the answer in a single line.
Sample Input 1
7
AtCoder
5
1 4 i
3 0 a
1 5 b
2 0 a
1 4 Y
Sample Output 1
atcYber
Initially, the string
S
S
S is AtCoder
.
The first operation changes the
4
4
4-th character to i
, changing
S
S
S to AtCider
.
The second operation converts all lowercase letters to uppercase, changing
S
S
S to ATCIDER
.
The third operation changes the
5
5
5-th character to b
, changing
S
S
S to ATCIbER
.
The fourth operation converts all uppercase letters to lowercase, changing
S
S
S to atciber
.
The fifth operation changes the
4
4
4-th character to Y
, changing
S
S
S to atcYber
.
After the operations, the string
S
S
S is atcYber
, so print atcYber
.
Sample Input 2
35
TheQuickBrownFoxJumpsOverTheLazyDog
10
2 0 a
1 19 G
1 13 m
1 2 E
1 21 F
2 0 a
1 27 b
3 0 a
3 0 a
1 15 i
Sample Output 2
TEEQUICKBROWMFiXJUGPFOVERTBELAZYDOG
Solution
贪心的思路可以想到找到最后的一个 3 3 3 或 2 2 2 的操作,因为到达这个点要么都是小写,要么都是大写。所以在此之前只需要处理字符的转变即可,然后将整个序列全转成大写或小写,最后将后面的修改处理完就可以啦。
Code
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 10;
int n, q;
string s;
int op[N], x[N];
char c[N];
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> n >> s >> q;
int last = 0;
for (int i = 1; i <= q; i ++)
{
cin >> op[i] >> x[i] >> c[i];
if (op[i] == 2 || op[i] == 3)
last = i;
}
int l1 = 0, l2 = 0;
for (int i = 1; i <= last; i ++)
if (op[i] == 1)
s[x[i] - 1] = c[i];
else if (op[i] == 2)
l1 = i;
else
l2 = i;
if (l1 == 0 && l2 == 0);
else if (l1 > l2)
{
for (auto &c : s)
if (c >= 'A' && c <= 'Z')
c = c - 'A' + 'a';
}
else
{
for (auto &c : s)
if (c >= 'a' && c <= 'z')
c = c - 'a' + 'A';
}
for (int i = last + 1; i <= q; i ++)
s[x[i] - 1] = c[i];
cout << s << endl;
return 0;
}
E - Roulettes
奋力钻研中…
F - A Certain Game
奋力钻研中…
G - Amulets
Description
Problem Statement
There are
N
N
N monsters in a cave: monster
1
1
1, monster
2
2
2,
…
\ldots
…, monster
N
N
N. Each monster has a positive integer attack power and a type represented by an integer between
1
1
1 and
M
M
M, inclusive.
Specifically, for
i
=
1
,
2
,
…
,
N
i = 1, 2, \ldots, N
i=1,2,…,N, the attack power and type of monster
i
i
i are
A
i
A_i
Ai and
B
i
B_i
Bi, respectively.
Takahashi will go on an adventure in this cave with a health of
H
H
H and some of the
M
M
M amulets: amulet
1
1
1, amulet
2
2
2,
…
\ldots
…, amulet
M
M
M.
In the adventure, Takahashi performs the following steps for
i
=
1
,
2
,
…
,
N
i = 1, 2, \ldots, N
i=1,2,…,N in this order (as long as his health does not drop to
0
0
0 or below).
- If Takahashi has not brought amulet B i B_i Bi with him, monster i i i will attack him and decrease his health by A i A_i Ai.
- Then,
- if his health is greater than 0 0 0, he defeats monster i i i;
- otherwise, he dies without defeating monster i i i and ends his adventure.
Solve the following problem for each K = 0 , 1 , … , M K = 0, 1, \ldots, M K=0,1,…,M independently.
Find the maximum number of monsters that Takahashi can defeat when choosing K K K of the M M M amulets to bring on the adventure.
The constraints guarantee that there is at least one monster of type i i i for each i = 1 , 2 , … , M i = 1, 2, \ldots, M i=1,2,…,M.
Constraints
1
≤
M
≤
N
≤
3
×
1
0
5
1 \leq M \leq N \leq 3 \times 10^5
1≤M≤N≤3×105
1
≤
H
≤
1
0
9
1 \leq H \leq 10^9
1≤H≤109
1
≤
A
i
≤
1
0
9
1 \leq A_i \leq 10^9
1≤Ai≤109
1
≤
B
i
≤
M
1 \leq B_i \leq M
1≤Bi≤M
For each
1
≤
i
≤
M
1 \leq i \leq M
1≤i≤M, there is
1
≤
j
≤
N
1 \leq j \leq N
1≤j≤N such that
B
j
=
i
B_j = i
Bj=i.
All input values are integers.
Input
The input is given from Standard Input in the following format:
N N N M M M H H H
A 1 A_1 A1 B 1 B_1 B1
A 2 A_2 A2 B 2 B_2 B2
⋮ \vdots ⋮
A N A_N AN B N B_N BN
Output
For each
i
=
0
,
1
,
2
,
…
,
M
i = 0, 1, 2, \ldots, M
i=0,1,2,…,M, let
X
i
X_i
Xi be the maximum number of monsters that Takahashi can defeat when
K
=
i
K = i
K=i.
Print
X
0
,
X
1
,
…
,
X
M
X_0, X_1, \ldots, X_M
X0,X1,…,XM separated by spaces in the following format:
X 0 X_0 X0 X 1 X_1 X1 … \ldots … X M X_M XM
Sample Input 1
7 3 7
3 2
1 1
4 2
1 2
5 1
9 3
2 3
Sample Output 1
2 5 7 7
Consider the case
K
=
1
K = 1
K=1. Here, Takahashi can bring amulet
2
2
2 to defeat the maximum possible number of monsters, which is
5
5
5.
The adventure proceeds as follows.
For
i
=
1
i = 1
i=1, he avoids the attack of monster
1
1
1 since he has amulet
2
2
2. Then, he defeats monster
1
1
1.
For
i
=
2
i = 2
i=2, he takes the attack of monster
2
2
2 and his health becomes
6
6
6 since he does not have amulet
1
1
1. Then, he defeats monster
2
2
2.
For
i
=
3
i = 3
i=3, he avoids the attack of monster
3
3
3 since he has amulet
2
2
2. Then, he defeats monster
3
3
3.
For
i
=
4
i = 4
i=4, he avoids the attack of monster
4
4
4 since he has amulet
2
2
2. Then, he defeats monster
4
4
4.
For
i
=
5
i = 5
i=5, he takes the attack of monster
5
5
5 and his health becomes
1
1
1 since he does not have amulet
1
1
1. Then, he defeats monster
5
5
5.
For
i
=
6
i = 6
i=6, he takes the attack of monster
6
6
6 and his health becomes
−
8
-8
−8 since he does not have amulet
3
3
3. Then, he dies without defeating monster
6
6
6 and ends his adventure.
Similarly, when
K
=
0
K=0
K=0, he can defeat
2
2
2 monsters; when
K
=
2
K=2
K=2, he can defeat all
7
7
7 monsters by bringing amulets
2
2
2 and
3
3
3; when
K
=
3
K=3
K=3, he can defeat all
7
7
7 monsters by bringing amulets
1
1
1,
2
2
2, and
3
3
3.
Sample Input 2
15 5 400
29 5
27 4
79 1
27 2
30 3
4 1
89 2
88 3
75 5
3 1
39 4
12 1
62 4
38 2
49 1
Sample Output 2
8 12 15 15 15 15
Solution
动态维护每一种选择的护身符抵挡的伤害之和的集合( u s e use use),以及每一种不选择的护身符所造成的伤害之和的集合( u n u s e d unused unused)。集合的类型为 multiset。
对于打死怪物 1 ∼ x 1\sim x 1∼x( x ∈ [ 1 , n ] x\in[1, n] x∈[1,n]):就是在打败怪物 1 ∼ x − 1 1\sim x-1 1∼x−1 的基础上,打死怪物 x x x
- 如果之前选过怪物 x x x 的类型的护身符,那么怪物 x x x 就不会再造成伤害,那么 u s e use use 集合中的抵挡当前怪物类型的伤害总和就会增加 A x A_x Ax( A x A_x Ax 表示怪物 x x x 的攻击量)
- 如果之前没有选过怪物 x x x 的类型的护身符,那么怪物 x x x 就会造成 A x A_x Ax 攻击量,所以对于 u n u s e d unused unused 集合中怪物 x x x 的类型所造成的伤害之和就会增加 A x A_x Ax
然后,我们进行贪心的考虑,如果 u s e use use 集合最小抵挡的伤害量,也就是 u s e use use 集合的第一个元素(因为是 multiset,集合是升序的)小于 u n u s e d unused unused 集合最大造成的伤害量,也就是 u n u s e d unused unused 集合的最后一个元素,那么如果我们不选择 u s e use use 集合第一个元素所代表的护身符的类型,而选择 u n u s e d unused unused 集合最后一个元素所代表的护身符的类型,那么护身符所抵挡伤害之和一定就会增加,而我们所要承受的伤害反而会减少!何乐而不为呢?
当然我们在计算的过程中也要维护一个需要承受的伤害的总值,也就是 u n u s e d unused unused 集合中所有元素的和。这样我们在每一次进行完上面的操作之后,承受的伤害总和一定是最少的了,所以此时判断一下有没有大于等于我们的血量,如果大于等于了,那么我们再贪心的选择 u n u s e d unused unused 集合中选择抵挡血量最多的护身符,然后使用它。
最后,我们就需要统计答案了,对于打死怪物 1 ∼ x 1\sim x 1∼x 所需要的护身符数量,就是 u s e use use 集合的元素个数,那么在 1 ∼ x − 1 1\sim x-1 1∼x−1 所需要的护身符的数量,一直到小于当前 u s e use use 集合的数量这一段区间所用的护身符都只能杀死 x − 1 x-1 x−1 只怪物,记录一下即可。然后,计算打死所有怪物的数量所需的护身符的数量。
这样,就可以顺利的解决这道题了!文章来源:https://www.toymoban.com/news/detail-645254.html
Code
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int SIZE = 3e5 + 10;
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int N, M, H;
cin >> N >> M >> H;
std::vector<int> Attack(N + 1, 0), Kind(N + 1, 0);
std::vector<int> Health(N + 1, 0); //Health存储每一种护身符的数量
for (int i = 1; i <= N; i ++)
cin >> Attack[i] >> Kind[i];
multiset<int> Use, Unused; //存储每一种护身符选/不选所抵挡/造成的伤害之和
int Health_Damage = 0, Amulets = 0;
for (int i = 1; i <= M; i ++) Unused.insert(0);
std::vector<int> Result(M + 1);
for (int i = 1; i <= N; i ++)
{
int Past = Health[Kind[i]];
int Now = Health[Kind[i]] + Attack[i];
Health[Kind[i]] = Now;
if (Use.count(Past)) //说明之前已经使用了这个种类,我们就可以将其抵挡的伤害改为Now。
{
Use.erase(Use.find(Past)); //删除再插入=修改 orz,orz!
Use.insert(Now);
}
else //说明此时会造成伤害
{
Unused.erase(Unused.find(Past));
Unused.insert(Now);
Health_Damage += Attack[i]; //伤害数会增多
}
if (Use.size() && Unused.size() && *Unused.rbegin() > *Use.begin()) //这说明我们当前的选择不是最优,将其改为最优即可,之前讲解也说过了~~~
{
int A = *Unused.rbegin(), B = *Use.begin();
Unused.erase(Unused.find(A));
Unused.insert(B);
Health_Damage += B - A; //造成的伤害数会减少
Use.erase(Use.find(B));
Use.insert(A);
}
if (Unused.size() && Health_Damage >= H) //说明我们嗝屁了,需要多一些护身符
{
int A = *Unused.rbegin(); //贪心的选择给我们防护最大的
Unused.erase(Unused.find(A));
Use.insert(A);
Health_Damage -= A;
}
//大家可以想一想为什么上面两个用 if 即可,没有必要用 while
while (Amulets < Use.size()) //记录答案,此时看似是个 while,其实总共最多就循环 m 次,因为 Amulets 会不停地加,最多加 M 次。
{
Result[Amulets] = i - 1; //这里我们考虑打死怪物 1 ~ i - 1 多少个护身符的个数,而不是打死怪物 i,因为有很多种可能,就比如说你选择 2 个或 3 个护身符都是打死怪物 i - 1,而此时我们只是知道打死怪物 i,所需的最少的个数,也就是下界,而不知道上界,上一次的打死 1 ~ i - 1 所需的数量就是下界,而此时诞生出了上界,也就是打死第 i 个怪物至少需要的次数减 1。所以考虑的是打死怪物 i - 1。如果还是不明白的话,可以手算一下~~~
Amulets ++;
}
}
while (Amulets <= M) //统计打死所有怪物,所需的护身符
{
Result[Amulets] = N;
Amulets ++;
}
for (auto c : Result)
cout << c << " ";
return 0;
}
Time Complexity: O ( N log 2 N ) O(N\log_2{N}) O(Nlog2N)
参考了大佬 ksun48 的代码~~~文章来源地址https://www.toymoban.com/news/detail-645254.html
到了这里,关于AtCoder Beginner Contest 314(A~D题 + G题)讲解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!