poj 1017 && uva 311 && la 5526

題目概述

有6種底面爲正方形的箱子,底面邊長分別爲1到6,要用底面爲邊長6的正方形的盒子去裝這些箱子,最少要多少個盒子
箱子不能堆疊

時限

1000ms/3000ms

輸入

每行6個整數,爲每種箱子數量,按邊長升序給出,輸入到6個0爲止

限制

沒有限制

輸出

每行一個數,爲最少盒子數

樣例輸入

0 0 4 0 0 1
7 5 1 0 0 0
36 9 4 1 1 1
0 9 4 1 1 0
0 0 4 0 0 0
36 0 0 0 0 0
0 9 0 0 0 0
79 96 94 30 18 14
53 17 12 98 76 54
83 44 47 42 80 3
15 26 13 29 42 40
41 61 36 90 54 66
78 56 445 45 23 65
13 4 8 29 45 3
15 75 45 98 34 53
40 9 0 2 0 0
41 9 0 2 0 0
44 0 0 0 4 0
0 2 3 0 0 0
37 7 2 0 1 0
12 2 0 1 0 0
13 2 0 1 0 0
0 0 0 0 0 0

樣例輸出

2
1
6
4
1
1
1
86
231
137
115
219
245
79
197
3
4
4
2
3
1
2

討論

貪心,很容易想到的是,先放大的,然後用小的填空,具體一點的說,邊長6的自然是獨佔一個,邊長5的每個盒子最多塞一個,因而有多少就要加多少盒子,同時每次會產生11個放邊長1的空,邊長4的也是之多塞一個,產生的空可以放5個邊長2,如果邊長2夠填滿所有的空,直接填,否則不夠的還得用邊長1來填,邊長3比較特殊,4個才佔一個盒子,模4後爲不滿一個盒子的部分,剩下的空無法單憑邊長2或邊長1填滿,但是可以發現規律,剩1個時,可再放5個邊長2,7個邊長1,剩2個,3個2,6個1,剩1個,1個2,5個1,規律就是,對於邊長2,最多能塞72n3 個,邊長1最多塞8n3 個,當邊長2不夠時,用面積計算需要多少個邊長1,至於邊長2和邊長1,都折算爲邊長1的,按面積算需要多少
其實這個題暴力也能過……

題解狀態

164K,0MS,C++,1013B

題解代碼

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 1004
#define memset0(a) memset(a,0,sizeof(a))

int nums[7];
int fun()
{
    int cnt = 0;
    cnt += nums[6];//邊長6
    cnt += nums[5];//邊長5
    nums[1] -= min(nums[1], nums[5] * 11);
    cnt += nums[4];//邊長4
    if (nums[2] >= nums[4] * 5)
        nums[2] -= nums[4] * 5;
    else {
        nums[1] -= min(nums[1], (nums[4] * 5 - nums[2]) * 4);
        nums[2] = 0;
    }
    cnt += (nums[3] + 3) / 4;//邊長3
    if (nums[3] %= 4) {
        if (nums[2] >= 7 - 2 * nums[3]) {
            nums[2] -= 7 - 2 * nums[3];
            nums[1] -= min(nums[1], 8 - nums[3]);
        }
        else {
            nums[1] -= min(nums[1], 36 - 9 * nums[3] - 4 * nums[2]);
            nums[2] = 0;
        }
    }
    cnt += (nums[2] * 4 + nums[1] + 35) / 36;//邊長2和邊長1
    return cnt;
}
int main(void)
{
    //freopen("vs_cin.txt", "r", stdin);
    //freopen("vs_cout.txt", "w", stdout);

    while (~scanf("%d%d%d%d%d%d", &nums[1], &nums[2], &nums[3], &nums[4], &nums[5], &nums[6]) && (nums[1] + nums[2] + nums[3] + nums[4] + nums[5] + nums[6]))//input
        printf("%d\n", fun());//output
}

EOF

發佈了208 篇原創文章 · 獲贊 2 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章