B. Jumbo Extra Cheese 2
B. Jumbo Extra Cheese 2
Pak Chanek has two-dimensional slices of cheese. The -th slice of cheese can be represented as a rectangle of dimensions . 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 () — 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 () — the number of slices of cheese Pak Chanek has.
The -th of the next lines of each test case contains two integers and () — the dimensions of the -th slice of cheese.
It is guaranteed that the sum of over all test cases does not exceed .
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 . 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 , it does not satisfy all conditions of the problem. The bottom edge of the 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 . It can be shown that we cannot get a smaller perimeter.
解题思路
当时比赛的时候被这题搞到心态炸了,想了一个多小时就是不知道怎么贪心。现在回来补个证明。
当每个矩形都竖着放(宽不超过高)时,整个图形的周长最小。
假设和表示第个矩形的长和高,那么整个图形的周长就是
其中如果按照从小到大的顺序来放置矩阵,那么很明显整个图形的周长就是,这确实是能够取到的最小值。整个图形的高至少是,现在我们构造出一个方案恰好取到这个值。剩下的问题就是如何确定每个矩形和的取值,使得最小。
注意到无论如何构造和,和中所有数的最大值必然要被算两次(如果作为高的话要乘,作为宽也要乘),我们可以让这个最大值作为高,那么其他的就会被计算次,此时就很容易贪心地想到对于每个矩形,让和的最小值作为(宽),最大值最为(高)。
按照上面的方式现在我们构造出了和,并且是升序的。下面证明这种构造方法能够取到周长的最小值。
如果我们选择将的某个矩形翻转(交换和),那么很明显会变大。如果翻转第个矩形,因为,,所以不确定和哪个大,因此需要分类讨论。
假设,此时第个矩形的最为高,图形周长为而,周长变大。
假设,此时图形周长为。
因此贪心的正确性得证。
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
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17112680.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效