[暴力]Wall Builder II 2022牛客多校第4场 H

题目描述

See Problem N for PDF statements.
As the builder employed by NIO, you need to build more walls for him. Now you have a number of bricks of height 111 but with different lengths. More precisely, you have nnn bricks of length 111, n−1n-1n−1 bricks of length 222, ..., two bricks of length n−1n-1n−1, and one brick of length nnn. The width of the bricks is negligible. You need to build a wall with these bricks whose shape must be strictly rectangular. When building the wall, you must keep the height of the bricks at 111, which means you cannot rotate them.

Of course, you can definitely build a wall with a height of 111 and a large length, but it is not a beautiful wall. In NIO's opinion, a beautiful wall should have the minimum possible circumference. Please tell him the minimum circumference of the wall, and how to build it.

输入描述:

The first line contains an integer TTT (1≤T≤1001 \le T \le 1001≤T≤100), indicating the number of test cases.

Each test case contains an integer nnn (1≤n≤1001 \le n \le 1001≤n≤100) in a single line. It is guaranteed that the sum of nnn over all test cases won't exceed 200200200.

输出描述:

For each test case, output an integer in a single line, indicating the minimum circumference of the wall. Then in the next n(n+1)2\dfrac{n(n+1)}{2}2n(n+1)​ lines, each line contains four integers x1,y1,x2,y2x_1,y_1,x_2,y_2x1​,y1​,x2​,y2​, indicating that the coordinate of the lower left of the brick is (x1,y1)(x_1,y_1)(x1​,y1​), and the upper right is (x2,y2)(x_2,y_2)(x2​,y2​).

You can consider the wall as a rectangular in the Cartesian coordinate system, where the xxx-axis represents the length and the yyy-axis represents the height. The lower left of the wall must be located at (0,0)(0,0)(0,0). If there are multiple solutions, output any.
示例1

输入

4
1
2
3
4

输出

4
0 0 1 1
8
0 1 1 2
1 1 2 2
0 0 2 1
14
2 1 3 2
3 1 4 2
4 1 5 2
3 0 5 1
0 1 2 2
0 0 3 1
18
4 0 5 1
2 3 3 4
3 3 4 4
4 3 5 4
3 1 5 2
3 2 5 3
0 3 2 4
0 1 3 2
0 2 3 3
0 0 4 1

说明

A possible solution for the third test case in the sample:

题意: 给出n个1*1的矩形、n-1个1*2的矩形......1个1*n的矩形,求它们能拼成的周长最小的矩形,输出最小周长及方案。

分析: 由于各矩形信息都已知了,那么最终大矩形的面积也就已知了,当面积一定时,最小的周长一定出现在构造出正方形时,设面积为s,两边长分别为a和b,那么s = a*b,周长 = 2*(a+s/a),通过均值不等式可知两边相等时取最值,所以可以从面积的开根开始枚举宽度,然后就进行暴力枚举,枚举成功一行就从下一行接着枚举,枚举的时候要先枚举大的,这样才能保证尽可能拼满,这里其实有一点贪心的思想在,由于长度小的更容易和其它长度拼满,所以它们更加重要,因此要先用不那么重要的长度大的拼,在枚举的时候顺便记录下方案,最后输出即可。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#define pii pair<int, int>
using namespace std;

int vis[105];
vector<pii> p;

signed main()
{
	int T;
	cin >> T;
	while(T--){
		int n;
		scanf("%d", &n);
		int s = 0;
		for(int i = 1; i <= n; i++)
			s += i*(n-i+1);
		int ans;
		int up = floor(sqrt(s));
		for(int i = up; i >= 1; i--){//枚举宽度 
			if(s%i != 0) continue;
			int len = s/i;//长度 
			bool fail = false;
			for(int j = 1; j <= n; j++)
				vis[j] = n-j+1;
			p.clear();
			for(int j = 1; j <= i; j++){
				int now = 0;
				while(now != len){
					for(int k = n; k >= 1; k--){
						if(vis[k] && k <= len-now){
							vis[k]--;
							now += k;
							p.push_back(make_pair(now, j));
							p.push_back(make_pair(now-k, j-1));
							break;
						}
						if(k == 1)
							fail = true;	
					}
					if(fail) break;
				}
				if(fail) break;
			}
			if(!fail){
				ans = 2*i+2*len;
				break;
			}
		}
		printf("%d\n", ans);
		for(int i = p.size()-1; i >= 0; i--){
			printf("%d %d %d %d\n", p[i].first, p[i].second, p[i-1].first, p[i-1].second);
			i--;
		}
	}
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值