AtCoder Beginner Contest 364(ABCDEF题)视频讲解

A - Glutton Takahashi

Problem Statement

Takahashi is planning to eat N N N dishes.

The i i i-th dish he plans to eat is sweet if S i = S_i = Si= sweet, and salty if S i = S_i = Si= salty.

If he eats two sweet dishes consecutively, he will feel sick and be unable to eat any more dishes.

Determine whether he can eat all the dishes.

Constraints

  • N N N is an integer between 1 1 1 and 100 100 100, inclusive.
  • Each S i S_i Si is sweet or salty.

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

Output

Print Yes if Takahashi can eat all the dishes, and No otherwise.

Sample Input 1

5
salty
sweet
salty
salty
sweet

Sample Output 1

Yes

He will not eat two sweet dishes consecutively, so he can eat all the dishes without feeling sick.

Sample Input 2

4
sweet
salty
sweet
sweet

Sample Output 2

Yes

He will feel sick but can still eat all the dishes.

Sample Input 3

6
salty
sweet
sweet
salty
sweet
sweet

Sample Output 3

No

He feels sick when eating the 3rd dish and cannot eat the 4th and subsequent dishes.

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);

	string s;
	int n;
	cin >> n;

	string lst;
	for (int i = 1; i <= n; i ++) {
		cin >> s;
		if (i != 1 && i != n && s == "sweet" && lst == "sweet") {
			cout << "No" << endl;
			return 0;
		}
		lst = s;
	}

	cout << "Yes" << endl;

	return 0;
}

B - Grid Walk

Problem Statement

There is a grid with H H H rows and W W W columns. Let ( i , j ) (i, j) (i,j) denote the cell at the i i i-th row from the top and j j j-th column from the left.

Cell ( i , j ) (i, j) (i,j) is empty if C i , j C_{i, j} Ci,j is ., and not empty if C i , j C_{i, j} Ci,j is #.

Takahashi is currently at cell ( S i , S j ) (S_i, S_j) (Si,Sj), and he will act according to the following rules for i = 1 , 2 , … , ∣ X ∣ i = 1, 2, \ldots, |X| i=1,2,,X in order.

  • If the i i i-th character of X X X is L, and the cell to the left of his current cell exists and is empty, he moves to the cell to the left. Otherwise, he stays in the current cell.
  • If the i i i-th character of X X X is R, and the cell to the right of his current cell exists and is empty, he moves to the cell to the right. Otherwise, he stays in the current cell.
  • If the i i i-th character of X X X is U, and the cell above his current cell exists and is empty, he moves to the cell above. Otherwise, he stays in the current cell.
  • If the i i i-th character of X X X is D, and the cell below his current cell exists and is empty, he moves to the cell below. Otherwise, he stays in the current cell.

Print the cell where he is after completing the series of actions.

Constraints

  • 1 ≤ H , W ≤ 50 1 \leq H, W \leq 50 1H,W50
  • 1 ≤ S i ≤ H 1 \leq S_i \leq H 1SiH
  • 1 ≤ S j ≤ W 1 \leq S_j \leq W 1SjW
  • H , W , S i , S j H, W, S_i, S_j H,W,Si,Sj are integers.
  • C i , j C_{i, j} Ci,j is . or #.
  • C S i , S j = C_{S_i, S_j} = CSi,Sj= .
  • X X X is a string of length between 1 1 1 and 50 50 50, inclusive, consisting of L, R, U, D.

Input

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

H H H W W W
S i S_i Si S j S_j Sj
C 1 , 1 C_{1, 1} C1,1 C 1 , 2 C_{1, 2} C1,2 … \ldots C 1 , W C_{1, W} C1,W
C 2 , 1 C_{2, 1} C2,1 C 2 , 2 C_{2, 2} C2,2 … \ldots C 2 , W C_{2, W} C2,W
⋮ \vdots
C H , 1 C_{H, 1} CH,1 C H , 2 C_{H, 2} CH,2 … \ldots C H , W C_{H, W} CH,W
X X X

Output

Let ( x , y ) (x, y) (x,y) be the cell where Takahashi is after completing the series of actions. Print x x x and y y y, separated by a space.

Sample Input 1

2 3
2 1
.#.
...
ULDRU

Sample Output 1

2 2

Takahashi starts at cell ( 2 , 1 ) (2, 1) (2,1). His series of actions are as follows:

  • The 1st character of X X X is U, and the cell above ( 2 , 1 ) (2, 1) (2,1) exists and is an empty cell, so he moves to the cell above, which is ( 1 , 1 ) (1, 1) (1,1).
  • The 2nd character of X X X is L, and the cell to the left of ( 1 , 1 ) (1, 1) (1,1) does not exist, so he stays at ( 1 , 1 ) (1, 1) (1,1).
  • The 3rd character of X X X is D, and the cell below ( 1 , 1 ) (1, 1) (1,1) exists and is an empty cell, so he moves to the cell below, which is ( 2 , 1 ) (2, 1) (2,1).
  • The 4th character of X X X is R, and the cell to the right of ( 2 , 1 ) (2, 1) (2,1) exists and is an empty cell, so he moves to the cell to the right, which is ( 2 , 2 ) (2, 2) (2,2).
  • The 5th character of X X X is U, and the cell above ( 2 , 2 ) (2, 2) (2,2) exists but is not an empty cell, so he stays at ( 2 , 2 ) (2, 2) (2,2).

Therefore, after completing the series of actions, he is at cell ( 2 , 2 ) (2, 2) (2,2).

Sample Input 2

4 4
4 2
....
.#..
...#
....
DUUUURULRD

Sample Output 2

2 4

Sample Input 3

6 6
1 1
.#####
######
######
######
######
######
RURLDLULLRULRDL

Sample Output 3

1 1

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 = 55;

int n, m, k, x, y;
char g[N][N];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	cin >> n >> m >> x >> y;
	for (int i = 1; i <= n; i ++) for (int j = 1; j <= m; j ++) cin >> g[i][j];
	string s;
	cin >> s, k = s.size(), s = ' ' + s;

	unordered_map<char, int> sd;
	sd['R'] = 0, sd['D'] = 1, sd['U'] = 3, sd['L'] = 2;
	for (int i = 1; i <= k; i ++) {
		int xx = x + dx[sd[s[i]]], yy = y + dy[sd[s[i]]];
		if (xx < 1 || yy < 1 || xx > n || yy > m || g[xx][yy] == '#') continue;
		x = xx, y = yy;
	}
	cout << x << " " << y << endl;

	return 0;
}

C - Minimum Glutton

Problem Statement

There are N N N dishes, and the i i i-th dish has a sweetness of A i A_i Ai and a saltiness of B i B_i Bi.

Takahashi plans to arrange these N N N dishes in any order he likes and eat them in that order.

He will eat the dishes in the arranged order, but he will stop eating as soon as the total sweetness of the dishes he has eaten exceeds X X X or the total saltiness exceeds Y Y Y.

Find the minimum possible number of dishes that he will end up eating.

Constraints

  • 1 ≤ N ≤ 2 × 1 0 5 1 \leq N \leq 2 \times 10^5 1N2×105
  • 1 ≤ X , Y ≤ 2 × 1 0 14 1 \leq X, Y \leq 2 \times 10^{14} 1X,Y2×1014
  • 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 X X X Y Y Y
A 1 A_1 A1 A 2 A_2 A2 … \ldots A N A_N AN
B 1 B_1 B1 B 2 B_2 B2 … \ldots B N B_N BN

Output

Print the answer.

Sample Input 1

4 7 18
2 3 5 1
8 8 1 4

Sample Output 1

2

The i i i-th dish will be denoted as dish i i i.

If he arranges the four dishes in the order 2 , 3 , 1 , 4 2, 3, 1, 4 2,3,1,4, as soon as he eats dishes 2 2 2 and 3 3 3, their total sweetness is 8 8 8, which is greater than 7 7 7. Therefore, in this case, he will end up eating two dishes.

The number of dishes he will eat cannot be 1 1 1 or less, so print 2 2 2.

Sample Input 2

5 200000000000000 200000000000000
1 1 1 1 1
2 2 2 2 2

Sample Output 2

5

Sample Input 3

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

Sample Output 3

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;

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int n, x, y;
	cin >> n >> x >> y;

	std::vector<int> a(n + 1), b(n + 1);
	for (int i = 1; i <= n; i ++) cin >> a[i];
	for (int i = 1; i <= n; i ++) cin >> b[i];
	sort(a.begin() + 1, a.end(), greater<int>());
	sort(b.begin() + 1, b.end(), greater<int>());

	int res = n, tot = 0;
	for (int i = 1; i <= n; i ++) {
		tot += a[i];
		if (tot > x) res = min(res, i);
	}
	tot = 0;
	for (int i = 1; i <= n; i ++) {
		tot += b[i];
		if (tot > y) res = min(res, i);
	}

	cout << res << endl;

	return 0;
}

D - K-th Nearest

Problem Statement

There are N + Q N+Q N+Q points A 1 , … , A N , B 1 , … , B Q A_1,\dots,A_N,B_1,\dots,B_Q A1,,AN,B1,,BQ on a number line, where point A i A_i Ai has a coordinate a i a_i ai and point B j B_j Bj has a coordinate b j b_j bj.

For each j = 1 , 2 , … , Q j=1,2,\dots,Q j=1,2,,Q, answer the following question:

  • Let X X X be the point among A 1 , A 2 , … , A N A_1,A_2,\dots,A_N A1,A2,,AN that is the k j k_j kj-th closest to point B j B_j Bj. Find the distance between points X X X and B j B_j Bj. More formally, let d i d_i di be the distance between points A i A_i Ai and B j B_j Bj. Sort ( d 1 , d 2 , … , d N ) (d_1,d_2,\dots,d_N) (d1,d2,,dN) in ascending order to get the sequence ( d 1 ′ , d 2 ′ , … , d N ′ ) (d_1',d_2',\dots,d_N') (d1,d2,,dN). Find d k j ′ d_{k_j}' dkj.

Constraints

  • 1 ≤ N , Q ≤ 1 0 5 1 \leq N, Q \leq 10^5 1N,Q105
  • − 1 0 8 ≤ a i , b j ≤ 1 0 8 -10^8 \leq a_i, b_j \leq 10^8 108ai,bj108
  • 1 ≤ k j ≤ N 1 \leq k_j \leq N 1kjN
  • All input values are integers.

Input

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

N N N Q Q Q
a 1 a_1 a1 a 2 a_2 a2 … \dots a N a_N aN
b 1 b_1 b1 k 1 k_1 k1
b 2 b_2 b2 k 2 k_2 k2
⋮ \vdots
b Q b_Q bQ k Q k_Q kQ

Output

Print Q Q Q lines. The l l l-th line ( 1 ≤ l ≤ Q ) (1 \leq l \leq Q) (1lQ) should contain the answer to the question for j = l j=l j=l as an integer.

Sample Input 1

4 3
-3 -1 5 6
-2 3
2 1
10 4

Sample Output 1

7
3
13

Let us explain the first query.

The distances from points A 1 , A 2 , A 3 , A 4 A_1, A_2, A_3, A_4 A1,A2,A3,A4 to point B 1 B_1 B1 are 1 , 1 , 7 , 8 1, 1, 7, 8 1,1,7,8, respectively, so the 3rd closest to point B 1 B_1 B1 is point A 3 A_3 A3. Therefore, print the distance between point A 3 A_3 A3 and point B 1 B_1 B1, which is 7 7 7.

Sample Input 2

2 2
0 0
0 1
0 2

Sample Output 2

0
0

There may be multiple points with the same coordinates.

Sample Input 3

10 5
-84 -60 -41 -100 8 -8 -52 -62 -61 -76
-52 5
14 4
-2 6
46 2
26 7

Sample Output 3

11
66
59
54
88

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 = 1e5 + 10;

int n, q;
int a[N];

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	cin >> n >> q;
	for (int i = 1; i <= n; i ++) cin >> a[i];
	sort(a + 1, a + 1 + n);

	while (q -- ) {
		int x, k;
		cin >> x >> k;
		auto get = [&](int d) -> int {
			int lo = lower_bound(a + 1, a + 1 + n, x - d) - a, ro = upper_bound(a + 1, a + 1 + n, x + d) - a - 1;
			return ro - lo + 1;
		};
		int l = 0, r = 3e8, res;
		while (l <= r) {
			int mid = l + r >> 1;
			if (get(mid) >= k) r = mid - 1, res = mid;
			else l = mid + 1;
		}
		cout << res << endl;
	}

	return 0;
}

E - Maximum Glutton

Problem Statement

Takahashi has prepared N N N dishes for Snuke. The dishes are numbered from 1 1 1 to N N N, and dish i i i has a sweetness of A i A_i Ai and a saltiness of B i B_i Bi.

Takahashi can arrange these dishes in any order he likes. Snuke will eat the dishes in the order they are arranged, but if at any point the total sweetness of the dishes he has eaten so far exceeds X X X or the total saltiness exceeds Y Y Y, he will not eat any further dishes.

Takahashi wants Snuke to eat as many dishes as possible. Find the maximum number of dishes Snuke will eat if Takahashi arranges the dishes optimally.

Constraints

  • 1 ≤ N ≤ 80 1 \leq N \leq 80 1N80
  • 1 ≤ A i , B i ≤ 10000 1 \leq A_i, B_i \leq 10000 1Ai,Bi10000
  • 1 ≤ X , Y ≤ 10000 1 \leq X, Y \leq 10000 1X,Y10000
  • All input values are integers.

Input

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

N N N X X X Y Y Y
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 the answer as an integer.

Sample Input 1

4 8 4
1 5
3 2
4 1
5 3

Sample Output 1

3

Consider the scenario where Takahashi arranges the dishes in the order 2 , 3 , 1 , 4 2, 3, 1, 4 2,3,1,4.

  • First, Snuke eats dish 2 2 2. The total sweetness so far is 3 3 3, and the total saltiness is 2 2 2.
  • Next, Snuke eats dish 3 3 3. The total sweetness so far is 7 7 7, and the total saltiness is 3 3 3.
  • Next, Snuke eats dish 1 1 1. The total sweetness so far is 8 8 8, and the total saltiness is 8 8 8.
  • The total saltiness has exceeded Y = 4 Y=4 Y=4, so Snuke will not eat any further dishes.

Thus, in this arrangement, Snuke will eat three dishes.

No matter how Takahashi arranges the dishes, Snuke will not eat all four dishes, so the answer is 3 3 3.

Sample Input 2

2 1 1
3 2
3 2

Sample Output 2

1

Sample Input 3

2 100 100
3 2
3 2

Sample Output 3

2

Sample Input 4

6 364 463
230 381
154 200
328 407
339 94
193 10
115 309

Sample Output 4

3

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 = 85, M = 1e4 + 10;

int n, x, y;
int a[N], b[N], dp[N][N][M];

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	cin >> n >> x >> y;
	for (int i = 1; i <= n; i ++) cin >> a[i] >> b[i];

	memset(dp, 0x3f, sizeof dp);
	dp[0][0][0] = 0;
	for (int i = 1; i <= n; i ++)
		for (int j = 0; j <= i; j ++)
			for (int k = 0; k <= x; k ++) {
				dp[i][j][k] = min(dp[i - 1][j][k], dp[i][j][k]);
				if (j && k >= a[i]) dp[i][j][k] = min(dp[i - 1][j - 1][k - a[i]] + b[i], dp[i][j][k]);
			}

	int res = 0;
	for (int i = 0; i <= n; i ++)
		for (int j = 0; j <= x; j ++)
			if (dp[n][i][j] <= y) res = max(res, min(i + 1, n));

	cout << res << endl;

	return 0;
}

F - Range Connect MST

Problem Statement

There is a graph with N + Q N + Q N+Q vertices, numbered 1 , 2 , … , N + Q 1, 2, \ldots, N + Q 1,2,,N+Q. Initially, the graph has no edges.

For this graph, perform the following operation for i = 1 , 2 , … , Q i = 1, 2, \ldots, Q i=1,2,,Q in order:

  • For each integer j j j satisfying L i ≤ j ≤ R i L_i \leq j \leq R_i LijRi, add an undirected edge with cost C i C_i Ci between vertices N + i N + i N+i and j j j.

Determine if the graph is connected after all operations are completed. If it is connected, find the cost of a minimum spanning tree of the graph.

A minimum spanning tree is a spanning tree with the smallest possible cost, and the cost of a spanning tree is the sum of the costs of the edges used in the spanning tree.

Constraints

  • 1 ≤ N , Q ≤ 2 × 1 0 5 1 \leq N, Q \leq 2 \times 10^5 1N,Q2×105
  • 1 ≤ L i ≤ R i ≤ N 1 \leq L_i \leq R_i \leq N 1LiRiN
  • 1 ≤ C i ≤ 1 0 9 1 \leq C_i \leq 10^9 1Ci109
  • All input values are integers.

Input

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

N N N Q Q Q
L 1 L_1 L1 R 1 R_1 R1 C 1 C_1 C1
L 2 L_2 L2 R 2 R_2 R2 C 2 C_2 C2
⋮ \vdots
L Q L_Q LQ R Q R_Q RQ C Q C_Q CQ

Output

If the graph is connected, print the cost of a minimum spanning tree. Otherwise, print − 1 -1 1.

Sample Input 1

4 3
1 2 2
1 3 4
2 4 5

Sample Output 1

22

The following edges form a minimum spanning tree:

  • An edge with cost 2 2 2 connecting vertices 1 1 1 and 5 5 5
  • An edge with cost 2 2 2 connecting vertices 2 2 2 and 5 5 5
  • An edge with cost 4 4 4 connecting vertices 1 1 1 and 6 6 6
  • An edge with cost 4 4 4 connecting vertices 3 3 3 and 6 6 6
  • An edge with cost 5 5 5 connecting vertices 3 3 3 and 7 7 7
  • An edge with cost 5 5 5 connecting vertices 4 4 4 and 7 7 7

Since 2 + 2 + 4 + 4 + 5 + 5 = 22 2 + 2 + 4 + 4 + 5 + 5 = 22 2+2+4+4+5+5=22, print 22 22 22.

Sample Input 2

6 2
1 2 10
4 6 10

Sample Output 2

\-1

The graph is disconnected.

Sample Input 3

200000 4
1 200000 1000000000
1 200000 998244353
1 200000 999999999
1 200000 999999999

Sample Output 3

199651870599998

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, q;
array<int, 3> sgt[N];
int par[N];

int find(int x) {
	if (par[x] != x) par[x] = find(par[x]);
	return par[x];
}

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	cin >> n >> q;
	for (int i = 1; i <= q; i ++) cin >> sgt[i][0] >> sgt[i][1] >> sgt[i][2];
	sort(sgt + 1, sgt + 1 + q, [&](auto a, auto b) { return a[2] < b[2]; });

	for (int i = 1; i <= n; i ++) par[i] = i;
	int ans = 0, cnt = n;
	for (int i = 1; i <= q; i ++) {
		int lst = -1;
		ans += sgt[i][2];
		for (int j = find(sgt[i][0]) + 1; j <= sgt[i][1]; j = find(j) + 1) {
			par[j - 1] = j, cnt --, ans += sgt[i][2];
			lst = j;
		}
	}
	
	if (cnt != 1) cout << -1 << endl;
	else cout << ans << endl;

	return 0;
}

视频题解

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


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

  • 20
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值