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

news2024/11/19 8:38:06

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 long

using 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 long

using 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 long

using 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 long

using 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 long

using 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 long

using 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 long

using 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.coloradmin.cn/o/1692215.html

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

相关文章

AWS迁移与传输之AMS/MGN

AWS Application Migration Service&#xff08;AWS Application Migration Service简称为AWS MGN&#xff0c;MGN是migration的缩写。&#xff09;是一项全面的迁移服务&#xff0c;旨在帮助企业将其本地服务器和虚拟机迁移到云端&#xff0c;包括AWS和VMware Cloud on AWS。 …

Bugku Crypto 部分题目简单题解(四)

目录 python_jail 简单的rsa 托马斯.杰斐逊 这不是md5 进制转换 affine Crack it rsa python_jail 启动场景 使用虚拟机nc进行连接 输入print(flag) 发现报错&#xff0c;经过测试只能传入10个字符多了就会报错 利用python中help()函数&#xff0c;借报错信息带出flag变…

海康威视NVR通过ehome协议接入视频监控平台,视频浏览显示3011超时错误的问题解决,即:The request timeout! 【3011】

目录 一、问题描述 二、问题分析 2.1 初步分析 2.2 查看日志 2.3 问题验证 1、查看防火墙 2、查看安全组 3、问题原因 三、问题解决 3.1 防火墙开放相关端口 3.2 安全组增加规则 3.3 测试 1、TCP端口能够联通的情况 2、TCP端口不能够联通的情况 四、验证 五、云…

.DS_store文件

感觉mac里的这个.DS_store文件烦人&#xff0c;老是莫名其妙的出现&#xff0c;然后造成困扰 处理方式如下&#xff1a; import os pic_list os.listdir("./mask_pic/") print(len(pic_list)) # 从文件夹中删掉 if(".DS_Store" in pic_list):print(&quo…

orin部署tensorrt、cuda、cudnn、pytorch

绝大部分参考https://blog.csdn.net/qq_41336087/article/details/129661850 非orin可以参考https://blog.csdn.net/JineD/article/details/131201121 报错显卡驱动安装535没法安装、原始是和l4t-cuda的部分文件冲突 Options marked [*] produce a lot of output - pipe it th…

webstorm新建vue项目相关问题

前言 这个迭代后端需求偏少&#xff0c;前端code的键盘都起火星子了。来了4个外包支持&#xff0c;1个后端3个前端&#xff0c;还是不够用啊。刚好趁这个机会稍微学习下vue&#xff0c;其实之前环境也配置过了&#xff0c;所以这里就不分享环境配置了&#xff0c;主要分享下新建…

R可视化:可发表的Y轴截断图

Y轴截断图by ggprism Y轴截断图by ggprism 介绍 ggplot2绘制Y轴截断图by ggprism加载R包 knitr::opts_chunk$set(message = FALSE, warning = FALSE)library(tidyverse) library(ggprism) library(patchwork)rm(list = ls()) options(stringsAsFactors = F) options(future.…

【全开源】分类记账小程序系统源码(ThinkPHP+FastAdmin+UniApp)

基于ThinkPHPFastAdminUniAppvk-uView-uiVue3.0开发的一款支持多人协作的记账本小程序&#xff0c;可用于家庭&#xff0c;团队&#xff0c;组织以及个人的日常收支情况记录&#xff0c;支持周月年度统计。 &#xff1a;智能管理您的财务生活 一、引言&#xff1a;财务智能化…

云计算中的弹性计算是什么?

弹性计算在管理云计算中的云服务器方面起着举足轻重的作用。顾名思义&#xff0c;弹性计算服务为云服务提供商提供了扩展和缩减计算资源&#xff08;如内存、带宽、基础架构等&#xff09;的能力。此功能可使用监控自动快速扩展资源以满足业务的不同需求工具。 本文 德迅云安全…

十五、Python模块(入门一定看!!!)「长期更新Python简单入门到适用」

首先什么是模块&#xff1f; 小伙伴们经常看我写的教程不难发现&#xff0c;前面我们用过几次模块就是sys的那个&#xff0c;其实python不仅标准库中包含了大量的模块&#xff08;也被称之为准模块&#xff09;&#xff0c;还有大量的第三方模块&#xff0c;开发者也可以自己发…

集创北方ICN6211 MIPIDSI桥接到RGB,支持RGB565/RGB888/RGB666

ICN6211描述&#xff1a; ICN6211是一个桥接芯片&#xff0c;它接收MIPIDSI输入并发送RGB输出。MIPIDSI最多支持4个车道&#xff0c;每个车道的最大运行频率为1Gbps&#xff1b;总最大输入带宽为4Gbps&#xff1b;并且还支持MIPI定义的ULPS&#xff08;超低功耗状态&#xff0…

算法课程笔记——矩阵乘法整除同余LCMGCD

算法课程笔记——矩阵乘法&整除&同余&LCM&GCD bool相等 不需要库函数 只有除法不是 本身就很大&#xff0c;如果不行就要考虑其他方法

vue3.0+ts+vite+scss创建一个vue项目

使用Vite创建一个vue3项目 文章目录 1、创建vue3项目2、安装less/scss3、自动导入 1、创建vue3项目 npm ​npm create vitelatestyarn ​yarn create vite输入新项目名字&#xff08;例子&#xff1a;vueDemo&#xff09; 按上下选择框架&#xff0c;vue按回车 ​​​​ 选…

Java进阶学习笔记5——Static应用知识:单例设计模式

设计模式&#xff1a; 架构师会使用到设计模式&#xff0c;开发框架&#xff0c;就需要掌握很多设计模式。 在Java基础阶段学习设计模式&#xff0c;将来面试笔试的时候&#xff0c;笔试题目会经常靠到设计模式。 将来会用到设计模式。框架代码中会用到设计模式。 什么是设计…

【Flutter】AspectRatio组件Card组件按钮组件Wrap组件

&#x1f525; 本文由 程序喵正在路上 原创&#xff0c;CSDN首发&#xff01; &#x1f496; 系列专栏&#xff1a;Flutter学习 &#x1f320; 首发时间&#xff1a;2024年5月25日 &#x1f98b; 欢迎关注&#x1f5b1;点赞&#x1f44d;收藏&#x1f31f;留言&#x1f43e; 目…

开源内网穿透神器:中微子代理(neutrino-proxy)实现内网穿刺

&#x1f604; 19年之后由于某些原因断更了三年&#xff0c;23年重新扬帆起航&#xff0c;推出更多优质博文&#xff0c;希望大家多多支持&#xff5e; &#x1f337; 古之立大事者&#xff0c;不惟有超世之才&#xff0c;亦必有坚忍不拔之志 &#x1f390; 个人CSND主页——Mi…

go select 原理

编译器会使用如下的流程处理 select 语句&#xff1a; 将所有的 case 转换成包含 channel 以及类型等信息的 runtime.scase 结构体。调用运行时函数 runtime.selectgo 从多个准备就绪的 channel 中选择一个可执行的 runtime.scase 结构体。通过 for 循环生成一组 if 语句&…

计算机网络——TCP 协议的三次握手 / 四次挥手

简述 TCP / UDP 协议都是传输层的协议。 UDP 是面向无连接的协议&#xff0c;就是说发送端不在乎消息数据是否传输到接收端了&#xff0c;所以会出现数据丢失的情况&#xff0c;所以可靠性也不高。 TCP 是面向连接的、可靠的、基于字节流的传输层协议。所谓面向连接的&#…

win11 wsl ubuntu24.04

win11 wsl ubuntu24.04 一&#xff1a;开启Hyper-V二&#xff1a;安装wsl三&#xff1a;安装ubuntu24.04三&#xff1a;桥接模式&#xff0c;固定IP四&#xff1a;U盘使用五&#xff1a;wsl 从c盘迁移到其它盘参考资料 一&#xff1a;开启Hyper-V win11家庭版开启hyper-v 桌面…

【Crypto】RSA

文章目录 题目步骤1.计算 &#x1d45b;2.计算欧拉函数 &#x1d719;(&#x1d45b;)3. 扩展欧几里得算法求逆元 &#x1d451; 解题感悟 题目 p473398607161 q4511491 e17 求d 步骤 1.计算 &#x1d45b; 公式&#xff1a;npq n47339860716145114912135733555619387051 …