首页 > 其他 > 详细

HDU 2335 Containers(暴力枚举)

时间:2016-02-19 17:16:45      阅读:184      评论:0      收藏:0      [点我收藏+]

题目链接:点击打开链接

题意:n个40X8的箱子, 要求建一个矩形场地来放这些箱子, 箱子间有已知大小的间隙, 最高可以放5层。 求场地的最小面积, 在此基础上尽量方。

思路:设场地x列,y行, 那么x*y == (n+4)/5  所以x不会超过sqrt(n), 所以枚举x求y就行了。

比赛的时候考虑到随着x的增加, 答案先变小后变大, 所以三分的, 但是样例都过不了, 后来才注意到是5层。。

细节参见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int mod = 1000000000 + 7;
const int INF = 1000000000;
const int maxn = 100;
int T;
ll n;
struct node {
    ll l, w;
    node(ll l=0, ll w=0):l(l),w(w) {}
};
node cal(ll m1) {
    ll l = 40*m1 + 4*(m1+1);
    ll m2;
    m2 = ceil(n*1.0/m1);
    ll w = 8*m2 + 2*(m2+1);
    return node(l, w);
}
int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%I64d",&n);
        n = (n + 4) / 5;
        ll m = sqrt(n);
        node pre = cal(1LL);
        ll ans_l, ans_w, area = 1LL << 60;
        if(n == 0) {
            printf("0 X 0 = 0\n"); continue;
        }
        for(ll i=1;i<=m;i++) {
            node cur = cal(i);
            if(cur.l * cur.w < area) {
                area = cur.l * cur.w;
                ans_l = cur.l;
                ans_w = cur.w;
            }
            else if(cur.l*cur.w == area && abs(cur.l - cur.w) < abs(ans_l - ans_w)) {
                ans_l = cur.l;
                ans_w = cur.w;
            }
        }
        if(ans_l < ans_w) swap(ans_l, ans_w);
        printf("%I64d X %I64d = %I64d\n",ans_l, ans_w, ans_l*ans_w);
    }
    return 0;
}


HDU 2335 Containers(暴力枚举)

原文:http://blog.csdn.net/weizhuwyzc000/article/details/50698496

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!