AtCoder Beginner Contest 354 (ABCDEFG题)视频讲解

2024年5月19日补充G题。

A - Exponential Plant

Problem Statement

Takahashi is growing a plant. Its height at the time of germination is 0 c m 0\,\mathrm{cm} 0cm. Considering the day of germination as day 0 0 0, its height increases by 2 i c m 2^i\,\mathrm{cm} 2icm day i i i’s night ( 0 ≤ i ) (0 \le i) (0i).
Takahashi’s height is H c m H\,\mathrm{cm} Hcm.
Every morning, Takahashi measures his height against this plant. Find the first day such that the plant’s height is strictly greater than Takahashi’s height in the morning.

Constraints

1 ≤ H ≤ 1 0 9 1 \leq H \leq 10^{9} 1H109
All input values are integers.

Input

The input is given from Standard Input in the following format:

H H H

Output

Print an integer representing the first day such that the plant’s height is greater than Takahashi’s height in the morning.

Sample Input 1

54

Sample Output 1

6

The plant’s height in the mornings of days 1 , 2 , 3 , 4 , 5 , 6 1, 2, 3, 4, 5, 6 1,2,3,4,5,6 will be 1 c m , 3 c m , 7 c m , 15 c m , 31 c m , 63 c m 1\,\mathrm{cm}, 3\,\mathrm{cm}, 7\,\mathrm{cm}, 15\,\mathrm{cm}, 31\,\mathrm{cm}, 63\,\mathrm{cm} 1cm,3cm,7cm,15cm,31cm,63cm, respectively. The plant becomes taller than Takahashi in the morning day 6 6 6, so print 6 6 6.

Sample Input 2

7

Sample Output 2

4

The plant’s height will be 7 c m 7\,\mathrm{cm} 7cm in the morning of day 3 3 3 and 15 c m 15\,\mathrm{cm} 15cm in the morning day 4 4 4. The plant becomes taller than Takahashi in the morning of day 4 4 4, so print 4 4 4. Note that, in the morning of day 3 3 3, the plant is as tall as Takahashi, but not taller.

Sample Input 3

262144

Sample Output 3

19

Solution

具体见文末视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int x;cin >> x;int j = 1;while ((1ll << j) - 1 <= x) j ++;cout << j << endl;return 0;
}

B - AtCoder Janken 2

Problem Statement

N N N AtCoder users have gathered to play AtCoder RPS 2. The i i i-th user’s name is S i S_i Si and their rating is C i C_i Ci.
AtCoder RPS 2 is played as follows:
Assign the numbers 0 , 1 , … , N − 1 0, 1, \dots, N - 1 0,1,,N1 to the users in lexicographical order of their usernames.
Let T T T be the sum of the ratings of the N N N users. The user assigned the number T m o d N T \bmod N TmodN is the winner.
Print the winner’s username.

What is lexicographical order? Lexicographical order, simply put, means "the order in which words appear in a dictionary." More precisely, the algorithm to determine the order of two distinct strings $S$ and $T$ consisting of lowercase English letters is as follows: Here, "the $i$-th character of $S$" is denoted as $S_i$. If $S$ is lexicographically smaller than $T$, we write $S \lt T$, and if $S$ is larger, we write $S \gt T$.
  1. Let $L$ be the length of the shorter string among $S$ and $T$. Check if $S_i$ and $T_i$ match for $i=1,2,\dots,L$. If there exists an $i$ such that $S_i \neq T_i$, let $j$ be the smallest such $i$. Compare $S_j$ and $T_j$. If $S_j$ is alphabetically smaller than $T_j$, then $S \lt T$. Otherwise, $S \gt T$. The algorithm ends here. If there is no $i$ such that $S_i \neq T_i$, compare the lengths of $S$ and $T$. If $S$ is shorter than $T$, then $S \lt T$. If $S$ is longer, then $S \gt T$. The algorithm ends here.
## Constraints

1 ≤ N ≤ 100 1 \leq N \leq 100 1N100
S i S_i Si is a string consisting of lowercase English letters with length between 3 3 3 and 16 16 16, inclusive.
S 1 , S 2 , … , S N S_1, S_2, \dots, S_N S1,S2,,SN are all distinct.
1 ≤ C i ≤ 4229 1 \leq C_i \leq 4229 1Ci4229
C i C_i Ci is an integer.

Input

The input is given from Standard Input in the following format:

N N N
S 1 S_1 S1 C 1 C_1 C1
S 2 S_2 S2 C 2 C_2 C2
⋮ \vdots
S N S_N SN C N C_N CN

Output

Print the answer on a single line.

Sample Input 1

3
takahashi 2
aoki 6
snuke 5

Sample Output 1

snuke

The sum of the ratings of the three users is 13 13 13. Sorting their names in lexicographical order yields aoki, snuke, takahashi, so aoki is assigned number 0 0 0, snuke is 1 1 1, and takahashi is 2 2 2.
Since 13 m o d 3 = 1 13 \bmod 3 = 1 13mod3=1, print snuke, who is assigned number 1 1 1.

Sample Input 2

3
takahashi 2813
takahashixx 1086
takahashix 4229

Sample Output 2

takahashix

Solution

具体见文末视频。

Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int n;cin >> n;std::vector<string> s(n);int x, sum = 0;for (int i = 0; i < n; i ++)cin >> s[i] >> x, sum += x;sort(s.begin(), s.end());cout << s[sum % n] << endl;return 0;
}

C - AtCoder Magics

Problem Statement

Takahashi has N N N cards from the card game “AtCoder Magics.” The i i i-th card will be called card i i i. Each card has two parameters: strength and cost. Card i i i has a strength of A i A_i Ai and a cost of C i C_i Ci.
He does not like weak cards, so he will discard them. Specifically, he will repeat the following operation until it can no longer be performed:
Choose two cards x x x and y y y such that KaTeX parse error: Expected 'EOF', got '&' at position 5: A_x &̲gt; A_y and KaTeX parse error: Expected 'EOF', got '&' at position 5: C_x &̲lt; C_y. Discard card y y y.
It can be proved that the set of remaining cards when the operations can no longer be performed is uniquely determined. Find this set of cards.

Constraints

2 ≤ N ≤ 2 × 1 0 5 2 \leq N \leq 2 \times 10^5 2N2×105
1 ≤ A i , C i ≤ 1 0 9 1 \leq A_i, C_i \leq 10^9 1Ai,Ci109
A 1 , A 2 , … , A N A_1, A_2, \dots ,A_N A1,A2,,AN are all distinct.
C 1 , C 2 , … , C N C_1, C_2, \dots ,C_N C1,C2,,CN are all distinct.
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N
A 1 A_1 A1 C 1 C_1 C1
A 2 A_2 A2 C 2 C_2 C2
⋮ \vdots
A N A_N AN C N C_N CN

Output

Let there be m m m remaining cards, cards i 1 , i 2 , … , i m i_1, i_2, \dots, i_m i1,i2,,im, in ascending order. Print these in the following format:

$m$
$i_1$ $i_2$ $\cdots$ $i_m$

Sample Input 1

3
2 4
1 1
3 2

Sample Output 1

2
2 3

Focusing on cards 1 1 1 and 3 3 3, we have KaTeX parse error: Expected 'EOF', got '&' at position 5: A_1 &̲lt; A_3 and KaTeX parse error: Expected 'EOF', got '&' at position 5: C_1 &̲gt; C_3, so card 1 1 1 can be discarded.
No further operations can be performed. At this point, cards 2 2 2 and 3 3 3 remain, so print them.

Sample Input 2

5
1 1
10 2
100 3
1000 4
10000 5

Sample Output 2

5
1 2 3 4 5

In this case, no cards can be discarded.

Sample Input 3

6
32 101
65 78
2 29
46 55
103 130
52 40

Sample Output 3

4
2 3 5 6

Solution

具体见文末视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int N = 2e5 + 10;int n;
struct Inf {int a, b, id;bool operator< (const Inf &tmp)const {return a < tmp.a;}
}card[N];signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> n;for (int i = 1; i <= n; i ++)cin >> card[i].a >> card[i].b, card[i].id = i;sort(card + 1, card + 1 + n);set<PII> tmp;for (int i = 1; i <= n; i ++) {while (tmp.size() && (*tmp.rbegin()).fi > card[i].b) tmp.erase(-- tmp.end());tmp.insert({card[i].b, card[i].id});}std::vector<int> res;for (auto v : tmp)res.emplace_back(v.se);sort(res.begin(), res.end());cout << res.size() << endl;for (auto v : res)cout << v << " ";return 0;
}

D - AtCoder Wallpaper

Problem Statement

The pattern of AtCoder’s wallpaper can be represented on the x y xy xy-plane as follows:
The plane is divided by the following three types of lines:
x = n x = n x=n (where n n n is an integer)
y = n y = n y=n (where n n n is an even number)
x + y = n x + y = n x+y=n (where n n n is an even number)
Each region is painted black or white. Any two regions adjacent along one of these lines are painted in different colors.
The region containing ( 0.5 , 0.5 ) (0.5, 0.5) (0.5,0.5) is painted black.
The following figure shows a part of the pattern.

You are given integers A , B , C , D A, B, C, D A,B,C,D. Consider a rectangle whose sides are parallel to the x x x- and y y y-axes, with its bottom-left vertex at ( A , B ) (A, B) (A,B) and its top-right vertex at ( C , D ) (C, D) (C,D). Calculate the area of the regions painted black inside this rectangle, and print twice that area.
It can be proved that the output value will be an integer.

Constraints

− 1 0 9 ≤ A , B , C , D ≤ 1 0 9 -10^9 \leq A, B, C, D \leq 10^9 109A,B,C,D109
KaTeX parse error: Expected 'EOF', got '&' at position 3: A &̲lt; C and KaTeX parse error: Expected 'EOF', got '&' at position 3: B &̲lt; D.
All input values are integers.

Input

The input is given from Standard Input in the following format:

A A A B B B C C C D D D

Output

Print the answer on a single line.

Sample Input 1

0 0 3 3

Sample Output 1

10

We are to find the area of the black-painted region inside the following square:

The area is 5 5 5, so print twice that value: 10 10 10.

Sample Input 2

-1 -2 1 3

Sample Output 2

11

The area is 5.5 5.5 5.5, which is not an integer, but the output value is an integer.

Sample Input 3

-1000000000 -1000000000 1000000000 1000000000

Sample Output 3

4000000000000000000

This is the case with the largest rectangle, where the output still fits into a 64-bit signed integer.

Solution

具体见文末视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int INF = 1e9 + 2;int work(int x, int y) {int res = 0;res += (((y + INF) / 2 + 1) / 2 * 2 + ((y + INF) / 2 - ((y + INF) / 2 + 1) / 2) * 6) * ((x + INF) / 2);if (y & 1) {if (((y + INF) / 2) & 1) res += ((x + INF) / 2) * 3;else res += (x + INF) / 2;}if (x & 1) {res += (((y + INF) / 2 + 1) / 2 + ((y + INF) / 2 - ((y + INF) / 2 + 1) / 2) * 3);}if ((x & 1) && (y & 1)) {if (((y + INF) / 2) & 1) {if ((x & 1) ^ (y & 1)) res += 1;else res += 2;} else {if ((x & 1) ^ (y & 1)) res += 1;}}return res;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int a, b, c, d;cin >> a >> b >> c >> d;swap(a, b), swap(c, d);cout << work(c, d) - work(a, d) - work(c, b) + work(a, b) << endl;return 0;
}

E - Remove Pairs

Problem Statement

Takahashi and Aoki are playing a game using N N N cards. The front side of the i i i-th card has A i A_i Ai written on it, and the back side has B i B_i Bi written on it. Initially, the N N N cards are laid out on the table. With Takahashi going first, the two players take turns performing the following operation:
Choose a pair of cards from the table such that either the numbers on their front sides are the same or the numbers on their back sides are the same, and remove these two cards from the table. If no such pair of cards exists, the player cannot perform the operation.
The player who is first to be unable to perform the operation loses, and the other player wins.
Determine who wins if both players play optimally.

Constraints

1 ≤ N ≤ 18 1 \leq N \leq 18 1N18
1 ≤ A i , B i ≤ 1 0 9 1 \leq A_i, B_i \leq 10^9 1Ai,Bi109
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N
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

Print Takahashi if Takahashi wins when both players play optimally, and Aoki otherwise.

Sample Input 1

5
1 9
2 5
4 9
1 4
2 5

Sample Output 1

Aoki

If Takahashi first removes
the first and third cards: Aoki can win by removing the second and fifth cards.
the first and fourth cards: Aoki can win by removing the second and fifth cards.
the second and fifth cards: Aoki can win by removing the first and third cards.
These are the only three pairs of cards Takahashi can remove in his first move, and Aoki can win in all cases. Therefore, the answer is Aoki.

Sample Input 2

9
3 2
1 7
4 1
1 8
5 2
9 8
2 1
6 8
5 2

Sample Output 2

Takahashi

Solution

具体见文末视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int N = 20;int n;
int a[N], b[N];
int f[1 << N];bool check(int x) {for (int i = 0; i < n; i ++)for (int j = i + 1; j < n; j ++)if ((x >> i & 1) && (x >> j & 1) && (a[i + 1] == a[j + 1] || b[i + 1] == b[j + 1]))return 0;return 1;
}
int rev(int x) {if (x == 1) return 2;else return 1;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> n;for (int i = 1; i <= n; i ++)cin >> a[i] >> b[i];for (int i = 0; i < 1 << n; i ++)if (check(i))f[i] = 2;for (int i = 0; i < 1 << n; i ++) {for (int j = 0; j < n; j ++)for (int k = j + 1; k < n; k ++)if (!(i >> j & 1) && !(i >> k & 1) && (a[j + 1] == a[k + 1] || b[j + 1] == b[k + 1])) {if (!f[i | (1 << j) | (1 << k)]) f[i | (1 << j) | (1 << k)] = rev(f[i]);else if (f[i | (1 << j) | (1 << k)] == 1) continue;else {if (rev(f[i]) == 1) f[i | (1 << j) | (1 << k)] = 1;}}}if (f[(1 << n) - 1] == 1) cout << "Takahashi" << endl;else cout << "Aoki" << endl;return 0;
}

F - Useless for LIS

Problem Statement

You are given an integer sequence A A A of length N N N.
For each t = 1 , 2 , … , N t = 1, 2, \dots, N t=1,2,,N, determine whether A t A_t At is included in a longest increasing subsequence of A A A.
Here, A t A_t At is included in a longest increasing subsequence of A A A if and only if the following holds:
Let L L L be the length of a longest increasing subsequence of A A A. There exists a strictly increasing integer sequence i = ( i 1 , i 2 , … , i L ) ( i 1 < i 2 < ⋯ < i L ) i = (i_1, i_2, \dots, i_L) \ (i_1 < i_2 < \dots < i_L) i=(i1,i2,,iL) (i1<i2<<iL), where each element is between 1 1 1 and N N N, inclusive, that satisfies all of the following conditions:
A i 1 < A i 2 < ⋯ < A i L A_{i_1} < A_{i_2} < \dots < A_{i_L} Ai1<Ai2<<AiL.
i k = t i_k = t ik=t for some k ( 1 ≤ k ≤ L ) k \ (1 \leq k \leq L) k (1kL).
You are given T T T test cases; solve each of them.

What is a longest increasing subsequence? A subsequence of a sequence $A$ is a sequence that can be derived by extracting some elements from $A$ without changing the order. A longest increasing subsequence of a sequence $A$ is a subsequence of $A$ that is strictly increasing with the greatest possible length. ## Constraints

1 ≤ T ≤ 2 × 1 0 5 1 \leq T \leq 2 \times 10^5 1T2×105
1 ≤ N ≤ 2 × 1 0 5 1 \leq N \leq 2 \times 10^5 1N2×105
1 ≤ A i ≤ 1 0 9 1 \leq A_i \leq 10^9 1Ai109
The sum of N N N across all test cases is at most 2 × 1 0 5 2 \times 10^5 2×105.

Input

The input is given from Standard Input in the following format:

T T T
c a s e 1 \mathrm{case}_1 case1
c a s e 2 \mathrm{case}_2 case2
⋮ \vdots
c a s e T \mathrm{case}_T caseT

Here, c a s e i \mathrm{case_i} casei represents the input for the i i i-th case. Each case is given in the following format:

N N N
A 1 A_1 A1 A 2 A_2 A2 ⋯ \cdots A N A_N AN

Output

Print the answers in the following format:

a n s w e r 1 \mathrm{answer}_1 answer1
a n s w e r 2 \mathrm{answer}_2 answer2
⋮ \vdots
a n s w e r T \mathrm{answer}_T answerT

Here, a n s w e r i \mathrm{answer}_i answeri represents the output for the i i i-th case. For each case, let there be m m m indices t t t such that A t A_t At is included in a longest increasing subsequence of A A A, which are i 1 , i 2 , … , i m i_1, i_2, \dots, i_m i1,i2,,im in ascending order. Print these in the following format:

m m m
i 1 i_1 i1 i 2 i_2 i2 ⋯ \cdots i m i_m im

Sample Input 1

1
5
2 1 4 5 3

Sample Output 1

4
1 2 3 4

One of the longest increasing subsequences is ( 2 , 4 , 5 ) (2, 4, 5) (2,4,5), with a length of 3 3 3. Another longest increasing subsequence is ( 1 , 4 , 5 ) (1, 4, 5) (1,4,5). However, no longest increasing subsequence includes A 5 A_5 A5.
Therefore, print 1 , 2 , 3 , 4 1, 2, 3, 4 1,2,3,4.

Sample Input 2

2
6
2 5 3 4 3 4
5
10000 1000 100 1 10

Sample Output 2

5
1 3 4 5 6
2
4 5

Solution

具体见文末视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int N = 2e5 + 10;int n;
int a[N], f[N], g[N];
std::vector<int> dct;
struct fenwick {int tr[N];void add(int x, int d) {for (int i = x; i < N; i += (i & -i)) tr[i] = max(d, tr[i]);}int mx(int x) {int res = 0;if (!x) return res;for (int i = x; i; i -= (i & -i)) res = max(tr[i], res);return res;}
}cnt1, cnt2;int find(int x) {return lower_bound(dct.begin(), dct.end(), x) - dct.begin() + 1;
}void solve() {dct.clear();cin >> n;for (int i = 1; i <= n; i ++)f[i] = g[i] = cnt1.tr[i] = cnt2.tr[i] = 0;for (int i = 1; i <= n; i ++)cin >> a[i], dct.emplace_back(a[i]);sort(dct.begin(), dct.end());dct.erase(unique(dct.begin(), dct.end()), dct.end());for (int i = 1; i <= n; i ++) {f[i] = max(1ll, cnt1.mx(find(a[i]) - 1) + 1);cnt1.add(find(a[i]), f[i]);}for (int i = n; i >= 1; i --) {g[i] = max(1ll, cnt2.mx(dct.size() - find(a[i])) + 1);cnt2.add(dct.size() - find(a[i]) + 1, g[i]);}int ans = 0;for (int i = 1; i <= n; i ++)ans = max(ans, f[i]);std::vector<int> res;for (int i = 1; i <= n; i ++)if (f[i] + g[i] - 1 == ans)res.emplace_back(i);cout << res.size() << endl;for (auto v : res)cout << v << " ";cout << endl;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}

G - Select Strings

Problem Statement

You are given N N N strings S 1 , S 2 , … , S N S_1, S_2, \ldots, S_N S1,S2,,SN consisting of lowercase English letters and N N N positive integers A 1 , A 2 , … , A N A_1, A_2, \ldots, A_N A1,A2,,AN.
A subset T T T of { 1 , 2 , … , N } \lbrace 1, 2, \ldots, N \rbrace {1,2,,N} is called a good set if there is no pair i , j ∈ T ( i ≠ j ) i, j \in T (i \neq j) i,jT(i=j) such that S i S_i Si is a substring of S j S_j Sj.
Find the maximum possible value of ∑ i ∈ T A i \displaystyle \sum_{i \in T} A_i iTAi for a good set T T T.

What is a substring? A substring of a string $S$ is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of $S$. For example, ab is a substring of abc, but ac is not a substring of abc. ## Constraints

1 ≤ N ≤ 100 1 \leq N \leq 100 1N100
S i S_i Si is a string consisting of lowercase English letters.
1 ≤ ∣ S i ∣ 1 \leq |S_i| 1Si
∣ S 1 ∣ + ∣ S 2 ∣ + … + ∣ S N ∣ ≤ 5000 |S_1| + |S_2| + \ldots + |S_N| \leq 5000 S1+S2++SN5000
1 ≤ A i ≤ 1 0 9 1 \leq A_i \leq 10^9 1Ai109

Input

The input is given from Standard Input in the following format:

N N N
S 1 S_1 S1
S 2 S_2 S2
⋮ \vdots
S N S_N SN
A 1 A_1 A1 A 2 A_2 A2 … \ldots A N A_N AN

Output

Print the answer.

Sample Input 1

4
atcoder
at
coder
code
5 2 3 4

Sample Output 1

6

The possible good sets T T T and their corresponding ∑ i ∈ T A i \displaystyle \sum_{i \in T} A_i iTAi are as follows:
T = { 1 } T = \lbrace 1 \rbrace T={1}: ∑ i ∈ T A i = 5 \displaystyle \sum_{i \in T} A_i = 5 iTAi=5
T = { 2 } T = \lbrace 2 \rbrace T={2}: ∑ i ∈ T A i = 2 \displaystyle \sum_{i \in T} A_i = 2 iTAi=2
T = { 3 } T = \lbrace 3 \rbrace T={3}: ∑ i ∈ T A i = 3 \displaystyle \sum_{i \in T} A_i = 3 iTAi=3
T = { 4 } T = \lbrace 4 \rbrace T={4}: ∑ i ∈ T A i = 4 \displaystyle \sum_{i \in T} A_i = 4 iTAi=4
T = { 2 , 3 } T = \lbrace 2, 3 \rbrace T={2,3}: ∑ i ∈ T A i = 5 \displaystyle \sum_{i \in T} A_i = 5 iTAi=5
T = { 2 , 4 } T = \lbrace 2, 4 \rbrace T={2,4}: ∑ i ∈ T A i = 6 \displaystyle \sum_{i \in T} A_i = 6 iTAi=6
The maximum among them is 6 6 6, so print 6 6 6.

Sample Input 2

10
abcd
abc
ab
a
b
c
d
ab
bc
cd
100 10 50 30 60 90 80 70 40 20

Sample Output 2

260

Solution

G题讲解

AtCoder Beginner Contest 354(G 题讲解)


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int N = 2e2 + 10, M = 8e4 + 10, INF = 1e18;int n, s, t;
string S[N];
int a[N], h[N], e[M], ne[M], f[M], idx;
int d[N], cur[N], din[N], dout[N];void add(int a, int b, int c) {e[idx] = b, ne[idx] = h[a], f[idx] = c, h[a] = idx ++;e[idx] = a, ne[idx] = h[b], f[idx] = 0, h[b] = idx ++;
}
int bfs() {memset(d, -1, sizeof d);queue<int> q;q.emplace(s), d[s] = 0, cur[s] = h[s];while (q.size()) {int u = q.front();q.pop();for (int i = h[u]; ~i; i = ne[i]) {int j = e[i];if (d[j] == -1 && f[i]) {d[j] = d[u] + 1, cur[j] = h[j];if (j == t) return 1;q.emplace(j);}}}return 0;
}
int find(int u, int lim) {if (u == t) return lim;int flow = 0;for (int i = cur[u]; ~i && flow < lim; i = ne[i]) {cur[u] = i;int j = e[i];if (d[j] == d[u] + 1 && f[i]) {int tmp = find(j, min(lim - flow, f[i]));if (!tmp) d[j] = -1;f[i] -= tmp, f[i ^ 1] += tmp, flow += tmp;}}return flow;
}
int dinic() {int res = 0, flow;while (bfs()) while (flow = find(s, INF)) res += flow;return res;
}
bool check(string a, string b) {if (a.size() > b.size()) return 0;for (int j = 0; j < b.size() - a.size() + 1; j ++)if (b.substr(j, a.size()) == a)return 1;return 0;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> n;memset(h, -1, sizeof h);s = 0, t = 2 * n + 1;for (int i = 1; i <= n; i ++)cin >> S[i];int sum = 0;for (int i = 1; i <= n; i ++)cin >> a[i], sum += a[i];for (int i = 1; i <= n; i ++)for (int j = 1; j <= n; j ++)if (i != j && check(S[i], S[j]) && (S[i] != S[j] || i < j))add(i, j + n, INF);for (int i = 1; i <= n; i ++)add(s, i, a[i]), add(i + n, t, a[i]);cout << sum - dinic() << endl;return 0;
}

视频题解

以下是A到F题视频讲解

AtCoder Beginner Contest 354(A ~ F 题讲解)


最后祝大家早日在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/15413.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

看一遍就理解:MVCC原理详解

介绍 MVCC&#xff08;Multi-Version Concurrency Control&#xff0c;多版本并发控制&#xff09;是一种用于实现数据库并发访问控制的机制。它允许多个用户同时读写同一数据项&#xff0c;从而提高了数据库在高并发环境下的性能和响应速度。以下是具体介绍&#xff1a; 基本…

Python代码注释的艺术与智慧

新书上架~&#x1f447;全国包邮奥~ python实用小工具开发教程http://pythontoolsteach.com/3 欢迎关注我&#x1f446;&#xff0c;收藏下次不迷路┗|&#xff40;O′|┛ 嗷~~ 目录 一、引言&#xff1a;注释的必要性 二、注释的误区&#xff1a;不是越多越好 过度注释的问题…

服务器端口号怎么看?如何查看服务器端口号呢?有哪些需要注意的?

简单来说&#xff0c;端口号就是计算机与外界通讯交流的出口&#xff0c;每个端口都有不同的编号&#xff0c;也就是“端口号”。它们是唯一的&#xff0c;用于标识不同的服务和应用程序。通过端口号&#xff0c;我们可以知道哪些服务正在运行&#xff0c;以及如何与它们进行通…

【Linux系统编程】进程概念、进程排队、进程标识符、进程状态

目录 什么是进程&#xff1f; 浅谈进程排队 简述进程属性 进程属性之进程标识符 进程操作之进程创建 初识fork fork返回值 原理角度理解fork fork的应用 进程属性之进程状态 再谈进程排队 进程状态 运行状态 阻塞状态 挂起状态 Linux下的进程状态 “R”(运行状…

洗地机哪个牌子最好用?2024洗地机排行榜

随着人们生活水平的提升&#xff0c;智能清洁家电已经成为日常生活中的必需品。如今的清洁家电市场上&#xff0c;洗地机、吸尘器和扫地机器人等设备各有其独特的功能和优势。洗地机结合了扫、拖、吸和自清洁等多种功能&#xff0c;不仅可以处理干湿垃圾&#xff0c;还能高效清…

工业路由器在新能源数字化中的应用:重塑能源行业的未来

随着全球对可再生能源和能源效率的追求日益加强&#xff0c;新能源数字化已成为推动行业发展的关键因素。在这一变革的浪潮中&#xff0c;工业路由器以其卓越的性能和独特的功能&#xff0c;成为新能源数字化不可或缺的核心组件。本文将深入探讨工业路由器在新能源数字化中的应…

使用Golang开发一个用于批量删除文件的命令行程序

核心代码 package cmdimport ("fmt""zdpgo_cobra""zdpgo_file" )func init() {rootCmd.AddCommand(deleteFileCmd)deleteFileCmd.Flags().StringVarP(&dirPath, "dir", "d", ".", "指定要删除的目录&qu…

【C语言】整型提升与char取值范围

整型提升介绍 C语言中整型算术运算总是至少以缺省&#xff08;默认&#xff09;整型类型的精度来进行的。为了获得这个精度&#xff0c;表达式中字符、短整型操作数在使用前被转换为普通整型。而这个过程是悄悄发生的。 整型提升的意义&#xff1a; 表达式的整型运算要在CPU…

C++ wasm 使用教程

环境搭建 git clone https://github.com/emscripten-core/emsdk.gitgit pull./emsdk install latest./emsdk activate latestsource ./emsdk_env.sh./emcc -v && ./emcc c11__Thread_local.c -s WASM_WORKERS --threadprofiler --memoryprofiler -v -o test.html &…

Sentinel的授权规则详解

文章目录 1、授权规则1.1、基本规则1.2、如何获取origin1.3、给网关添加请求头1.4、配置授权规则 2、自定义异常结果2.1、异常类型2.2、自定义异常处理 &#x1f343;作者介绍&#xff1a;双非本科大三网络工程专业在读&#xff0c;阿里云专家博主&#xff0c;专注于Java领域学…

深度学习中的优化算法一(Pytorch 18)

一 优化和深度学习 优化算法 使我们能够 继续更新模型参数&#xff0c; 并使损失函数的值最小化。这就像在训练集上评估一样。事实上&#xff0c;任何满足于将优化视为黑盒装置&#xff0c;以在简 单的设置中最小化目标函数的人&#xff0c;都可能会知道存在着一系列此类“咒…

STM32 学习——2. PWM

这个项目将会不断改变pwm占空比&#xff0c;使用proteus示波器进行观察。 1. proteus8.15 原理图 2. cubemx 上图是配置外部晶振 上图配置在proteus中没啥作用&#xff0c;注意&#xff1a; 在实际开发板中&#xff0c;一定要配置它&#xff0c;不然下一次你写不进代码。 上图配…

番外篇 | YOLOv5-SPD:用最简单的方式完成低分辨率图像和小目标检测升级

前言:Hello大家好,我是小哥谈。论文提出了一个新的CNN构建模块称为SPD-Conv,用来替换每个步长卷转层和每个池化层(从而完全消除它们)。SPD-Conv由一个空间到深度(SPD)层和一个非步长卷积(Conv)层组成。本文详细介绍了如何在YOLOv5中引入SPD-Conv,助力助力低分辨率与小…

自用网站合集

总览 线上工具-图片压缩 TinyPNG线上工具-url参数解析 线上工具-MOV转GIF UI-Vant微信小程序版本其他-敏捷开发工具 Leangoo领歌 工具 线上工具-图片压缩 TinyPNG 不能超过5m&#xff0c;别的没啥缺点 线上工具-url参数解析 我基本上只用url参数解析一些常用的操作在线…

“等保测评与安全运维的协同:保障企业网络安宁

"等保测评与安全运维的协同&#xff1a;保障企业网络安宁"是一个涉及信息安全领域的重要话题。这里&#xff0c;我们可以从几个方面来探讨这个主题。 1. 等保测评&#xff08;等级保护测评&#xff09; 等保测评&#xff0c;即信息安全等级保护测评&#xff0c;是依…

[DDR5 Jedec 3]DDR5 SDRAM 状态图 和 基本功能

3000字, 依公知及经验整理,原创保护,禁止转载。 专栏 《深入理解DDR》 3.1 简化状态图 来源: Jedec Proposed DDR5 Full spec 缩写功能ACT激活PRE预充电PRE_A全部预充电MRS寄存器集模式REF刷新TEN边界扫描模式ReadRD, RDS4, RDS8Read ARDA, RDS4A, RDS8AwriteWR, WRS4…

Ollama:一个在本地部署、运行大型语言模型的工具

Ollama&#xff1a;一个在本地部署、运行大型语言模型的工具 Ollama部署、运行大型语言模型概述安装配置Ollama命令模型库使用示例自定义模型从GGUF导入自定义提示从PyTorch或Safetensors导入 开启服务REST API卸载Ollama One-API概述One-API管理本地模型 Open WebUI概述Docker…

【Linux学习】深入了解Linux中进程状态及其转换

文章目录 进程状态进程排队进程的状态&#xff08;运行&#xff0c;阻塞&#xff0c;挂起&#xff09;进程的七个状态 孤儿进程 进程状态 进程 task_struct 可执行程序 进程不是一 直在运行的&#xff0c;可能在等待软硬件资源&#xff0c;比如scanf后&#xff0c;程序停止运…

邮件系统数据面临的安全问题及解决方法

随着电子邮件的普及&#xff0c;邮件系统已成为企业、学校、个人等用户之间进行信息交流的重要工具。然而&#xff0c;随着数据量的增加和用户对邮件系统的依赖&#xff0c;邮件系统数据安全问题也逐渐凸显。下面U-Mail技术张工就给大家讲解一下邮件系统数据面临的主要安全问题…

Ardupilot开源代码之Rover上路 - 后续3

Ardupilot开源代码之Rover上路 - 后续3 1. 源由2. 深度配置2.1 编码器2.2 WS2812B LED灯带2.3 4GLTE超视距2.3.1 摄像头2.3.2 QGC OSD虚拟遥控2.3.3 QGC外接JoyStick遥控 2.4 伴机电脑供电 3. 实测效果4. 遗留&后续4.1 设置倒车按钮4.2 MP无法连接ESP82664.3 高精度编码器问…