The start of the new academic year brought about the problem of accommodation students into dormitories. One of such dormitories has aa × b square meter wonder room. The caretaker wants to accommodate exactly n students there. But the law says that there must be at least 6 square meters per student in a room (that is, the room for n students must have the area of at least 6n square meters). The caretaker can enlarge any (possibly both) side of the room by an arbitrary positive integer of meters. Help him change the room so as all nstudents could live in it and the total area of the room was as small as possible.
The first line contains three space-separated integers n, a and b (1 ≤ n, a, b ≤ 109) — the number of students and the sizes of the room.
Print three integers s, a1 and b1 (a ≤ a1; b ≤ b1) — the final area of the room and its sizes. If there are multiple optimal solutions, print any of them.
3 3 5
18 3 6
2 4 4
16 4 4
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>`
#include <algorithm>
#include <cctype>
#include <set>
#include <vector>
#define INF 0x7fffffff
#define eps (1e-9)
#define clearto(s,x) memset(s,x,sizeof(s))
using namespace std;
long long n,m,tot=0;
int main()
{
//freopen("D:\data.txt","r",stdin);
int TT,i,k,t;
long long a,b,atp,btp,ansa,ansb;
scanf("%I64d %I64d %I64d",&n,&a,&b); tot =6*n;
if(tot <= a*b ) { printf("%I64d\n%I64d %I64d",a*b,a,b); return 0; }
if(tot%a==0){ b =tot/a; printf("%I64d\n%I64d %I64d",a*b,a,b); return 0; }
if(tot%b==0){ a =tot/b; printf("%I64d\n%I64d %I64d",a*b,a,b); return 0; }
m = ceil( sqrt((double)tot) );
int flag=0;
if(a>b){ a=a^b; b=b^a; a=a^b; flag=1; }
ansa=max(a,m);
ansb=max(b,m);
for(atp=a;atp<=m; atp++){
btp=(tot+atp-1)/atp;
if(atp*btp<ansa*ansb && btp>b) { ansa=atp; ansb=btp; }
}
if(flag) { a=ansb; b=ansa; }
else { a=ansa; b=ansb; }
printf("%I64d\n%I64d %I64d",a*b,a,b); return 0;
}
宿舍房间优化算法
本文介绍了一个关于宿舍房间分配的问题及解决方法。面对新生入学带来的住宿挑战,如何在满足每名学生至少6平方米空间的基础上,通过调整房间尺寸来容纳尽可能多的学生,并确保总面积最小。文章提供了一段C++代码实现解决方案,通过枚举和数学计算确定最优房间尺寸。
1601

被折叠的 条评论
为什么被折叠?



