Array Balancing solution codeforces
You are given two arrays of length nn: a1,a2,…,ana1,a2,…,an and b1,b2,…,bnb1,b2,…,bn.
You can perform the following operation any number of times:
- Choose integer index ii (1≤i≤n1≤i≤n);
- Swap aiai and bibi.
What is the minimum possible sum |a1−a2|+|a2−a3|+⋯+|an−1−an||a1−a2|+|a2−a3|+⋯+|an−1−an| ++ |b1−b2|+|b2−b3|+⋯+|bn−1−bn||b1−b2|+|b2−b3|+⋯+|bn−1−bn| (in other words, ∑i=1n−1(|ai−ai+1|+|bi−bi+1|)∑i=1n−1(|ai−ai+1|+|bi−bi+1|)) you can achieve after performing several (possibly, zero) operations?
Array Balancing solution codeforces
The first line contains a single integer tt (1≤t≤40001≤t≤4000) — the number of test cases. Then, tt test cases follow.
The first line of each test case contains the single integer nn (2≤n≤252≤n≤25) — the length of arrays aa and bb.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the array aa.
The third line of each test case contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤1091≤bi≤109) — the array bb.
For each test case, print one integer — the minimum possible sum ∑i=1n−1(|ai−ai+1|+|bi−bi+1|)∑i=1n−1(|ai−ai+1|+|bi−bi+1|).
Array Balancing solution codeforces
3 4 3 3 10 10 10 10 3 3 5 1 2 3 4 5 6 7 8 9 10 6 72 101 108 108 111 44 10 87 111 114 108 100
0 8 218
In the first test case, we can, for example, swap a3a3 with b3b3 and a4a4 with b4b4. We’ll get arrays a=[3,3,3,3]a=[3,3,3,3] and b=[10,10,10,10]b=[10,10,10,10] with sum 3⋅|3−3|+3⋅|10−10|=3⋅|3−3|+3⋅|10−10|=0.
In the second test case, arrays already have minimum sum (described above) equal to |1−2|+⋯+|4−5|+|6−7|+⋯+|9−10||1−2|+⋯+|4−5|+|6−7|+⋯+|9−10| =4+4=8=4+4=8.
In the third test case, we can, for example, swap a5a5 and b5b5.
ANSWER
“Click Here“

I am the Founder and the creator of the blog neoideasblog.com where we share latest trivia questions and answers on a daily basis.