B. Jumbo Extra Cheese 2

本文解析了Codeforces中一道关于如何通过合理排列多个矩形块以获得最小周长的问题。关键在于理解不可堆叠的约束条件,并通过算法实现最优布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem - B - Codeforces

 

 

 题目大意:给你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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值