当前位置:网站首页>(2022牛客多校四)H-Wall Builder II(思维)
(2022牛客多校四)H-Wall Builder II(思维)
2022-08-01 04:47:00 【AC__dream】
题目:
样例输入:
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题意:给n个1*1的矩形,n-1个1*2的矩形,n-2个1*3的矩形,1个1*n的矩形,把这些矩形拼接成一个大矩形,求大矩形的最小周长。
分析:看了下数据范围,发现n是小于200的。由于我们怎么拼接,最后形成的矩形面积一定是固定的,有个显然的道理就是面积一定的情况下长和宽差越小周长就越小,因为由均值不等式可得ab<=((a+b)/2)^2,等号在a=b时取得,所以我们肯定是从面积的平方根开始枚举长和宽,然后逐一判断用给定的多个小矩形能否拼接成w*h的矩形,单独写一个函数判断即可,比如我们枚举宽,那么能够组成一个矩形的第一个条件就是S%w*w==S,其次才是看能否用小矩形拼接得到。那么我们怎么判断能否用小矩形拼接得到呢?有一个贪心的策略就是说每次填充我们都尽量选择大的矩形,为什么这样一定是最优的呢?其实假如我们现在需要一个1*x的矩形,我们可以由一个1*x的矩形充当,也可以由多个更小的矩形充当,当我们需要一个更小的矩形时,1*x的矩形却没办法分开,但是我们可以用小矩形去填充,而且任何时候小矩形都可以代替大矩形,所以这样贪心一定是正确的。
最后我们用一个vector p[i]记录第i行填的小矩形的宽即可
下面是代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
const int N=1e4+10;
int n;
int cnt[N];
vector<int>p[N];//p[i]存第i行组成的矩形宽度
bool check(int w,int h)
{
for(int i=1;i<=h;i++) p[i].clear();
for(int i=1;i<=n;i++) cnt[i]=0;
for(int i=1;i<=h;i++)
{
int t=w;
for(int j=n;j>=1;j--)
{
while(cnt[j]<n+1-j&&t>=j)
{
p[i].push_back(j);
cnt[j]++;
t-=j;
}
if(!t) break;
}
if(t) return false;
}
return true;
}
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d",&n);
long long ans=0;
for(int i=1;i<=n;i++)
ans+=i*(n+1-i);
for(int i=(int)sqrt(ans);i<=ans;i++)
{
if(ans/i*i!=ans) continue;
if(check(i,ans/i))
{
printf("%d\n",(i+ans/i)*2);
for(int j=ans/i;j>=1;j--)
{
int t=0;
for(int k=0;k<p[j].size();k++)
{
printf("%d %d %d %d\n",t,j-1,t+p[j][k],j);
t+=p[j][k];
}
}
break;
}
}
}
return 0;
}
边栏推荐
猜你喜欢
![Valentine's Day Romantic 3D Photo Wall [with source code]](/img/a9/2c26f4f048f3c0a9a65551bc734233.png)
Valentine's Day Romantic 3D Photo Wall [with source code]

MySQL-数据操作-分组查询-连接查询-子查询-分页查询-联合查询

API Design Notes: The pimpl trick

状态压缩dp

产品经理访谈 | 第五代验证码的创新与背景

The maximum quantity leetcode6133. Grouping (medium)

scheduleWithFixedDelay和scheduleAtFixedRate的区别

基于ProXmoX VE的虚拟化家庭服务器(篇一)—ProXmoX VE 安装及基础配置

Difference Between Compiled and Interpreted Languages

微软 Win10 照片磁贴后的又一“刺客”,谷歌 Chrome 浏览器将在新标签页展示用户照片
随机推荐
UE4 rays flashed from mouse position detection
Unity's primary method for implementing PlanarReflection under the BuildIn rendering pipeline
最新 955 不加班的公司名单
一个往年的朋友
PMP 相关方管理必背总结
【堆】小红的数组
typescript19-对象可选参数
请问表格储存中用sql只能查询到主键列,ots sql非主键不支持吗?
请问shake数据库中为什么读取100个collection 后,直接就退出了,不继续读了呢?
typescript22-接口继承
这里有110+公开的专业数据集
lambda
Make your Lottie support word wrapping in text fields
Software Testing Weekly (Issue 82): In fact, all those who are entangled in making choices already have the answer in their hearts, and consultation is just to get the choice that they prefer.
【kali-信息收集】枚举——DNS枚举:DNSenum、fierce
Game Theory (Depu) and Sun Tzu's Art of War (42/100)
25. 这三道常见的面试题,你有被问过吗?
y83.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十四)
认真对待每一个时刻
Advice given by experts with four years of development experience in Flutter tutorial