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 ai×bi. 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 (1t2104) — 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 (1n2105) — the number of slices of cheese Pak Chanek has.

The i-th of the next n lines of each test case contains two integers ai and bi (1ai,bi109) — the dimensions of the i-th slice of cheese.

It is guaranteed that the sum of n over all test cases does not exceed 2105.

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×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.

 

解题思路

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

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

  假设cidi表示第i个矩形的长和高,那么整个图形的周长就是2(c1+c2++cn)+d1+|d1d2|+|d2d3|++|dn1dn|+dn

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

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

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

  如果我们选择将1n1的某个矩形翻转(交换cidi),那么很明显2(c1+c2++cn+dn)会变大。如果翻转第n个矩形,因为dn1dncndn,所以不确定dn1cn哪个大,因此需要分类讨论。

  假设dn1cn,此时第i1个矩形的di1最为高,图形周长为2(c1+c2++cn1+dn1+dn)2(c1+c2++cn1+dn1+dn)2(c1+c2++cn+dn)=2(dn1cn)0,周长变大。

  假设dn1<cn,此时图形周长为2(c1+c2++cn1+dn+cn)=2(c1+c2++cn+dn)

  因此贪心的正确性得证。

  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 @   onlyblues  阅读(46)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示