You are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$.
In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i \lt j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.
Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.
A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).
The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.
For each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.
43 12 3 13 31 2 34 23 4 1 21 11
1 0 2 0
In the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.
In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
You are given a positive integer $$$n$$$.
Find any permutation $$$p$$$ of length $$$n$$$ such that the sum $$$\operatorname{lcm}(1,p_1) + \operatorname{lcm}(2, p_2) + \ldots + \operatorname{lcm}(n, p_n)$$$ is as large as possible.
Here $$$\operatorname{lcm}(x, y)$$$ denotes the least common multiple (LCM) of integers $$$x$$$ and $$$y$$$.
A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1\,000$$$). Description of the test cases follows.
The only line for each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case print $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, $$$\ldots$$$, $$$p_n$$$ — the permutation with the maximum possible value of $$$\operatorname{lcm}(1,p_1) + \operatorname{lcm}(2, p_2) + \ldots + \operatorname{lcm}(n, p_n)$$$.
If there are multiple answers, print any of them.
212
1 2 1
For $$$n = 1$$$, there is only one permutation, so the answer is $$$[1]$$$.
For $$$n = 2$$$, there are two permutations:
You are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$.
In one operation you do the following:
Find the minimum number of operations required to sort the array in non-decreasing order.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).
The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.
533 3 241 3 1 354 1 5 3 242 4 1 211
1 2 4 3 0
In the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.
In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
An array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.
You can apply the following operation at most $$$k$$$ times:
Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l \lt r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.
You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.
The diameter of a graph is equal to $$$\max\limits_{1 \le u \lt v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).
The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.
63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2
4 168 10 1000000000 9 1000000000
In the first test case, one of the optimal arrays is $$$[2,4,5]$$$.
The graph built on this array:
$$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
This version of the problem differs from the next one only in the constraint on $$$t$$$. You can make hacks only if both versions of the problem are solved.
You are given two positive integers $$$l$$$ and $$$r$$$.
Count the number of distinct triplets of integers $$$(i, j, k)$$$ such that $$$l \le i \lt j \lt k \le r$$$ and $$$\operatorname{lcm}(i,j,k) \ge i + j + k$$$.
Here $$$\operatorname{lcm}(i, j, k)$$$ denotes the least common multiple (LCM) of integers $$$i$$$, $$$j$$$, and $$$k$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$\bf{1 \le t \le 5}$$$). Description of the test cases follows.
The only line for each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 2 \cdot 10^5$$$, $$$l + 2 \le r$$$).
For each test case print one integer — the number of suitable triplets.
51 43 58 8668 866 86868
3 1 78975 969 109229059713337
In the first test case, there are $$$3$$$ suitable triplets:
In the second test case, there is $$$1$$$ suitable triplet:
This version of the problem differs from the previous one only in the constraint on $$$t$$$. You can make hacks only if both versions of the problem are solved.
You are given two positive integers $$$l$$$ and $$$r$$$.
Count the number of distinct triplets of integers $$$(i, j, k)$$$ such that $$$l \le i \lt j \lt k \le r$$$ and $$$\operatorname{lcm}(i,j,k) \ge i + j + k$$$.
Here $$$\operatorname{lcm}(i, j, k)$$$ denotes the least common multiple (LCM) of integers $$$i$$$, $$$j$$$, and $$$k$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$\bf{1 \le t \le 10^5}$$$). Description of the test cases follows.
The only line for each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 2 \cdot 10^5$$$, $$$l + 2 \le r$$$).
For each test case print one integer — the number of suitable triplets.
51 43 58 8668 866 86868
3 1 78975 969 109229059713337
In the first test case, there are $$$3$$$ suitable triplets:
In the second test case, there is $$$1$$$ suitable triplet:
A tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The degree of a vertex is the number of edges connected to this vertex.
You are given a weighted tree with $$$n$$$ vertices, each edge has a weight of $$$1$$$. Let $$$L$$$ be the set of vertices with degree equal to $$$1$$$.
You have to answer $$$q$$$ independent queries. In the $$$i$$$-th query:
The diameter of a graph is equal to $$$\max\limits_{1 \le u \lt v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 10^6$$$).
The second line contains $$$n - 1$$$ integers $$$p_2,p_3,\ldots,p_n$$$ ($$$1 \le p_i \lt i$$$) indicating that there is an edge between vertices $$$i$$$ and $$$p_i$$$. It is guaranteed that the given edges form a tree.
The third line contains a single integer $$$q$$$ ($$$1 \le q \le 10$$$).
The fourth line contains $$$q$$$ integers $$$x_1,x_2,\ldots,x_q$$$ ($$$1 \le x_i \le n$$$). All $$$x_i$$$ are distinct.
Print $$$q$$$ integers in a single line — the answers to the queries.
4 1 2 2 4 1 2 3 4
1 2 2 2
7 1 2 3 4 2 1 7 2 1 3 7 5 6 4
3 3 4 5 5 5 4
3 1 2 1 1
1
The graph in the first test after adding the edges: