A. Brick Wall
Problem Statement
A brick is a strip of size 1 × k 1 \times k 1×k, placed horizontally or vertically, where k k k can be an arbitrary number that is at least 2 2 2 ( k ≥ 2 k \ge 2 k≥2).
A brick wall of size n × m n \times m n×m is such a way to place several bricks inside a rectangle n × m n \times m n×m, that all bricks lie either horizontally or vertically in the cells, do not cross the border of the rectangle, and that each cell of the n × m n \times m n×m rectangle belongs to exactly one brick. Here n n n is the height of the rectangle n × m n \times m n×m and m m m is the width. Note that there can be bricks with different values of k in the same brick wall.
The wall stability is the difference between the number of horizontal bricks and the number of vertical bricks. Note that if you used 0 0 0 horizontal bricks and 2 2 2 vertical ones, then the stability will be − 2 -2 −2, not 2 2 2.
What is the maximal possible stability of a wall of size n × m n \times m n×m?
It is guaranteed that under restrictions in the statement at least one n × m n \times m n×m wall exists.
Input
The first line of the input contains one integer t t t ( 1 ≤ t ≤ 10 000 1 \le t \le 10\,000 1≤t≤10000), the number of test cases.
The only line of each test case contains two integers n n n and m m m ( 2 ≤ n , m ≤ 1 0 4 2 \le n,\,m \le 10^4 2≤n,m≤104).
Output
For each test case, print one integer, the maximum stability of a wall of size n × m n \times m n×m.
Example
Example
input |
---|
5 |
2 2 |
7 8 |
16 9 |
3 5 |
10000 10000 |
output |
---|
2 |
28 |
64 |
6 |
50000000 |
Note
In the 1st test case, the maximum stability of 2 2 2 is obtained by placing two horizontal bricks 1 × 2 1 \times 2 1×2 one on top of the other.
In the 2nd test case, one can get the maximum stability of 28 28 28 by placing 4 4 4 horizontal bricks 1 × 2 1 \times 2 1×2 in each of the 7 7 7 rows.
Solution
A
Code
#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;void solve()
{int N, M;cin >> N >> M;cout << N * (M / 2) << endl;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int Data;cin >> Data;while (Data --)solve();return 0;
}
B. Minimize Inversions
Problem Statement
You are given two permutations a a a and b b b of length n n n. A permutation is an array of n n n elements from 1 1 1 to n n n where all elements are distinct. For example, an array [ 2 , 1 , 3 2,1,3 2,1,3] is a permutation, but [ 0 , 1 0,1 0,1] and [ 1 , 3 , 1 1,3,1 1,3,1] aren’t.
You can (as many times as you want) choose two indices i i i and j j j, then swap a i a_i ai with a j a_j aj and b i b_i bi with b j b_j bj simultaneously.
You hate inversions, so you want to minimize the total number of inversions in both permutations.
An inversion in a permutation p p p is a pair of indices ( i , j ) (i, j) (i,j) such that KaTeX parse error: Expected 'EOF', got '&' at position 3: i &̲lt; j and KaTeX parse error: Expected 'EOF', got '&' at position 5: p_i &̲gt; p_j. For example, if p = [ 3 , 1 , 4 , 2 , 5 ] p=[3,1,4,2,5] p=[3,1,4,2,5] then there are 3 3 3 inversions in it (the pairs of indices are ( 1 , 2 ) (1,2) (1,2), ( 1 , 4 ) (1,4) (1,4) and ( 3 , 4 ) (3,4) (3,4)).
Input
The first line contains an integer t t t ( 1 ≤ t ≤ 20 000 1 \leq t \leq 20\,000 1≤t≤20000) — the number of test cases.
Each test case consists of three lines. The first line contains an integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2\cdot10^5 1≤n≤2⋅105) — the length of the permutations a a a and b b b. The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 1 ≤ a i ≤ n 1 \leq a_i \leq n 1≤ai≤n) — permutation a a a. The third line contains b b b in a similar format.
It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2\cdot10^5 2⋅105.
Output
For each test case, output two permutations a ′ a' a′ and b ′ b' b′ (in the same format as in the input) — the permutations after all operations. The total number of inversions in a ′ a' a′ and b ′ b' b′ should be the minimum possible among all pairs of permutations that can be obtained using operations from the statement.
If there are multiple solutions, print any of them.
Example
Example
input |
---|
3 |
5 |
1 2 3 4 5 |
5 4 3 2 1 |
3 |
3 1 2 |
3 1 2 |
6 |
2 5 6 1 3 4 |
1 5 3 6 2 4 |
output |
---|
3 2 5 1 4 |
3 4 1 5 2 |
1 2 3 |
1 2 3 |
2 3 4 6 5 1 |
1 2 4 3 5 6 |
Note
In the first test case, the minimum possible number of inversions is 10 10 10.
In the second test case, we can sort both permutations at the same time. For this, the following operations can be done:
- Swap the elements in the positions 1 1 1 and 3 3 3 in both permutations. After the operation, a = a = a= [ 2 , 1 , 3 2,1,3 2,1,3], b = b = b= [ 2 , 1 , 3 2,1,3 2,1,3].
- Swap the elements in the positions 1 1 1 and 2 2 2. After the operations, a a a and b b b are sorted.
In the third test case, the minimum possible number of inversions is 7 7 7.
Solution
B
Code
#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int SIZE = 2e5 + 10;int N;
PII A[SIZE];int Cmp(PII a, PII b)
{if (a.first + a.second == b.first + b.second)return abs(a.first - a.second) < abs(b.first - b.second);return a.first + a.second < b.first + b.second;
}void solve()
{cin >> N;for (int i = 1; i <= N; i ++)cin >> A[i].first;for (int i = 1; i <= N; i ++)cin >> A[i].second;sort(A + 1, A + 1 + N, Cmp);for (int i = 1; i <= N; i ++)cout << A[i].first << " ";cout << endl;for (int i = 1; i <= N; i ++)cout << A[i].second << " ";cout << endl;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int Data;cin >> Data;while (Data --)solve();return 0;
}
C. XOR-distance
Problem Statement
You are given integers a a a, b b b, r r r. Find the smallest value of ∣ ( a ⊕ x ) − ( b ⊕ x ) ∣ |({a \oplus x}) - ({b \oplus x})| ∣(a⊕x)−(b⊕x)∣ among all 0 ≤ x ≤ r 0 \leq x \leq r 0≤x≤r.
⊕ \oplus ⊕ is the operation of bitwise XOR, and ∣ y ∣ |y| ∣y∣ is absolute value of y y y.
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases.
Each test case contains integers a a a, b b b, r r r ( 0 ≤ a , b , r ≤ 1 0 18 0 \le a, b, r \le 10^{18} 0≤a,b,r≤1018).
Output
For each test case, output a single number — the smallest possible value.
Example
input |
---|
10 |
4 6 0 |
0 3 2 |
9 6 10 |
92 256 23 |
165 839 201 |
1 14 5 |
2 7 2 |
96549 34359 13851 |
853686404475946 283666553522252166 127929199446003072 |
735268590557942972 916721749674600979 895150420120690183 |
output |
---|
2 |
1 |
1 |
164 |
542 |
5 |
3 |
37102 |
27934920819538516 |
104449824168870225 |
Note
In the first test, when r = 0 r = 0 r=0, then x x x is definitely equal to 0 0 0, so the answer is ∣ 4 ⊕ 0 − 6 ⊕ 0 ∣ = ∣ 4 − 6 ∣ = 2 |{4 \oplus 0} - {6 \oplus 0}| = |4 - 6| = 2 ∣4⊕0−6⊕0∣=∣4−6∣=2.
In the second test:
- When x = 0 x = 0 x=0, ∣ 0 ⊕ 0 − 3 ⊕ 0 ∣ = ∣ 0 − 3 ∣ = 3 |{0 \oplus 0} - {3 \oplus 0}| = |0 - 3| = 3 ∣0⊕0−3⊕0∣=∣0−3∣=3.
- When x = 1 x = 1 x=1, ∣ 0 ⊕ 1 − 3 ⊕ 1 ∣ = ∣ 1 − 2 ∣ = 1 |{0 \oplus 1} - {3 \oplus 1}| = |1 - 2| = 1 ∣0⊕1−3⊕1∣=∣1−2∣=1.
- When x = 2 x = 2 x=2, ∣ 0 ⊕ 2 − 3 ⊕ 2 ∣ = ∣ 2 − 1 ∣ = 1 |{0 \oplus 2} - {3 \oplus 2}| = |2 - 1| = 1 ∣0⊕2−3⊕2∣=∣2−1∣=1.
Therefore, the answer is 1 1 1.
In the third test, the minimum is achieved when x = 1 x = 1 x=1.
Solution
C
Code
#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;void solve()
{int A, B, R;string R1, R2;cin >> A >> B >> R;if (A == B){cout << 0 << endl;return;}if (A < B) swap(A, B);for (int i = 0; i < 64; i ++)R1 += ((A >> i & 1) ^ 48);for (int i = 0; i < 64; i ++)R2 += ((B >> i & 1) ^ 48);int Vis[65] = {0};int Highest;for (int i = 63; i >= 0; i --)if ((A >> i & 1) != (B >> i & 1)){Highest = i;break;}for (int i = 0; i < Highest; i ++)if ((A >> i & 1) && !(B >> i & 1))Vis[i] = 1;int Limit = 1;for (int i = 63; i >= 0; i --){if ((R >> i & 1) && !Vis[i]) Limit = 0;if (Vis[i]){if (Limit){if (R >> i & 1) swap(R1[i], R2[i]);else continue;}elseswap(R1[i], R2[i]);}}int T1 = 0, T2 = 0;for (int i = 0; i < 64; i ++){if (R1[i] == '1')T1 += (1ll << i);if (R2[i] == '1')T2 += (1ll << i);}cout << T1 - T2 << endl;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int Data;cin >> Data;while (Data --)solve();return 0;
}
D. Blocking Elements
Problem Statement
You are given an array of numbers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an. Your task is to block some elements of the array in order to minimize its cost. Suppose you block the elements with indices 1 ≤ b 1 < b 2 < … < b m ≤ n 1 \leq b_1 < b_2 < \ldots < b_m \leq n 1≤b1<b2<…<bm≤n. Then the cost of the array is calculated as the maximum of:
- the sum of the blocked elements, i.e., a b 1 + a b 2 + … + a b m a_{b_1} + a_{b_2} + \ldots + a_{b_m} ab1+ab2+…+abm.
- the maximum sum of the segments into which the array is divided when the blocked elements are removed. That is, the maximum sum of the following ( m + 1 m + 1 m+1) subarrays: [ 1 , b 1 − 1 1, b_1 − 1 1,b1−1], [ b 1 + 1 , b 2 − 1 b_1 + 1, b_2 − 1 b1+1,b2−1], [ … \ldots …], [ b m − 1 + 1 , b m − 1 b_{m−1} + 1, b_m - 1 bm−1+1,bm−1], [ b m + 1 , n b_m + 1, n bm+1,n] (the sum of numbers in a subarray of the form [ x , x − 1 x,x − 1 x,x−1] is considered to be 0 0 0).
For example, if n = 6 n = 6 n=6, the original array is [ 1 , 4 , 5 , 3 , 3 , 2 1, 4, 5, 3, 3, 2 1,4,5,3,3,2], and you block the elements at positions 2 2 2 and 5 5 5, then the cost of the array will be the maximum of the sum of the blocked elements ( 4 + 3 = 7 4 + 3 = 7 4+3=7) and the sums of the subarrays ( 1 1 1, 5 + 3 = 8 5 + 3 = 8 5+3=8, 2 2 2), which is max ( 7 , 1 , 8 , 2 ) = 8 \max(7,1,8,2) = 8 max(7,1,8,2)=8.
You need to output the minimum cost of the array after blocking.
Input
The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 30 000 1 \leq t \leq 30\,000 1≤t≤30000) — the number of queries.
Each test case consists of two lines. The first line contains an integer n n n ( 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1≤n≤105) — the length of the array a a a. The second line contains n n n elements a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1≤ai≤109) — the array a a a.
It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 5 10^5 105.
Output
For each test case, output a single number — the minimum cost of blocking the array.
Example
Example
input |
---|
3 |
6 |
1 4 5 3 3 2 |
5 |
1 2 3 4 5 |
6 |
4 1 6 3 10 7 |
output |
---|
7 |
5 |
11 |
Note
The first test case matches with the array from the statement. To obtain a cost of 7 7 7, you need to block the elements at positions 2 2 2 and 4 4 4. In this case, the cost of the array is calculated as the maximum of:
- the sum of the blocked elements, which is a 2 + a 4 = 7 a_2 + a_4 = 7 a2+a4=7.
- the maximum sum of the segments into which the array is divided when the blocked elements are removed, i.e., the maximum of a 1 a_1 a1, a 3 a_3 a3, a 5 + a 6 = max ( 1 , 5 , 5 ) = 5 a_5 + a_6 = \max(1,5,5) = 5 a5+a6=max(1,5,5)=5.
So the cost is max ( 7 , 5 ) = 7 \max(7,5) = 7 max(7,5)=7.
In the second test case, you can block the elements at positions 1 1 1 and 4 4 4.
In the third test case, to obtain the answer 11 11 11, you can block the elements at positions 2 2 2 and 5 5 5. There are other ways to get this answer, for example, blocking positions 4 4 4 and 6 6 6.
Solution
D
Code
#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int SIZE = 1e5 + 10;int N;
int A[SIZE], S[SIZE];
int F[SIZE];struct Node
{int l, r;int v;
}tree[SIZE * 4];inline void pushup(int u)
{tree[u].v = min(tree[u << 1].v, tree[u << 1 | 1].v);
}inline void build(int u, int l, int r)
{tree[u] = {l, r, (int)1e18};if (l == r) return;int mid = l + r >> 1;build(u << 1, l, mid);build(u << 1 | 1, mid + 1, r);
}inline void modify(int u, int x, int c)
{if (tree[u].l == x && tree[u].r == x) tree[u].v = c;else {int mid = tree[u].l + tree[u].r >> 1;if (x <= mid) modify(u << 1, x, c);else modify(u << 1 | 1, x, c);pushup(u);}return;
}inline int query(int u, int l, int r)
{if (tree[u].l >= l && tree[u].r <= r) return tree[u].v;int mid = tree[u].l + tree[u].r >> 1, v = 1e18;if (l <= mid) v = query(u << 1, l, r);if (r > mid) v = min(v, query(u << 1 | 1, l, r));return v;
}bool Check(int X)
{build(1, 0, N);modify(1, 0, 0);for (int i = 1; i <= N; i ++){int P = lower_bound(S, S + 1 + i - 1, S[i - 1] - X) - S;modify(1, i, query(1, P, i - 1) + A[i]);// cout << query(1, P, i - 1) << " " << P << " " << A[i] << endl;}for (int i = 1; i <= N; i ++)if (max(query(1, i, i), S[N] - S[i]) <= X)return 1;return 0;
}void solve()
{cin >> N;int Sum = 0;for (int i = 1; i <= N; i ++)cin >> A[i], Sum += A[i], S[i] = S[i - 1] + A[i];int l = 0, r = Sum;while (l < r){int mid = l + r >> 1;if (Check(mid)) r = mid;else l = mid + 1;}cout << r << endl;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int Data;cin >> Data;while (Data --)solve();return 0;
}
E. ace5 and Task Order
Problem Statement
This is an interactive problem!
In the new round, there were n n n tasks with difficulties from 1 1 1 to n n n. The coordinator, who decided to have the first round with tasks in unsorted order of difficulty, rearranged the tasks, resulting in a permutation of difficulties from 1 1 1 to n n n. After that, the coordinator challenged ace5 to guess the permutation in the following way.
Initially, the coordinator chooses a number x x x from 1 1 1 to n n n.
ace5 can make queries of the form: ? i ?\ i ? i. The answer will be:
- > > >, if a i > x a_i > x ai>x, after which x x x increases by 1 1 1.
- < < <, if a i < x a_i < x ai<x, after which x x x decreases by 1 1 1.
- = = =, if a i = x a_i = x ai=x, after which x x x remains unchanged.
The task for ace5 is to guess the permutation in no more than 40 n 40n 40n queries. Since ace5 is too busy writing the announcement, he has entrusted this task to you.
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \leq t \leq 1000 1≤t≤1000) — the number of test cases.
Interaction
The interaction between your program and the jury’s program begins with reading a positive integer n n n ( 1 ≤ n ≤ 2000 1 \leq n \leq 2000 1≤n≤2000) — the length of the hidden permutation.
To make a query, output a line in the format “? i”, where 1 ≤ i ≤ n 1 \leq i \leq n 1≤i≤n.
As an answer, you will receive:
- “>”, if a i a_i ai > x x x, after which x x x will increase by 1 1 1.
- “<”, if a i a_i ai < x x x, after which x x x will decrease by 1 1 1.
- “=”, if a i a_i ai = x x x, after which x x x will remain unchanged.
You can make no more than 40 n 40n 40n queries. To output the answer, you need to print “! a_1 a_2 … a_n”, where 1 ≤ a i ≤ n 1 \leq a_i \leq n 1≤ai≤n, and all of them are distinct. Outputting the answer does not count as a query.
If your program makes more than 40 n 40n 40n queries for one test case, or makes an invalid query, then the response to the query will be -1. After receiving such a response, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, it may receive any other verdict.
After outputting a query, do not forget to print a newline and flush the output buffer. Otherwise, you will receive the verdict Presentation Error. To flush the buffer, use:
- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- flush(output) in Pascal;
- stdout.flush() in Python;
- see the documentation for other languages.
It is guaranteed that the sum of n n n over all test cases does not exceed 2000 2000 2000.
The interactor in this problem is not adaptive.
Hacks:
To make a hack, use the following format:
The first line contains a single integer t t t — the number of test cases.
The description of each test case should consist of two lines. The first line contains the numbers n n n and x x x ( 1 ≤ x ≤ n ≤ 2000 1 \leq x \leq n \leq 2000 1≤x≤n≤2000) — the length of the hidden permutation and the initial value of the number x x x. The second line contains n n n distinct numbers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 1 ≤ a i ≤ n 1 \leq a_i \leq n 1≤ai≤n) — the permutation that the jury should choose in this test case.
Sum of n n n over all test cases should not exceed 2000 2000 2000.
Example
input |
---|
2 |
5 |
> |
= |
< |
= |
< |
< |
2 |
> |
output |
---|
? 4 |
? 2 |
? 1 |
? 5 |
? 1 |
? 3 |
! 2 4 1 5 3 |
? 1 |
! 2 1 |
Solution
E
Code
方法一:
#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int SIZE = 2e3 + 10;int N;
int Result[SIZE];char Ask(int X)
{cout << "? " << X << endl;char T;cin >> T;return T;
}void Quick_Sort(vector<int> &A, vector<int> &Order)
{if (!A.size()) return;int mid = rand() % (int)A.size();while (Ask(A[mid]) != '=');std::vector<int> L, R;for (int i = 0; i < A.size(); i ++){if (i == mid) continue;else if (Ask(A[i]) == '<')L.push_back(A[i]);elseR.push_back(A[i]);Ask(A[mid]);}vector<int> L_Order, R_Order;Quick_Sort(L, L_Order), Quick_Sort(R, R_Order);for (auto c : L_Order)Order.push_back(c);Order.push_back(A[mid]);for (auto c : R_Order)Order.push_back(c);
}void solve()
{cin >> N;std::vector<int> A, Order;for (int i = 1; i <= N; i ++)A.push_back(i);Quick_Sort(A, Order);for (int i = 1; i <= N; i ++)Result[Order[i - 1]] = i;cout << "! ";for (int i = 1; i <= N; i ++)cout << Result[i] << " ";cout << endl;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);srand(time(0));int Data;cin >> Data;while (Data --)solve();return 0;
}
方法二:
#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int SIZE = 2e3 + 10;int N;
int L[SIZE], R[SIZE], Result[SIZE];char Ask(int Id)
{cout << "? " << Id << endl;char T;cin >> T;return T;
}void solve()
{cin >> N;int X = 0, Cnt = 0;for (int i = 1; i <= N; i ++)L[i] = -N - 1, R[i] = N + 1;while (Cnt < N){int Max = -1e18, Id;for (int j = 1; j <= N; j ++){if (L[j] == R[j]) continue;if (min(X - L[j], R[j] - X) > Max)Max = min(X - L[j], R[j] - X), Id = j;}char T = Ask(Id);if (T == '>'){X ++;L[Id] = max(L[Id], X);}else if (T == '<'){X --;R[Id] = min(R[Id], X);}elseL[Id] = R[Id] = X;if (L[Id] == R[Id]){Cnt ++;Result[Id] = X;}}int Min = 1e18;for (int i = 1; i <= N; i ++)Min = min(Min, Result[i]);cout << "! ";for (int i = 1; i <= N; i ++)cout << Result[i] - Min + 1 << " ";cout << endl;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int Data;cin >> Data;while (Data --)solve();return 0;
}
F. Caterpillar on a Tree
Problem Statement
The caterpillar decided to visit every node of the tree. Initially, it is sitting at the root.
The tree is represented as a rooted tree with the root at the node 1 1 1. Each crawl to a neighboring node takes 1 1 1 minute for the caterpillar.
And there is a trampoline under the tree. If the caterpillar detaches from the tree and falls onto the trampoline, it will end up at the root of the tree in 0 0 0 seconds. But the trampoline is old and can withstand no more than k k k caterpillar’s falls.
What is the minimum time the caterpillar can take to visit all the nodes of the tree?
More formally, we need to find the minimum time required to visit all the nodes of the tree, if the caterpillar starts at the root (node 1 1 1) and moves using two methods.
- Crawl along an edge to one of the neighboring nodes: takes 1 1 1 minute.
- Teleport to the root: takes no time, no new nodes become visited.
The second method (teleportation) can be used at most k k k times. The caterpillar can finish the journey at any node.
Input
The first line of the input contains two integers: n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2\cdot 10^5 2≤n≤2⋅105) — the number of nodes in the tree, and k k k ( 0 ≤ k ≤ 1 0 9 0 \le k \le 10^9 0≤k≤109) — the maximum number of teleports to the root.
The second line contains n − 1 n-1 n−1 integers p 2 p_2 p2, p 3 p_3 p3, …, p n p_n pn ( 1 ≤ p i ≤ n 1 \le p_i \le n 1≤pi≤n) — the ancestors in the tree for nodes 2 , 3 , … , n 2, 3, \ldots, n 2,3,…,n; node 1 1 1 is the root.
Output
Print a single number in a single line — the minimum number of minutes required to visit all the nodes of the tree.
Example
input |
---|
8 1 |
1 1 2 2 4 3 3 |
output |
---|
9 |
input |
---|
4 0 |
4 4 1 |
output |
---|
4 |
Note
The figure shows one of the ways to traverse the tree from the first test in 9 minutes.
Solution
F
Code
#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int SIZE = 2e5 + 10;int N, K;
std::vector<int> G[SIZE];
int Longest[SIZE], Son[SIZE];
std::vector<int> Euler;
int Vis[SIZE], Depth[SIZE];void DFS(int u, int fa)
{for (auto c : G[u]){Depth[c] = Depth[u] + 1;DFS(c, u);if (Longest[c] + 1 > Longest[u])Longest[u] = Longest[c] + 1, Son[u] = c;}
}void DFS2(int u, int fa)
{Euler.push_back(u);for (auto c : G[u]){if (c == Son[u]) continue;DFS2(c, u);Euler.push_back(u);}if (Son[u])DFS2(Son[u], u), Euler.push_back(u);
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> N >> K;int Fa;for (int i = 2; i <= N; i ++)cin >> Fa, G[Fa].push_back(i);DFS(1, -1);DFS2(1, -1);vector<int> Pos;for (int i = 0; i < Euler.size(); i ++)if (!Vis[Euler[i]])Vis[Euler[i]] = 1, Pos.push_back(i);vector<int> Dis;int Result = Euler.size() - 1 - Longest[1];for (int i = 1; i < Pos.size(); i ++)Dis.push_back(Pos[i] - Pos[i - 1] - Depth[Euler[Pos[i]]]);sort(Dis.begin(), Dis.end(), greater<int>());for (int i = 0; i < min(K, (int)Dis.size()); i ++)Result -= max(Dis[i], 0ll);cout << Result << endl;return 0;
}
最后祝大家早日