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

A - delete .

Problem Statement

You are given a string S S S consisting of lowercase English letters and ..
Find the string obtained by removing all . from S S S.

Constraints

S S S is a string of length between 1 1 1 and 100 100 100, inclusive, consisting of lowercase English letters and ..

Input

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

S S S

Output

Print the string obtained by removing all . from S S S.

Sample Input 1
.v.
Sample Output 1
v

Removing all . from .v. yields v, so print v.

Sample Input 2
chokudai
Sample Output 2
chokudai

There are cases where S S S does not contain ..

Sample Input 3
...
Sample Output 3

There are also cases where all characters in S S S are ..

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;

string s;

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

	cin >> s;

	for (auto v : s)
		if (v != '.') cout << v;
	cout << endl;

	return 0;
}

B - 3^A

Problem Statement

You are given a positive integer M M M.
Find a positive integer N N N and a sequence of non-negative integers A = ( A 1 , A 2 , … , A N ) A = (A_1, A_2, \ldots, A_N) A=(A1,A2,,AN) that satisfy all of the following conditions:
1 ≤ N ≤ 20 1 \le N \le 20 1N20
0 ≤ A i ≤ 10 0 \le A_i \le 10 0Ai10 ( 1 ≤ i ≤ N ) (1 \le i \le N) (1iN)
∑ i = 1 N 3 A i = M \displaystyle \sum_{i=1}^N 3^{A_i} = M i=1N3Ai=M
It can be proved that under the constraints, there always exists at least one such pair of N N N and A A A satisfying the conditions.

Constraints

1 ≤ M ≤ 1 0 5 1 \le M \le 10^5 1M105

Input

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

$M$
Output

Print N N N and A A A satisfying the conditions in the following format:

$N$
$A_1$ $A_2$ $\ldots$ $A_N$

If there are multiple valid pairs of N N N and A A A, any of them is acceptable.

Sample Input 1
6
Sample Output 1
2
1 1

For example, with N = 2 N=2 N=2 and A = ( 1 , 1 ) A=(1,1) A=(1,1), we have ∑ i = 1 N 3 A i = 3 + 3 = 6 \displaystyle \sum_{i=1}^N 3^{A_i} = 3+3=6 i=1N3Ai=3+3=6, satisfying all conditions.
Another example is N = 4 N=4 N=4 and A = ( 0 , 0 , 1 , 0 ) A=(0,0,1,0) A=(0,0,1,0), which also satisfies the conditions.

Sample Input 2
100
Sample Output 2
4
2 0 2 4
Sample Input 3
59048
Sample Output 3
20
0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

Note the condition 1 ≤ N ≤ 20 1 \le N \le 20 1N20.

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<int> bit;
	while (n) {
		bit.push_back(n % 3);
		n /= 3;
	}
	std::vector<int> res;
	for (int i = bit.size() - 1; i >= 0; i --)
		for (int j = bit[i]; j >= 1; j --) res.push_back(i);

	cout << res.size() << endl;
	for (auto v : res)
		cout << v << " ";
	cout << endl;

	return 0;
}

C - Count ABC Again

Problem Statement

You are given a string S S S of length N N N. You are also given Q Q Q queries, which you should process in order.
The i i i-th query is as follows:
Given an integer X i X_i Xi and a character C i C_i Ci, replace the X i X_i Xi-th character of S S S with C i C_i Ci. Then, print the number of times the string ABC appears as a substring in S S S.
Here, a substring of S S S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S S S.

For example, ab is a substring of abc, but ac is not a substring of abc.

Constraints

3 ≤ N ≤ 2 × 1 0 5 3 \le N \le 2 \times 10^5 3N2×105
1 ≤ Q ≤ 2 × 1 0 5 1 \le Q \le 2 \times 10^5 1Q2×105
S S S is a string of length N N N consisting of uppercase English letters.
1 ≤ X i ≤ N 1 \le X_i \le N 1XiN
C i C_i Ci is an uppercase English letter.

Input

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

$N$ $Q$
$S$
$X_1$ $C_1$
$X_2$ $C_2$
$\vdots$
$X_Q$ $C_Q$
Output

Print Q Q Q lines.
The i i i-th line ( 1 ≤ i ≤ Q ) (1 \le i \le Q) (1iQ) should contain the answer to the i i i-th query.

Sample Input 1
7 4
ABCDABC
4 B
3 A
5 C
4 G
Sample Output 1
2
1
1
0

After processing each query, S S S becomes as follows.
After the first query: S = S= S= ABCBABC. In this string, ABC appears twice as a substring.
After the second query: S = S= S= ABABABC. In this string, ABC appears once as a substring.
After the third query: S = S= S= ABABCBC. In this string, ABC appears once as a substring.
After the fourth query: S = S= S= ABAGCBC. In this string, ABC appears zero times as a substring.

Sample Input 2
3 3
ABC
1 A
2 B
3 C
Sample Output 2
1
1
1

There are cases where S S S does not change through processing a query.

Sample Input 3
15 10
BBCCBCACCBACACA
9 C
11 B
5 B
11 B
4 A
8 C
8 B
5 B
7 B
14 B
Sample Output 3
0
0
0
0
1
1
2
2
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;

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

	int n, q;
	string s;
	cin >> n >> q >> s;

	auto count = [&](int x) -> int {
		int cnt = 0;
		if (x + 2 < n) cnt += (s[x] == 'A' && s[x + 1] == 'B' && s[x + 2] == 'C');
		if (x > 0 && x + 1 < n) cnt += (s[x - 1] == 'A' && s[x] == 'B' && s[x + 1] == 'C');
		if (x > 1) cnt += (s[x - 2] == 'A' && s[x - 1] == 'B' && s[x] == 'C');
		return cnt;
	};
	int res = 0;
	for (int i = 0; i < n - 2; i ++)
		if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') res ++, i += 2;
	while (q --) {
		int x;
		char c;
		cin >> x >> c, x --;

		res -= count(x), s[x] = c, res += count(x);
		cout << res << endl;
	}


	return 0;
}

D - Buildings

Problem Statement

There are N N N buildings, Building 1 1 1, Building 2 2 2, … \ldots , Building N N N, arranged in a line in this order. The height of Building i i i ( 1 ≤ i ≤ N ) (1 \leq i \leq N) (1iN) is H i H_i Hi.
For each i = 1 , 2 , … , N i = 1, 2, \ldots, N i=1,2,,N, find the number of integers j j j KaTeX parse error: Expected 'EOF', got '&' at position 4: (i &̲lt; j \leq N) satisfying the following condition:
There is no building taller than Building j j j between Buildings i i i and j j j.

Constraints

1 ≤ N ≤ 2 × 1 0 5 1 \leq N \leq 2 \times 10^5 1N2×105
1 ≤ H i ≤ N 1 \leq H_i \leq N 1HiN
$ H_i\neq H_j\ (i\neq j)$
All input values are integers.

Input

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

$N$
$H_1$ $H_2$ $\ldots$ $H_N$
Output

For each i = 1 , 2 , … , N i = 1, 2, \ldots, N i=1,2,,N, let c i c_i ci be the number of j j j satisfying the condition. Print c 1 , c 2 , … , c N c_1, c_2, \ldots, c_N c1,c2,,cN in order, separated by spaces.

Sample Input 1
5
2 1 4 3 5
Sample Output 1
3 2 2 1 0

For i = 1 i=1 i=1, the integers j j j satisfying the condition are 2 2 2, 3 3 3, and 5 5 5: there are three. (Between Buildings 1 1 1 and 4 4 4, there is a building taller than Building 4 4 4, which is Building 3 3 3, so j = 4 j=4 j=4 does not satisfy the condition.) Therefore, the first number in the output is 3 3 3.

Sample Input 2
4
1 2 3 4
Sample Output 2
3 2 1 0
Sample Input 3
10
1 9 6 5 2 7 10 4 8 3
Sample Output 3
2 3 3 3 2 1 2 1 1 0

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<int> a(n + 1);
	for (int i = 1; i <= n; i ++) cin >> a[i];

	stack<int> stk;
	std::vector<int> res(n + 1, 0);
	for (int i = n; i >= 1; i --) {
		res[i] = stk.size();
		while (stk.size() && stk.top() < a[i]) stk.pop();
		stk.push(a[i]);
	}

	for (int i = 1; i <= n; i ++) cout << res[i] << " ";
	cout << endl;

	return 0;
}

E - K-th Largest Connected Components

Problem Statement

There is an undirected graph with N N N vertices and 0 0 0 edges. The vertices are numbered 1 1 1 to N N N.
You are given Q Q Q queries to process in order. Each query is of one of the following two types:
Type 1 1 1: Given in the format 1 u v. Add an edge between vertices u u u and v v v.
Type 2 2 2: Given in the format 2 v k. Print the k k k-th largest vertex number among the vertices connected to vertex v v v. If there are fewer than k k k vertices connected to v v v, print -1.

Constraints

1 ≤ N , Q ≤ 2 × 1 0 5 1 \leq N, Q \leq 2 \times 10^5 1N,Q2×105
In a Type 1 1 1 query, KaTeX parse error: Expected 'EOF', got '&' at position 10: 1 \leq u &̲lt; v \leq N.
In a Type 2 2 2 query, 1 ≤ v ≤ N 1 \leq v \leq N 1vN, 1 ≤ k ≤ 10 1 \leq k \leq 10 1k10.
All input values are integers.

Input

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

N N N Q Q Q
q u e r y 1 \mathrm{query}_1 query1
q u e r y 2 \mathrm{query}_2 query2
⋮ \vdots
q u e r y Q \mathrm{query}_Q queryQ

Here, q u e r y i \mathrm{query}_i queryi is the i i i-th query and is given in one of the following formats:

1 1 1 u u u v v v

2 2 2 v v v k k k

Output

Let q q q be the number of Type 2 2 2 queries. Print q q q lines.
The i i i-th line should contain the answer to the i i i-th Type 2 2 2 query.

Sample Input 1
4 10
1 1 2
2 1 1
2 1 2
2 1 3
1 1 3
1 2 3
1 3 4
2 1 1
2 1 3
2 1 5
Sample Output 1
2
1
-1
4
2
-1

In the first query, an edge is added between vertices 1 1 1 and 2 2 2.
In the second query, two vertices are connected to vertex 1 1 1: 1 1 1 and 2 2 2. Among them, the 1 1 1-st largest vertex number is 2 2 2, which should be printed.
In the third query, two vertices are connected to vertex 1 1 1: 1 1 1 and 2 2 2. Among them, the 2 2 2-nd largest vertex number is 1 1 1, which should be printed.
In the fourth query, two vertices are connected to vertex 1 1 1: 1 1 1 and 2 2 2, which is fewer than 3 3 3, so print -1.
In the fifth query, an edge is added between vertices 1 1 1 and 3 3 3.
In the sixth query, an edge is added between vertices 2 2 2 and 3 3 3.
In the seventh query, an edge is added between vertices 3 3 3 and 4 4 4.
In the eighth query, four vertices are connected to vertex 1 1 1: 1 , 2 , 3 , 4 1,2,3,4 1,2,3,4. Among them, the 1 1 1-st largest vertex number is 4 4 4, which should be printed.
In the ninth query, four vertices are connected to vertex 1 1 1: 1 , 2 , 3 , 4 1,2,3,4 1,2,3,4. Among them, the 3 3 3-rd largest vertex number is 2 2 2, which should be printed.
In the tenth query, four vertices are connected to vertex 1 1 1: 1 , 2 , 3 , 4 1,2,3,4 1,2,3,4, which is fewer than 5 5 5, so print -1.

Sample Input 2
6 20
1 3 4
1 3 5
2 1 1
2 3 1
1 1 5
2 6 9
2 1 3
2 6 1
1 4 6
2 2 1
2 6 2
2 4 7
1 1 4
2 6 2
2 3 4
1 2 5
2 4 1
1 1 6
2 3 3
2 1 3
Sample Output 2
1
5
-1
3
6
2
5
-1
5
3
6
4
4

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, q;
	cin >> n >> q;

	std::vector<set<int>> pnt(n + 1);
	std::vector<int> par(n + 1);
	for (int i = 1; i <= n; i ++) par[i] = i, pnt[i].insert(i);

	auto find = [&](auto self, int x) -> int {
		if (par[x] != x) par[x] = self(self, par[x]);
		return par[x];
	};
	while (q -- ) {
		int op, u, v;
		cin >> op >> u >> v;

		if (op == 1) {
			int pu = find(find, u), pv = find(find, v);
			if (pu == pv) continue;
			if (pnt[pu].size() > pnt[pv].size()) swap(pu, pv);
			for (auto v : pnt[pu]) pnt[pv].insert(v);
			par[pu] = pv;
		} else {
			if (pnt[find(find, u)].size() < v) cout << -1 << endl;
			else {
				auto it = pnt[find(find, u)].end();
				v --, it --;
				while (v -- ) it --;
				cout << *it << endl;
			}
		}
	}

	return 0;
}

F - Teleporting Takahashi 2

Problem Statement

There is a simple directed graph G G G with N N N vertices and N + M N+M N+M edges. The vertices are numbered 1 1 1 to N N N, and the edges are numbered 1 1 1 to N + M N+M N+M.
Edge i i i ( 1 ≤ i ≤ N ) (1 \leq i \leq N) (1iN) goes from vertex i i i to vertex i + 1 i+1 i+1. (Here, vertex N + 1 N+1 N+1 is considered as vertex 1 1 1.)

Edge N + i N+i N+i ( 1 ≤ i ≤ M ) (1 \leq i \leq M) (1iM) goes from vertex X i X_i Xi to vertex Y i Y_i Yi.
Takahashi is at vertex 1 1 1. At each vertex, he can move to any vertex to which there is an outgoing edge from the current vertex.
Compute the number of ways he can move exactly K K K times.
That is, find the number of integer sequences ( v 0 , v 1 , … , v K ) (v_0, v_1, \dots, v_K) (v0,v1,,vK) of length K + 1 K+1 K+1 satisfying all of the following three conditions:
1 ≤ v i ≤ N 1 \leq v_i \leq N 1viN for i = 0 , 1 , … , K i = 0, 1, \dots, K i=0,1,,K.
v 0 = 1 v_0 = 1 v0=1.
There is a directed edge from vertex v i − 1 v_{i-1} vi1 to vertex v i v_i vi for i = 1 , 2 , … , K i = 1, 2, \ldots, K i=1,2,,K.
Since this number can be very large, print it modulo 998244353 998244353 998244353.

Constraints

2 ≤ N ≤ 2 × 1 0 5 2 \leq N \leq 2 \times 10^5 2N2×105
0 ≤ M ≤ 50 0 \leq M \leq 50 0M50
1 ≤ K ≤ 2 × 1 0 5 1 \leq K \leq 2 \times 10^5 1K2×105
1 ≤ X i , Y i ≤ N 1 \leq X_i, Y_i \leq N 1Xi,YiN, X i ≠ Y i X_i \neq Y_i Xi=Yi
All of the N + M N+M N+M directed edges are distinct.
All input values are integers.

Input

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

N N N M M M K K K
X 1 X_1 X1 Y 1 Y_1 Y1
X 2 X_2 X2 Y 2 Y_2 Y2
⋮ \vdots
X M X_M XM Y M Y_M YM

Output

Print the count modulo 998244353 998244353 998244353.

Sample Input 1
6 2 5
1 4
2 5
Sample Output 1
5
sample1 The above figure represents the graph $G$. There are five ways for Takahashi to move: Vertex $1 \to$ Vertex $2 \to$ Vertex $3 \to$ Vertex $4 \to$ Vertex $5 \to$ Vertex $6$ Vertex $1 \to$ Vertex $2 \to$ Vertex $5 \to$ Vertex $6 \to$ Vertex $1 \to$ Vertex $2$ Vertex $1 \to$ Vertex $2 \to$ Vertex $5 \to$ Vertex $6 \to$ Vertex $1 \to$ Vertex $4$ Vertex $1 \to$ Vertex $4 \to$ Vertex $5 \to$ Vertex $6 \to$ Vertex $1 \to$ Vertex $2$ Vertex $1 \to$ Vertex $4 \to$ Vertex $5 \to$ Vertex $6 \to$ Vertex $1 \to$ Vertex $4$ #### Sample Input 2 ``` 10 0 200000 ``` #### Sample Output 2 ``` 1 ``` #### Sample Input 3 ``` 199 10 1326 122 39 142 49 164 119 197 127 188 145 69 80 6 120 24 160 18 154 185 27 ``` #### Sample Output 3 ``` 451022766 ```

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, M = 1e2 + 10, mod = 998244353;

int n, m, k;
std::vector<PII> g[N];
int st[N], dp[M][N], pnt[M], to[M];

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

	cin >> n >> m >> k;
	while (m -- ) {
		int u, v;
		cin >> u >> v;
		g[u].emplace_back(v, 1), st[u] = st[v] = 1;
	}

	int lst = 1, cnt = 0;
	unordered_map<int, int> idx;
	idx[1] = ++ cnt, pnt[1] = 1;
	for (int i = 2; i <= n; i ++)
		if (st[i]) {
			g[lst].emplace_back(i, i - lst), to[cnt] = i - lst;
			lst = i, idx[i] = ++ cnt, pnt[cnt] = i;
		}
	if (~lst) g[lst].emplace_back(1, n - lst + 1), to[cnt] = n - lst + 1;

	dp[idx[1]][0] = 1;
	for (int j = 0; j <= k; j ++)
		for (int i = 1; i <= cnt; i ++) {
			int u = pnt[i];
			for (auto v : g[u])
				if (j + v.se <= k)
					(dp[idx[v.fi]][j + v.se] += dp[i][j]) %= mod;
		}

	int res = 0;
	for (int i = 1; i <= cnt; i ++) {
		// int lst = i - 1;
		// if (!lst) lst = cnt;
		// cout << to[lst] << endl;
		for (int j = k - to[i] + 1; j <= k; j ++) {
			(res += dp[i][j]) %= mod;
			// cout << i << " " << j << ":" << dp[i][j] << endl;
		}
	}

	cout << res << endl;

	return 0;
}

视频题解

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


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值