Codeforces Round 953 (Div. 2 ABCDEF题) 视频讲解

2024-06-19 08:20

本文主要是介绍Codeforces Round 953 (Div. 2 ABCDEF题) 视频讲解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

A. Alice and Books

Problem Statement

Alice has n n n books. The 1 1 1-st book contains a 1 a_1 a1 pages, the 2 2 2-nd book contains a 2 a_2 a2 pages, … \ldots , the n n n-th book contains a n a_n an pages. Alice does the following:

  • She divides all the books into two non-empty piles. Thus, each book ends up in exactly one of the two piles.
  • Alice reads one book with the highest number in each pile.

Alice loves reading very much. Help her find the maximum total number of pages she can read by dividing the books into two piles.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 500 1 \le t \le 500 1t500) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 100 2 \le n \le 100 2n100) — the number of books Alice has.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109) — the number of pages in each book.

Output

For each test case, output a single integer — the maximum number of pages Alice can read.

Example

Example

input
5
2
1 1
4
2 3 3 1
5
2 2 3 2 2
2
10 3
3
1 2 3
output
2
4
5
13
5

Note

In the first test case, Alice can put book number 1 1 1 in the first pile, and book number 2 2 2 in the second pile. Then she will read a 1 + a 2 = 1 + 1 = 2 a_1 + a_2 = 1 + 1 = 2 a1+a2=1+1=2 pages.

In the second test case, Alice can put books with numbers 2 2 2 and 3 3 3 in the first pile, and books with numbers 1 1 1 and 4 4 4 in the second pile. Then she will read the book with the highest number 3 3 3 from the first pile, and the book with the highest number 4 4 4 from the second pile. Then she will read a 3 + a 4 = 3 + 1 = 4 a_3 + a_4 = 3 + 1 = 4 a3+a4=3+1=4 pages.

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 = 1e2 + 10;int n;
int a[N];void solve() {cin >> n;int mx = 0;for (int i = 1; i <= n; i ++)cin >> a[i];for (int i = 1; i < n; i ++)mx = max(mx, a[i]);cout << mx + a[n] << endl;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}

B. New Bakery

Problem Statement

Bob decided to open a bakery. On the opening day, he baked n n n buns that he can sell. The usual price of a bun is a a a coins, but to attract customers, Bob organized the following promotion:

  • Bob chooses some integer k k k ( 0 ≤ k ≤ min ⁡ ( n , b ) 0 \le k \le \min(n, b) 0kmin(n,b)).
  • Bob sells the first k k k buns at a modified price. In this case, the price of the i i i-th ( 1 ≤ i ≤ k 1 \le i \le k 1ik) sold bun is ( b − i + 1 ) (b - i + 1) (bi+1) coins.
  • The remaining ( n − k ) (n - k) (nk) buns are sold at a a a coins each.

Note that k k k can be equal to 0 0 0. In this case, Bob will sell all the buns at a a a coins each.

Help Bob determine the maximum profit he can obtain by selling all n n n buns.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases. The description of the test cases follows.

The only line of each test case contains three integers n n n, a a a, and b b b ( 1 ≤ n , a , b ≤ 1 0 9 1 \le n, a, b \le 10^9 1n,a,b109) — the number of buns, the usual price of a bun, and the price of the first bun to be sold at a modified price.

Output

For each test case, output a single integer — the maximum profit that Bob can obtain.

Example

Example

input
7
4 4 5
5 5 9
10 10 5
5 5 11
1000000000 1000000000 1000000000
1000000000 1000000000 1
1000 1 1000
output
17
35
100
45
1000000000000000000
1000000000000000000
500500

Note

In the first test case, it is optimal for Bob to choose k = 1 k = 1 k=1. Then he will sell one bun for 5 5 5 coins, and three buns at the usual price for 4 4 4 coins each. Then the profit will be 5 + 4 + 4 + 4 = 17 5 + 4 + 4 + 4 = 17 5+4+4+4=17 coins.

In the second test case, it is optimal for Bob to choose k = 5 k = 5 k=5. Then he will sell all the buns at the modified price and obtain a profit of 9 + 8 + 7 + 6 + 5 = 35 9 + 8 + 7 + 6 + 5 = 35 9+8+7+6+5=35 coins.

In the third test case, it is optimal for Bob to choose k = 0 k = 0 k=0. Then he will sell all the buns at the usual price and obtain a profit of 10 ⋅ 10 = 100 10 \cdot 10 = 100 1010=100 coins.

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;void solve() {int n, a, b;cin >> n >> a >> b;int k1 = max(min(b - a, n), 0ll), k2 = max(min(b - a + 1, n), 0ll);cout << max((2 * b - k1 + 1) * k1 / 2 + (n - k1) * a, (2 * b - k2 + 1) * k2 / 2 + (n - k2) * a) << endl;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}

C. Manhattan Permutations

Problem Statement

Let’s call the Manhattan value of a permutation † ^{\dagger} p p p the value of the expression ∣ p 1 − 1 ∣ + ∣ p 2 − 2 ∣ + … + ∣ p n − n ∣ |p_1 - 1| + |p_2 - 2| + \ldots + |p_n - n| p11∣+p22∣++pnn.

For example, for the permutation [ 1 , 2 , 3 ] [1, 2, 3] [1,2,3], the Manhattan value is ∣ 1 − 1 ∣ + ∣ 2 − 2 ∣ + ∣ 3 − 3 ∣ = 0 |1 - 1| + |2 - 2| + |3 - 3| = 0 ∣11∣+∣22∣+∣33∣=0, and for the permutation [ 3 , 1 , 2 ] [3, 1, 2] [3,1,2], the Manhattan value is ∣ 3 − 1 ∣ + ∣ 1 − 2 ∣ + ∣ 2 − 3 ∣ = 2 + 1 + 1 = 4 |3 - 1| + |1 - 2| + |2 - 3| = 2 + 1 + 1 = 4 ∣31∣+∣12∣+∣23∣=2+1+1=4.

You are given integers n n n and k k k. Find a permutation p p p of length n n n such that its Manhattan value is equal to k k k, or determine that no such permutation exists.

† ^{\dagger} A permutation of length n n n is an array consisting of n n n distinct integers from 1 1 1 to n n n in arbitrary order. For example, [ 2 , 3 , 1 , 5 , 4 ] [2,3,1,5,4] [2,3,1,5,4] is a permutation, but [ 1 , 2 , 2 ] [1,2,2] [1,2,2] is not a permutation ( 2 2 2 appears twice in the array), and [ 1 , 3 , 4 ] [1,3,4] [1,3,4] is also not a permutation ( n = 3 n=3 n=3 but there is 4 4 4 in the array).

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^{4} 1t104) — the number of test cases. The description of the test cases follows.

The only line of each test case contains two integers n n n and k k k ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 0 ≤ k ≤ 1 0 12 1 \le n \le 2 \cdot 10^{5}, 0 \le k \le 10^{12} 1n2105,0k1012) — the length of the permutation and the required Manhattan value.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^{5} 2105.

Output

For each test case, if there is no suitable permutation, output “No”. Otherwise, in the first line, output “Yes”, and in the second line, output n n n distinct integers p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,,pn ( 1 ≤ p i ≤ n 1 \le p_i \le n 1pin) — a suitable permutation.

If there are multiple solutions, output any of them.

You can output the answer in any case (for example, the strings “yEs”, “yes”, “Yes”, and “YES” will be recognized as a positive answer).

Example

input
8
3 4
4 5
7 0
1 1000000000000
8 14
112 777
5 12
5 2
output
Yes
3 1 2
No
Yes
1 2 3 4 5 6 7
No
Yes
8 2 3 4 5 6 1 7
No
Yes
5 4 3 1 2
Yes
2 1 3 4 5

Note

In the first test case, the permutation [ 3 , 1 , 2 ] [3, 1, 2] [3,1,2] is suitable, its Manhattan value is ∣ 3 − 1 ∣ + ∣ 1 − 2 ∣ + ∣ 2 − 3 ∣ = 2 + 1 + 1 = 4 |3 - 1| + |1 - 2| + |2 - 3| = 2 + 1 + 1 = 4 ∣31∣+∣12∣+∣23∣=2+1+1=4.

In the second test case, it can be proven that there is no permutation of length 4 4 4 with a Manhattan value of 5 5 5.

In the third test case, the permutation [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] [1,2,3,4,5,6,7] [1,2,3,4,5,6,7] is suitable, its Manhattan value is ∣ 1 − 1 ∣ + ∣ 2 − 2 ∣ + ∣ 3 − 3 ∣ + ∣ 4 − 4 ∣ + ∣ 5 − 5 ∣ + ∣ 6 − 6 ∣ + ∣ 7 − 7 ∣ = 0 |1-1|+|2-2|+|3-3|+|4-4|+|5-5|+|6-6|+|7-7|=0 ∣11∣+∣22∣+∣33∣+∣44∣+∣55∣+∣66∣+∣77∣=0.

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;void solve() {int n, k;cin >> n >> k;if (n * n / 2 < k || k & 1) {cout << "No" << endl;return;}cout << "Yes" << endl;std::vector<int> p(n + 1);int cnt = (n * n / 2 - k) / ((n + 1 >> 1) * 2), pos = ((n * n / 2 - k) % ((n + 1 >> 1) * 2)) / 2;for (int i = 1; i <= cnt; i ++)p[i] = i;for (int i = cnt + 1, j = n / 2 + 1; i <= cnt + (n + 1 >> 1); i ++, j ++)p[i] = j;for (int i = cnt + (n + 1 >> 1) + 1, j = cnt + 1; i <= n; i ++, j ++)p[i] = j;for (int i = cnt + (n + 1 >> 1) + 1, j = pos; j; j --, i --)swap(p[i], p[i - 1]);for (int i = 1; i <= n; i ++)cout << p[i] << " ";cout << endl;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}

D. Elections

Problem Statement

Elections are taking place in Berland. There are n n n candidates participating in the elections, numbered from 1 1 1 to n n n. The i i i-th candidate has a i a_i ai fans who will vote for him. Additionally, there are c c c people who are undecided about their favorite candidate, let’s call them undecided. Undecided people will vote for the candidate with the lowest number.

The candidate who receives the maximum number of votes wins the elections, and if multiple candidates receive the same maximum number of votes, the candidate with the lowest number among them wins.

You found these elections too boring and predictable, so you decided to exclude some candidates from them. If you do not allow candidate number i i i to participate in the elections, all a i a_i ai of his fans will become undecided, and will vote for the candidate with the lowest number.

You are curious to find, for each i i i from 1 1 1 to n n n, the minimum number of candidates that need to be excluded from the elections for candidate number i i i to win the elections.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 2 ⋅ 1 0 4 1 \leq t \leq 2 \cdot 10^4 1t2104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n n n and c c c ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105, 0 ≤ c ≤ 1 0 9 0 \le c \le 10^9 0c109) — the number of candidates in the elections and the number of undecided people.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 0 ≤ a i ≤ 1 0 9 0 \le a_i \le 10^9 0ai109) — the number of fans for each candidate.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output n n n integers, the i i i-th of which should be equal to the minimum number of candidates that need to be excluded from the elections for candidate number i i i to win.

Example

Example

input
5
3 1
2 0 3
2 3
0 10
5 3
5 4 3 2 1
4 5
3 10 7 1
6 0
2 2 2 3 3 3
output
0 1 2
1 0
0 1 2 3 4
1 0 2 3
1 1 2 0 4 5

Note

In the first test case:

  • If all candidates are allowed, candidate number 1 1 1 will receive 3 3 3 votes ( 1 1 1 undecided person will vote for him), candidate number 2 2 2 will receive 0 0 0 votes, and candidate number 3 3 3 will receive 3 3 3 votes. Therefore, candidate number 1 1 1 wins (he received the same number of votes as candidate 3 3 3, but his number is lower), so the answer for him is 0 0 0.
  • If candidate number 1 1 1 is not allowed, his 2 2 2 fans will become undecided. Then candidate number 2 2 2 will receive 3 3 3 votes ( 3 3 3 undecided people will vote for him) and candidate number 3 3 3 will receive 3 3 3 votes. Therefore, candidate number 2 2 2 wins (he received the same number of votes as candidate 3 3 3, but his number is lower), so the answer for him is 1 1 1.
  • If candidates with numbers 1 1 1 and 2 2 2 are not allowed, candidate number 3 3 3 wins, so the answer for him is 2 2 2.

In the second test case, candidate number 1 1 1 will win if candidate number 2 2 2 is not allowed to participate.

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;void solve() {int n, c;cin >> n >> c;std::vector<int> a(n);int mx = 0, p;for (int i = 0; i < n; i ++) {cin >> a[i];if (a[i] > mx) mx = a[i], p = i;}int sum = 0;for (int i = 0; i < n; i ++) {if (p == 0 && i == 0 || i == p && a[0] + c < a[i]) cout << 0 << " ";else if (sum + c + a[i] >= mx) cout << i << " ";else cout << i + 1 << " ";sum += a[i];}cout << endl;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}

E. Computing Machine

Problem Statement

Sasha has two binary strings s s s and t t t of the same length n n n, consisting of the characters 0 and 1.

There is also a computing machine that can perform two types of operations on binary strings a a a and b b b of the same length k k k:

  1. If a i = a i + 2 = a_{i} = a_{i + 2} = ai=ai+2= 0, then you can assign b i + 1 : = b_{i + 1} := bi+1:= 1 ( 1 ≤ i ≤ k − 2 1 \le i \le k - 2 1ik2).
  2. If b i = b i + 2 = b_{i} = b_{i + 2} = bi=bi+2= 1, then you can assign a i + 1 : = a_{i + 1} := ai+1:= 1 ( 1 ≤ i ≤ k − 2 1 \le i \le k - 2 1ik2).

Sasha became interested in the following: if we consider the string a = s l s l + 1 … s r a=s_ls_{l+1}\ldots s_r a=slsl+1sr and the string b = t l t l + 1 … t r b=t_lt_{l+1}\ldots t_r b=tltl+1tr, what is the maximum number of 1 characters in the string a a a that can be obtained using the computing machine. Since Sasha is very curious but lazy, it is up to you to answer this question for several pairs ( l i , r i ) (l_i, r_i) (li,ri) that interest him.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^{4} 1t104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105) — the length of the strings s s s and t t t.

The second line of each test case contains a binary string s s s of length n n n, consisting of the characters 0 and 1.

The third line of each test case contains a binary string t t t of length n n n, consisting of the characters 0 and 1.

The fourth line of each test case contains a single integer q q q ( 1 ≤ q ≤ 2 ⋅ 1 0 5 1 \le q \le 2 \cdot 10^5 1q2105) — the number of queries.

The i i i-th of the following lines contains two integers l i l_{i} li and r i r_{i} ri ( 1 ≤ l i ≤ r i ≤ n 1 \le l_{i} \le r_{i} \le n 1lirin) — the boundaries of the i i i-th pair of substrings that interest Sasha.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105 and the sum of q q q over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output q q q integers — the answers to all queries.

Example

input
3
4
1111
0000
2
1 2
2 4
4
1010
1101
2
1 3
1 4
6
010101
011010
5
2 3
1 6
2 5
4 4
3 6
output
2
3
2
3
1
4
3
1
2

Note

In the first test case:

  • In the first query, a = a = a= 11, so the maximum number of 1 characters is 2 2 2.
  • In the second query, a = a = a= 111, so the maximum number of 1 characters is 3 3 3.

In the second test case:

  • In the first query, a = a = a= 101 and b = b = b= 110. No operations can be performed, so the maximum number of 1 characters is 2 2 2.
  • In the second query, a = a = a= 1010 and b = b = b= 1101. Since a 2 = a 4 = a_2 = a_4 = a2=a4= 0, we can assign b 3 : = b_3 := b3:= 1. Now b 1 = b 3 = b_1 = b_3 = b1=b3= 1, so we can assign a 2 : = a_2 := a2:= 1. The string a a a becomes 1110, so the maximum number of 1 characters is 3 3 3.

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, q, cnt;
string a, b, c, d;
int tot[N];int work(int l, int r) {string s = b, t = a;for (int i = l; i <= r - 2; i ++)if (a[i] == a[i + 2] && a[i] == '0') s[i + 1] = '1';for (int i = l; i <= r - 2; i ++)if (s[i] == s[i + 2] && s[i] == '1') t[i + 1] = '1';int res = 0;for (int i = l; i <= r; i ++)res += t[i] == '1';return res;
}void solve() {cin >> n >> a >> b >> q;a = ' ' + a, b = ' ' + b, c = a, d = b;for (int i = 1; i <= n - 2; i ++)if (c[i] == c[i + 2] && c[i] == '0') d[i + 1] = '1';for (int i = 1; i <= n - 2; i ++)if (d[i] == d[i + 2] && d[i] == '1') c[i + 1] = '1';for (int i = 1; i <= n; i ++)tot[i] = tot[i - 1] + (c[i] == '1');while (q -- ) {int l, r;cin >> l >> r, cnt ++;if (r - l + 1 <= 4) {cout << work(l, r) << endl;continue;}int res = tot[r] - tot[l - 1];if (d[l - 1] == d[l + 1] && d[l - 1] == '1' && a[l] == '0') res --;if (l + 1 <= n && a[l - 1] == '0' && a[l + 1] == '0' && b[l] == '0') {if (l + 2 <= n && d[l + 2] == '1') res --;}if (r + 1 <= n && d[r - 1] == d[r + 1] && d[r + 1] == '1' && a[r] == '0') res --;if (r + 1 <= n && a[r - 1] == '0' && a[r + 1] == '0' && b[r] == '0') {if (r - 2 >= 1 && d[r - 2] == '1') res --;}cout << res << endl;}
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}

F. Large Graph

Problem Statement

Given an array a a a of length n n n. Let’s construct a square matrix b b b of size n × n n \times n n×n, in which the i i i-th row contains the array a a a cyclically shifted to the right by ( i − 1 ) (i - 1) (i1). For example, for the array a = [ 3 , 4 , 5 ] a = [3, 4, 5] a=[3,4,5], the obtained matrix is

b = [ 3 4 5 5 3 4 4 5 3 ] b = \begin{bmatrix} 3 & 4 & 5 \\ 5 & 3 & 4 \\ 4 & 5 & 3 \end{bmatrix} b= 354435543

Let’s construct the following graph:

  • The graph contains n 2 n^2 n2 vertices, each of which corresponds to one of the elements of the matrix. Let’s denote the vertex corresponding to the element b i , j b_{i, j} bi,j as ( i , j ) (i, j) (i,j).
  • We will draw an edge between vertices ( i 1 , j 1 ) (i_1, j_1) (i1,j1) and ( i 2 , j 2 ) (i_2, j_2) (i2,j2) if ∣ i 1 − i 2 ∣ + ∣ j 1 − j 2 ∣ ≤ k |i_1 - i_2| + |j_1 - j_2| \le k i1i2+j1j2k and gcd ⁡ ( b i 1 , j 1 , b i 2 , j 2 ) > 1 \gcd(b_{i_1, j_1}, b_{i_2, j_2}) > 1 gcd(bi1,j1,bi2,j2)>1, where gcd ⁡ ( x , y ) \gcd(x, y) gcd(x,y) denotes the greatest common divisor of integers x x x and y y y.

Your task is to calculate the number of connected components † ^{\dagger} in the obtained graph.

† ^{\dagger} A connected component of a graph is a set of vertices in which any vertex is reachable from any other via edges, and adding any other vertex to the set violates this rule.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 5 1 \leq t \leq 10^5 1t105) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n n n and k k k ( 2 ≤ n ≤ 1 0 6 2 \le n \le 10^6 2n106, 2 ≤ k ≤ 2 ⋅ 1 0 6 2 \le k \le 2 \cdot 10^6 2k2106) — the length of the array and the parameter k k k.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 6 1 \le a_i \le 10^6 1ai106) — the elements of the array a a a.

It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 6 10^6 106.

Output

For each test case, output a single integer — the number of connected components in the obtained graph.

Example

input
6
3 3
3 4 5
3 3
3 4 9
3 2
3 4 9
2 2
2 8
5 3
8 27 5 4 3
4 10
2 2 2 2
output
3
2
3
1
4
1

Note

In the first test case, the matrix b b b is given in the statement. The first connected component contains the vertices ( 1 , 1 ) (1, 1) (1,1), ( 2 , 2 ) (2, 2) (2,2), and ( 3 , 3 ) (3, 3) (3,3). The second connected component contains the vertices ( 1 , 2 ) (1, 2) (1,2), ( 2 , 3 ) (2, 3) (2,3), and ( 3 , 1 ) (3, 1) (3,1). The third connected component contains the vertices ( 1 , 3 ) (1, 3) (1,3), ( 2 , 1 ) (2, 1) (2,1), and ( 3 , 2 ) (3, 2) (3,2). Thus, the graph has 3 3 3 connected components.

In the second test case, the following matrix is obtained:

b = [ 3 4 9 9 3 4 4 9 3 ] b = \begin{bmatrix} 3 & 4 & 9 \\ 9 & 3 & 4 \\ 4 & 9 & 3 \end{bmatrix} b= 394439943

The first connected component contains all vertices corresponding to elements with values 3 3 3 and 9 9 9. The second connected component contains all vertices corresponding to elements with the value 4 4 4.

In the fourth test case, all vertices are in one connected component.

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 = 2e6 + 10;int n, m, k;
int a[N], b[N], p[N];
int st[N], prime[N], idx;
std::vector<int> fact[N], pos[N];int find(int x) {if (p[x] != x) p[x] = find(p[x]);return p[x];
}
void Euler(int lim) {st[1] = 1;for (int i = 2; i <= lim; i ++) {if (!st[i]) prime[ ++ idx] = i;for (int j = 1; prime[j] * i <= lim; j ++) {st[prime[j] * i] = 1;if (i % prime[j] == 0) break;}}
}
void solve() {cin >> n >> k;for (int i = 1; i <= n; i ++)cin >> a[i];m = 0;for (int i = 2; i <= n; i ++) b[ ++ m] = a[i];b[ ++ m] = a[1];for (int i = 2; i <= n; i ++) b[ ++ m] = a[i];int res = m;set<int> avl;for (int i = 1; i <= m; i ++) {p[i] = i, res += (b[i] == 1) * ((m + 1 >> 1) - abs(i - (m + 1 >> 1)) - 1);for (auto v : fact[b[i]])if (!st[v])pos[v].push_back(i), avl.insert(v);}for (auto i : avl)for (int j = 1; j < pos[i].size(); j ++) {if (pos[i][j] - pos[i][j - 1] <= k) {int pa = find(pos[i][j]), pb = find(pos[i][j - 1]);if (pa != pb) {p[pa] = pb;res --;}}}cout << res << endl;for (int i = 1; i <= m; i ++)for (auto v : fact[b[i]])pos[v].clear();
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);for (int i = 1; i < (N >> 1); i ++)for (int j = i; j < (N >> 1); j += i)fact[j].push_back(i);Euler(N >> 1);int dt;cin >> dt;while (dt --) solve();return 0;
}

视频讲解

Codeforces Round 953 (Div. 2)(A ~ F 题讲解)


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

这篇关于Codeforces Round 953 (Div. 2 ABCDEF题) 视频讲解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


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

相关文章

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处

C++快速排序超详细讲解

《C++快速排序超详细讲解》快速排序是一种高效的排序算法,通过分治法将数组划分为两部分,递归排序,直到整个数组有序,通过代码解析和示例,详细解释了快速排序的工作原理和实现过程,需要的朋友可以参考下... 目录一、快速排序原理二、快速排序标准代码三、代码解析四、使用while循环的快速排序1.代码代码1.由快

Java集合中的List超详细讲解

《Java集合中的List超详细讲解》本文详细介绍了Java集合框架中的List接口,包括其在集合中的位置、继承体系、常用操作和代码示例,以及不同实现类(如ArrayList、LinkedList和V... 目录一,List的继承体系二,List的常用操作及代码示例1,创建List实例2,增加元素3,访问元

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

Java如何获取视频文件的视频时长

《Java如何获取视频文件的视频时长》文章介绍了如何使用Java获取视频文件的视频时长,包括导入maven依赖和代码案例,同时,也讨论了在运行过程中遇到的SLF4J加载问题,并给出了解决方案... 目录Java获取视频文件的视频时长1、导入maven依赖2、代码案例3、SLF4J: Failed to lo

Python实现多路视频多窗口播放功能

《Python实现多路视频多窗口播放功能》这篇文章主要为大家详细介绍了Python实现多路视频多窗口播放功能的相关知识,文中的示例代码讲解详细,有需要的小伙伴可以跟随小编一起学习一下... 目录一、python实现多路视频播放功能二、代码实现三、打包代码实现总结一、python实现多路视频播放功能服务端开

Python实现视频转换为音频的方法详解

《Python实现视频转换为音频的方法详解》这篇文章主要为大家详细Python如何将视频转换为音频并将音频文件保存到特定文件夹下,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. python需求的任务2. Python代码的实现3. 代码修改的位置4. 运行结果5. 注意事项