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) (0≤i).

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} 1≤H≤109

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

cpp 复制代码
#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,...,N−1 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 1≤N≤100
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 1≤Ci≤4229
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

cpp 复制代码
#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 2≤N≤2×105
1 ≤ A i , C i ≤ 1 0 9 1 \leq A_i, C_i \leq 10^9 1≤Ai,Ci≤109
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

cpp 复制代码
#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 −109≤A,B,C,D≤109
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

cpp 复制代码
#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 1≤N≤18
1 ≤ A i , B i ≤ 1 0 9 1 \leq A_i, B_i \leq 10^9 1≤Ai,Bi≤109

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

cpp 复制代码
#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 (1≤k≤L).

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 1≤T≤2×105
1 ≤ N ≤ 2 × 1 0 5 1 \leq N \leq 2 \times 10^5 1≤N≤2×105
1 ≤ A i ≤ 1 0 9 1 \leq A_i \leq 10^9 1≤Ai≤109

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

cpp 复制代码
#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,j∈T(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 i∈T∑Ai 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 1≤N≤100
S i S_i Si is a string consisting of lowercase English letters.
1 ≤ ∣ S i ∣ 1 \leq |S_i| 1≤∣Si∣
∣ S 1 ∣ + ∣ S 2 ∣ + ... + ∣ S N ∣ ≤ 5000 |S_1| + |S_2| + \ldots + |S_N| \leq 5000 ∣S1∣+∣S2∣+...+∣SN∣≤5000
1 ≤ A i ≤ 1 0 9 1 \leq A_i \leq 10^9 1≤Ai≤109

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 i∈T∑Ai are as follows:
T = { 1 } T = \lbrace 1 \rbrace T={1}: ∑ i ∈ T A i = 5 \displaystyle \sum_{i \in T} A_i = 5 i∈T∑Ai=5
T = { 2 } T = \lbrace 2 \rbrace T={2}: ∑ i ∈ T A i = 2 \displaystyle \sum_{i \in T} A_i = 2 i∈T∑Ai=2
T = { 3 } T = \lbrace 3 \rbrace T={3}: ∑ i ∈ T A i = 3 \displaystyle \sum_{i \in T} A_i = 3 i∈T∑Ai=3
T = { 4 } T = \lbrace 4 \rbrace T={4}: ∑ i ∈ T A i = 4 \displaystyle \sum_{i \in T} A_i = 4 i∈T∑Ai=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 i∈T∑Ai=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 i∈T∑Ai=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

cpp 复制代码
#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 题讲解)


最后祝大家早日

相关推荐
捕鲸叉11 分钟前
怎样在软件设计中选择使用GOF设计模式
c++·设计模式
捕鲸叉15 分钟前
C++设计模式和编程框架两种设计元素的比较与相互关系
开发语言·c++·设计模式
龙的爹233317 分钟前
论文翻译 | The Capacity for Moral Self-Correction in Large Language Models
人工智能·深度学习·算法·机器学习·语言模型·自然语言处理·prompt
鸣弦artha1 小时前
蓝桥杯——杨辉三角
java·算法·蓝桥杯·eclipse
我是聪明的懒大王懒洋洋1 小时前
力扣力扣力:动态规划入门(1)
算法·leetcode·动态规划
未知陨落1 小时前
数据结构——二叉搜索树
开发语言·数据结构·c++·二叉搜索树
丶Darling.1 小时前
Day44 | 动态规划 :状态机DP 买卖股票的最佳时机IV&&买卖股票的最佳时机III
算法·动态规划
一丝晨光2 小时前
gcc 1.c和g++ 1.c编译阶段有什么区别?如何知道g++编译默认会定义_GNU_SOURCE?
c语言·开发语言·c++·gnu·clang·gcc·g++
TN_stark9322 小时前
多进程/线程并发服务器
服务器·算法·php
汉克老师2 小时前
GESP4级考试语法知识(贪心算法(四))
开发语言·c++·算法·贪心算法·图论·1024程序员节