题目大意:给你n个边长为a*b的方块,让你通过组合使这个图形的周长最小。
组合的要求如下:
1)每个方块的边平行于x轴或者y轴。
2)每个方块的底边要接触到x轴上。
3)方块不可以堆叠,但是两侧可以接触。
思路:有一点诈骗的意思,因为不可以堆叠只可以通过方块两两接触来缩小周长,那么把最长的边作为y的边,然后每次将方块的长边与y轴平行用于减少周长。那么最后组成的周长就是y轴的最长边*2+每个方块的短边之和*2.
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long
using namespace std;
const int N=1000000+100;
int n ,m,h;
ll s[N];
struct lk{
int a,b;
}cnt[N];
bool cmp(lk x ,lk y ){return x.a>y.a;}
int main()
{
int t;
sc_int(t);
while(t--)
{
sc_int(n);
for(int i =1;i<=n;i++)
{
sc_int(cnt[i].a),sc_int(cnt[i].b);
if(cnt[i].a<cnt[i].b)swap(cnt[i].a,cnt[i].b);
}
sort(cnt+1,cnt+1+n,cmp);
ll res=0;
for(int i =1;i<=n;i++)
{
res+=cnt[i].b;
}// 810
res=2*res+2*cnt[1].a;
cout<<res<<endl;
}
return 0;
}