B. Jumbo Extra Cheese 2

B. Jumbo Extra Cheese 2

Pak Chanek has $n$ two-dimensional slices of cheese. The $i$-th slice of cheese can be represented as a rectangle of dimensions $a_i \times b_i$. We want to arrange them on the two-dimensional plane such that:

  • Each edge of each cheese is parallel to either the x-axis or the y-axis.
  • The bottom edge of each cheese is a segment of the x-axis.
  • No two slices of cheese overlap, but their sides can touch.
  • They form one connected shape.

Note that we can arrange them in any order (the leftmost slice of cheese is not necessarily the first slice of cheese). Also note that we can rotate each slice of cheese in any way as long as all conditions still hold.

Find the minimum possible perimeter of the constructed shape.

Input

Each test contains multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) — the number of test cases. The following lines contain the description of each test case.

The first line of each test case contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the number of slices of cheese Pak Chanek has.

The $i$-th of the next $n$ lines of each test case contains two integers $a_i$ and $b_i$ ($1 \leq a_i,b_i \leq 10^9$) — the dimensions of the $i$-th slice of cheese.

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

Output

For each test case, output a line containing an integer representing the minimum possible perimeter of the constructed shape.

Example

input

3
4
4 1
4 5
1 1
2 3
3
2 4
2 6
2 3
1
2 65

output

26
24
134

Note

In the first test case, a way of getting the minimum possible perimeter is to arrange the slices of cheese as follows.

We can calculate that the perimeter of the constructed shape is $2+5+1+1+1+1+3+1+5+1+2+3=26$. It can be shown that we cannot get a smaller perimeter.

Consider the following invalid arrangement.

Even though the perimeter of the shape above is $24$, it does not satisfy all conditions of the problem. The bottom edge of the $1 \times 1$ slice of cheese is not a segment of the x-axis.

In the second test case, a way of getting the minimum possible perimeter is to arrange the slices of cheese as follows.

We can calculate that the perimeter of the constructed shape is $2+2+2+3+2+3+2+2+2+4=24$. It can be shown that we cannot get a smaller perimeter.

 

解题思路

  当时比赛的时候被这题搞到心态炸了,想了一个多小时就是不知道怎么贪心。现在回来补个证明。

  当每个矩形都竖着放(宽不超过高)时,整个图形的周长最小。

  假设$c_i$和$d_i$表示第$i$个矩形的长和高,那么整个图形的周长就是$$2(c_1+c_2+\ldots+c_n)+d_1+|d_1-d_2|+|d_2-d_3|+\ldots+|d_{n-1}-d_n|+d_n$$

  其中如果按照$d_i$从小到大的顺序来放置矩阵,那么很明显整个图形的周长就是$2(c_1+c_2+\ldots+c_n+d_n)$,这确实是能够取到的最小值。整个图形的高至少是$2 \cdot \max \{ d_i \}$,现在我们构造出一个方案恰好取到这个值。剩下的问题就是如何确定每个矩形$c_i$和$d_i$的取值,使得$2(c_1+c_2+\ldots+c_n+ \max \{ d_i \})$最小。

  注意到无论如何构造$c$和$d$,$a$和$b$中所有数的最大值必然要被算两次(如果作为高的话要乘$2$,作为宽也要乘$2$),我们可以让这个最大值作为高,那么其他的$d_i$就会被计算$0$次,此时就很容易贪心地想到对于每个矩形,让$a_i$和$b_i$的最小值作为$c_i$(宽),最大值最为$d_i$(高)。

  按照上面的方式现在我们构造出了$c_i$和$d_i$,并且$d_i$是升序的。下面证明这种构造方法能够取到周长的最小值。

  如果我们选择将$1 \sim n-1$的某个矩形翻转(交换$c_i$和$d_i$),那么很明显$2(c_1+c_2+\ldots+c_n+d_n)$会变大。如果翻转第$n$个矩形,因为$d_{n-1} \leq d_{n}$,$c_{n} \leq d_{n}$,所以不确定$d_{n-1}$和$c_n$哪个大,因此需要分类讨论。

  假设$d_{n-1} \geq c_n$,此时第$i-1$个矩形的$d_{i-1}$最为高,图形周长为$2(c_1+c_2+\ldots+c_{n-1} + d_{n-1} + d_{n})$而$2(c_1+c_2+\ldots+c_{n-1} + d_{n-1} + d_{n}) - 2(c_1+c_2+\ldots+c_n+d_n) = 2(d_{n-1} - c_n) \geq 0$,周长变大。

  假设$d_{n-1} < c_n$,此时图形周长为$2(c_1+c_2+\ldots+c_{n - 1} + d_{n} + c_{n}) = 2(c_1+c_2+\ldots+c_n+d_n)$。

  因此贪心的正确性得证。

  AC代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 const int N = 2e5 + 10;
 7 
 8 int a[N], b[N];
 9 
10 void solve() {
11     int n;
12     scanf("%d", &n);
13     for (int i = 0; i < n; i++) {
14         scanf("%d %d", a + i, b + i);
15         if (a[i] > b[i]) swap(a[i], b[i]);
16     }
17     printf("%lld\n", accumulate(a, a + n, 0ll) + *max_element(b, b + n) << 1);
18 }
19 
20 int main() {
21     int t;
22     scanf("%d", &t);
23     while (t--) {
24         solve();
25     }
26     
27     return 0;
28 }

 

参考资料

  Codeforces Round #831 A-E:https://zhuanlan.zhihu.com/p/578722890

  Codeforces Round #831 (Div. 1 + Div. 2, based on COMPFEST 14 Final) Editorial:https://codeforces.com/blog/entry/108567

posted @ 2023-02-11 22:06  onlyblues  阅读(41)  评论(0编辑  收藏  举报
Web Analytics